<commit_before>/* @flow */

import React from 'react-native';
import type { Embed } from './oEmbedTypes';

const {
	AsyncStorage
} = React;

let data;

export default {
	async _readData(): Promise<void> {
		const dataString = await AsyncStorage.getItem('oembed_storage');

		if (dataString) {
			data = JSON.parse(dataString);
		} else {
			data = [];
		}
	},

	_findItem(key: string): ?Embed {
		for (let i = 0, l = data.length; i < l; i++) {
			if (data[i] && data[i].url === key) {
				return data[i].json;
			}
		}

		return null;
	},

	async set(url: string, json: Embed): Promise<void> {
		const item = await this.get(url);

		if (item) {
			item.json = json;
		} else {
			data.push({
				url,
				json
			});
		}

		if (data.length >= 100) {
			data.splice(0, 10);
		}

		return AsyncStorage.setItem('oembed_storage', JSON.stringify(data));
	},

	async get(url: string): Promise<?Embed> {
		if (!data) {
			await this._readData();
		}

		return this._findItem(url);
	}

};<commit_msg>refactor(oembed): use PersistentStorage for oEmbed<commit_after>/* @flow */

import PersistentStorage from '../../lib/PersistentStorage';
import type { Embed } from './oEmbedTypes';

const oEmbedStorage = new PersistentStorage('oembed');

let data;

export default {
	async _readData(): Promise<void> {
		const dataString = await oEmbedStorage.getItem('data');

		if (dataString) {
			data = JSON.parse(dataString);
		} else {
			data = [];
		}
	},

	_findItem(key: string): ?Embed {
		for (let i = 0, l = data.length; i < l; i++) {
			if (data[i] && data[i].url === key) {
				return data[i].json;
			}
		}

		return null;
	},

	async set(url: string, json: Embed): Promise<void> {
		const item = await this.get(url);

		if (item) {
			item.json = json;
		} else {
			data.push({
				url,
				json
			});
		}

		if (data.length >= 100) {
			data.splice(0, 10);
		}

		return oEmbedStorage.setItem('data', JSON.stringify(data));
	},

	async get(url: string): Promise<?Embed> {
		if (!data) {
			await this._readData();
		}

		return this._findItem(url);
	}

};
{'fraction_non_alphanumeric': 0.1739766081871345, 'fraction_numerical': 0.00682261208576998, 'mean_word_length': 3.097804391217565, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2346768', 'n_tokens_mistral': 871, 'n_tokens_neox': 747, 'n_words': 179}