<commit_before>import * as func from '../core/func';
import { log } from './log';

import pref from './pref';
import storage from './storage';
import defaultPrefs from './defaultPrefs';

function getStorageArea () {
	return storage.sync || storage.local;
}

function prefToSync (prefName, prefValue) {
	return new Promise(resolve => {
		storage.sync.get(prefName, (result) => {
			if (!(prefName in result)) {
				let obj = {};
				obj[prefName] = prefValue;
				storage.sync.set(obj, resolve);
			} else {
				resolve();
			}
		});
	}).then(() => storage.local.remove(prefName));
}

function migratePrefsToSync () {
	if (storage.sync) {
		return new Promise((resolve) => {
			storage.local.get(resolve);
		}).then(result => {
			return Promise.all(Object.keys(result).map((name) => {
				return prefToSync(name, result[name]);
			}));
		});
	}

	return Promise.resolve(null);
}

function createDefaultPrefs () {
	return Promise.all(defaultPrefs.map((pref) => {
		return new Promise(resolve => {
			getStorageArea().get(pref.name, result => {
				if (pref.name in result) {
					resolve(null);
				} else {
					// getStorageArea().set(value);
					resolve([pref.name, pref.value]);
				}
			});
		});
	})).then(values => {
		let saveObj = values.reduce((prev, curr) => {
			if (curr !== null) {
				let [name, value] = curr;
				if (typeof value === 'undefined') {
					value = ':noSuchValue';
				}
				prev[name] = value;
			}
			return prev;
		}, {});

		return new Promise(resolve => getStorageArea().set(saveObj, () => resolve()));
	});
	/* */
}

let events = (function () {
	let listeners = new Map();

	return {
		on (name, cb) {
			if (!listeners.has(name)) {
				listeners.set(name, []);
			}

			listeners.get(name).push(cb);
		},

		off (name, cb) {
			if (listeners.get(name)) {
				for (let i = 0, ll = listeners.get(name).length; i < ll; i++) {
					if (listeners.get(name)[i] === cb) {
						listeners.get(name)[i] = null;
					}
				}

				listeners.set(name, listeners[name].filter((listener) => listener !== null));
			}
		},

		emit (name, args) {
			if (listeners.get(name)) {
				listeners.get(name).forEach((cb) => {
					cb(args);
				});
			}
		}
	};
}());

function validateType (prefType, value) {
	let isValid = false;
	let actualType = Object.prototype.toString.call(value);

	switch (prefType) {

	case 'array':
		isValid = actualType === '[object Array]';
		break;

	case 'string':
		isValid = actualType === '[object String]';
		break;

	case 'bool':<commit_msg>chore(prefs) Use events lib<commit_after>import * as func from '../core/func';
import { log } from './log';
import { createEmitter } from './events';

import pref from './pref';
import storage from './storage';
import defaultPrefs from './defaultPrefs';

function getStorageArea () {
	return storage.sync || storage.local;
}

function prefToSync (prefName, prefValue) {
	return new Promise(resolve => {
		storage.sync.get(prefName, (result) => {
			if (!(prefName in result)) {
				let obj = {};
				obj[prefName] = prefValue;
				storage.sync.set(obj, resolve);
			} else {
				resolve();
			}
		});
	}).then(() => storage.local.remove(prefName));
}

function migratePrefsToSync () {
	if (storage.sync) {
		return new Promise((resolve) => {
			storage.local.get(resolve);
		}).then(result => {
			return Promise.all(Object.keys(result).map((name) => {
				return prefToSync(name, result[name]);
			}));
		});
	}

	return Promise.resolve(null);
}

function createDefaultPrefs () {
	return Promise.all(defaultPrefs.map((pref) => {
		return new Promise(resolve => {
			getStorageArea().get(pref.name, result => {
				if (pref.name in result) {
					resolve(null);
				} else {
					// getStorageArea().set(value);
					resolve([pref.name, pref.value]);
				}
			});
		});
	})).then(values => {
		let saveObj = values.reduce((prev, curr) => {
			if (curr !== null) {
				let [name, value] = curr;
				if (typeof value === 'undefined') {
					value = ':noSuchValue';
				}
				prev[name] = value;
			}
			return prev;
		}, {});

		return new Promise(resolve => getStorageArea().set(saveObj, () => resolve()));
	});
	/* */
}

let events = createEmitter();

function validateType (prefType, value) {
	let isValid = false;
	let actualType = Object.prototype.toString.call(value);

	switch (prefType) {

	case 'array':
		isValid = actualType === '[object Array]';
		break;

	case 'string':
		isValid = actualType === '[object String]';
		break;

	case 'bool':
{'fraction_non_alphanumeric': 0.18695163104611923, 'fraction_numerical': 0.0002249718785151856, 'mean_word_length': 3.270893371757925, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 31, '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': '3259460', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1493, 'n_words': 330}