repair reading from db
That was tiring
This commit is contained in:
		
							parent
							
								
									f2e2b0da0d
								
							
						
					
					
						commit
						af69db6cb8
					
				
					 1 changed files with 140 additions and 26 deletions
				
			
		|  | @ -2,39 +2,155 @@ | ||||||
| Manage the local cache. | Manage the local cache. | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
| /* Read all storeed data in the browser cache. | import logging from "/gui/scripts/logging.JS"; | ||||||
| 
 | 
 | ||||||
| @param {string} prefname the preference name | /* Read all stored data in the browser cache. | ||||||
| @param {int} cloud determine cloud reading, which is otherwise set to automatic (0) | 
 | ||||||
| @return {dictionary} the preferences | @param {array} DATA_NAME the data name | ||||||
|  | @param {int} CLOUD determine cloud reading, which is otherwise set to automatic (0) | ||||||
|  | @param {string} PARAMETER_CHECK Determine which parameter to check via regular expressions. | ||||||
|  | @return {object} the data | ||||||
| */ | */ | ||||||
| export function read(prefname, cloud = 0) { | export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) { | ||||||
| 
 | 
 | ||||||
| 	// Initialize the selected pref data.
 | 	// Initialize the selected pref data.
 | ||||||
| 	let pref_data; | 	let DATA = {}, DATA_ALL = {}, DATA_RETURNED = {}; | ||||||
| 
 | 
 | ||||||
| 	if (prefname) { | 	// Convert the entered prefname to an array if it is not one.
 | ||||||
| 		// Retrieve the data.
 | 	if (!(typeof DATA_NAME).includes(`object`)) { | ||||||
| 		let data_readings = {'sync': null, 'local': null} | 		// Avoid null
 | ||||||
| 		chrome.storage.sync.get(null, (database) => {data_readings[`sync`] = database[prefname]}); | 		if (((typeof DATA_NAME).includes(`str`)) ? DATA_NAME.trim() : DATA_NAME) { | ||||||
| 		chrome.storage.sync.get(null, (database) => {data_readings[`local`] = database[prefname]}); | 			// Syntax of splitting is by commas.
 | ||||||
|  | 			DATA_NAME = (String(DATA_NAME).trim()).split(","); | ||||||
|  | 		} | ||||||
|  | 	}; | ||||||
| 
 | 
 | ||||||
| 		if ((cloud == 0 && data_readings[`sync`]) || cloud == 1) { | 	/* | ||||||
| 			pref_data = data_readings[`sync`]; | 		Find the data now. | ||||||
|  | 
 | ||||||
|  | 		@param {number} SOURCE the data source | ||||||
|  | 	*/ | ||||||
|  | 	function read_database(SOURCE = -1) { | ||||||
|  | 		let data = {}; | ||||||
|  | 		let data_returned; | ||||||
|  | 
 | ||||||
|  | 		async function read_database_local() { | ||||||
|  | 			return new Promise((resolve, reject) => { | ||||||
|  | 				chrome.storage.local.get(null, function(result) { | ||||||
|  | 					if (chrome.runtime.lastError) { | ||||||
|  | 						// Something went wrong
 | ||||||
|  | 						reject(new Error(chrome.runtime.lastError)); | ||||||
| 					} else { | 					} else { | ||||||
| 			pref_data = data_readings[`local`]; | 						// If the key exists, return the value
 | ||||||
|  | 						resolve(result); | ||||||
|  | 					} | ||||||
|  | 				}); | ||||||
|  | 			}); | ||||||
|  | 		}; | ||||||
|  | 
 | ||||||
|  | 		async function read_database_sync() { | ||||||
|  | 			return new Promise((resolve, reject) => { | ||||||
|  | 				chrome.storage.sync.get(null, function(result) { | ||||||
|  | 					if (chrome.runtime.lastError) { | ||||||
|  | 						// Something went wrong
 | ||||||
|  | 						reject(new Error(chrome.runtime.lastError)); | ||||||
|  | 					} else { | ||||||
|  | 						// If the key exists, return the value
 | ||||||
|  | 						resolve(result); | ||||||
|  | 					} | ||||||
|  | 				}); | ||||||
|  | 			}); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		// Return the data.
 | ||||||
|  | 		if (SOURCE > 0) { | ||||||
|  | 			data_returned = read_database_sync(); | ||||||
|  | 		} else { | ||||||
|  | 			data_returned = read_database_local(); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		return(data_returned); | ||||||
|  | 	}; | ||||||
|  | 
 | ||||||
|  | 	/* Recursively find through each data, returning either that value or null when the object is not found. | ||||||
|  | 
 | ||||||
|  | 	@param {dictionary} DATA_ALL the data | ||||||
|  | 	@param {object} DATA_PATH the path of the data | ||||||
|  | 	@return {object} the data | ||||||
|  | 	*/ | ||||||
|  | 	function find_data(DATA_ALL, DATA_PATH, PARAMETER_TEST) { | ||||||
|  | 		// Pull the data out.
 | ||||||
|  | 		let DATA_PATH_SELECTED = String(DATA_PATH.shift()).trim(); | ||||||
|  | 		let DATA_SELECTED = DATA_ALL; | ||||||
|  | 
 | ||||||
|  | 		// Only run when the data is valid.
 | ||||||
|  | 		if (DATA_ALL) { | ||||||
|  | 			if (DATA_SELECTED) { | ||||||
|  | 				// Get the selected data.
 | ||||||
|  | 				DATA_SELECTED = DATA_ALL[DATA_PATH_SELECTED]; | ||||||
|  | 
 | ||||||
|  | 				if (DATA_PATH.length > 0) { | ||||||
|  | 					// Recursively run to make use of the existing data.
 | ||||||
|  | 					DATA_SELECTED = find_data(DATA_SELECTED, DATA_PATH, PARAMETER_TEST); | ||||||
|  | 				} | ||||||
|  | 			} else if (PARAMETER_TEST && DATA_SELECTED) { | ||||||
|  | 				let QUALIFIED = false; | ||||||
|  | 
 | ||||||
|  | 				// The expected keys are "field" and "test value"
 | ||||||
|  | 				DATA_SELECTED_KEYS = Object.keys(DATA_SELECTED); | ||||||
|  | 				if (PARAMETER_TEST[`field`] && PARAMETER_TEST[`test value`]) { | ||||||
|  | 
 | ||||||
|  | 					// Perform a sequential search.
 | ||||||
|  | 					for (let DATA_SELECTED_KEY_INDEX = 0; ((DATA_SELECTED_KEY_INDEX < DATA_SELECTED_KEYS.length) || (!QUALIFIED)); DATA_SELECTED_KEY_INDEX++) { | ||||||
|  | 						PARAMETER_TEST[`value`] = DATA_SELECTED[DATA_SELECTED_KEYS[DATA_SELECTED_KEY_INDEX]][PARAMETER_TEST[`field`]] | ||||||
|  | 							if (PARAMETER_TEST[`value`]) { | ||||||
|  | 								QUALIFIED = (((new RegExp(String(PARAMETER_TEST[`value`])).test(PARAMETER_TEST[`test value`])) || (PARAMETER_TEST[`test value`].includes(PARAMETER_TEST[`value`])))); | ||||||
|  | 							}; | ||||||
|  | 
 | ||||||
|  | 							if (QUALIFIED) { | ||||||
|  | 								DATA_SELECTED = DATA_SELECTED[DATA_SELECTED_KEYS[DATA_SELECTED_KEY_INDEX]]; | ||||||
|  | 								break; | ||||||
|  | 							} | ||||||
|  | 					} | ||||||
|  | 
 | ||||||
|  | 					if (!QUALIFIED) { | ||||||
|  | 						DATA_SELECTED = null; | ||||||
| 					} | 					} | ||||||
| 				} else { | 				} else { | ||||||
| 		// You can get everything if you'd like.
 | 					// It is not valid, so do not return anything.
 | ||||||
| 		if (cloud > 0) { | 					DATA_SELECTED = null; | ||||||
| 			chrome.storage.sync.get(null, (database) => {pref_data = database}); | 				} | ||||||
| 		} else { | 			} | ||||||
| 			chrome.storage.local.get(null, (database) => {pref_data = database;}); | 		} | ||||||
|  | 
 | ||||||
|  | 		// Now return the data.
 | ||||||
|  | 		return (DATA_SELECTED); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Read data from local and sync storage (asynchronous operations)
 | ||||||
|  | 	try { | ||||||
|  | 		if (CLOUD <= 0) { | ||||||
|  | 			[DATA_ALL[`local`]] = await Promise.all([read_database(-1)]); | ||||||
| 		}; | 		}; | ||||||
|  | 		if (CLOUD >= 0) { | ||||||
|  | 			[DATA_ALL[`sync`]] = await Promise.all([read_database(1)]); | ||||||
|  | 		}; | ||||||
|  | 	} catch ({name, message}) { | ||||||
|  | 		logging.error(name, message); | ||||||
| 	}; | 	}; | ||||||
| 
 | 
 | ||||||
|  | 	// Let's get through everything and then determine which one has…
 | ||||||
|  | 	(Object.keys(DATA_ALL)).forEach((DATA_SOURCE) => { | ||||||
|  | 		if (DATA_ALL[DATA_SOURCE]) { | ||||||
|  | 			DATA[DATA_SOURCE] = (DATA_NAME) ? find_data(DATA_ALL[DATA_SOURCE], DATA_NAME, PARAMETER_TEST) : DATA_ALL[DATA_SOURCE]; | ||||||
|  | 		} | ||||||
|  | 	}); | ||||||
| 
 | 
 | ||||||
| 	return(pref_data); | 	// Now return the data.
 | ||||||
|  | 	DATA_RETURNED[`source`] = (CLOUD != 0) ? ((CLOUD > 0) ? `sync` : `local`) : (((DATA[`sync`]) ? (DATA[`sync`].length <= 0) : (DATA[`sync`])) ? `sync` : `local`) | ||||||
|  | 	DATA_RETURNED[`value`] = DATA[DATA_RETURNED[`source`]]; | ||||||
|  | 
 | ||||||
|  | 	return(DATA_RETURNED[`value`]); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* List the matching rule or memory for a particular domain. | /* List the matching rule or memory for a particular domain. | ||||||
|  | @ -112,7 +228,6 @@ export function specifics(WHERE, domain) { | ||||||
| @param {int} CLOUD store in the cloud; otherwise set to automatic | @param {int} CLOUD store in the cloud; otherwise set to automatic | ||||||
| */ | */ | ||||||
| export function write(PREFERENCE, SUBPREFERENCE, DATA, CLOUD = 0) { | export function write(PREFERENCE, SUBPREFERENCE, DATA, CLOUD = 0) { | ||||||
| 
 |  | ||||||
| 	let DATA_INJECTED = DATA; | 	let DATA_INJECTED = DATA; | ||||||
| 
 | 
 | ||||||
| 	if (SUBPREFERENCE) { | 	if (SUBPREFERENCE) { | ||||||
|  | @ -200,7 +315,6 @@ export function forget(preference, subpreference, CLOUD = 0) { | ||||||
| @param {dictionary} data this build's managed data | @param {dictionary} data this build's managed data | ||||||
| */ | */ | ||||||
| export function init(data) { | export function init(data) { | ||||||
| 
 |  | ||||||
| 	let PREFERENCES_ALL = {}; | 	let PREFERENCES_ALL = {}; | ||||||
| 	PREFERENCES_ALL[`build`] = data; | 	PREFERENCES_ALL[`build`] = data; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue