From af69db6cb888189ce83668689c198b5e533a19cd Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Wed, 27 Mar 2024 07:33:40 +0800 Subject: [PATCH] repair reading from db That was tiring --- scripts/secretariat.js | 166 ++++++++++++++++++++++++++++++++++------- 1 file changed, 140 insertions(+), 26 deletions(-) diff --git a/scripts/secretariat.js b/scripts/secretariat.js index 4a4f7d6..d04f8bd 100644 --- a/scripts/secretariat.js +++ b/scripts/secretariat.js @@ -2,39 +2,155 @@ 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 -@param {int} cloud determine cloud reading, which is otherwise set to automatic (0) -@return {dictionary} the preferences +/* Read all stored data in the browser cache. + +@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. - let pref_data; + let DATA = {}, DATA_ALL = {}, DATA_RETURNED = {}; - if (prefname) { - // Retrieve the data. - let data_readings = {'sync': null, 'local': null} - chrome.storage.sync.get(null, (database) => {data_readings[`sync`] = database[prefname]}); - chrome.storage.sync.get(null, (database) => {data_readings[`local`] = database[prefname]}); - - if ((cloud == 0 && data_readings[`sync`]) || cloud == 1) { - pref_data = data_readings[`sync`]; - } else { - pref_data = data_readings[`local`]; + // Convert the entered prefname to an array if it is not one. + if (!(typeof DATA_NAME).includes(`object`)) { + // Avoid null + if (((typeof DATA_NAME).includes(`str`)) ? DATA_NAME.trim() : DATA_NAME) { + // Syntax of splitting is by commas. + DATA_NAME = (String(DATA_NAME).trim()).split(","); } - } else { - // You can get everything if you'd like. - if (cloud > 0) { - chrome.storage.sync.get(null, (database) => {pref_data = database}); - } else { - chrome.storage.local.get(null, (database) => {pref_data = database;}); - }; }; + /* + Find the data now. - return(pref_data); + @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 { + // 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 { + // It is not valid, so do not return anything. + DATA_SELECTED = null; + } + } + } + + // 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]; + } + }); + + // 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. @@ -112,7 +228,6 @@ export function specifics(WHERE, domain) { @param {int} CLOUD store in the cloud; otherwise set to automatic */ export function write(PREFERENCE, SUBPREFERENCE, DATA, CLOUD = 0) { - let DATA_INJECTED = DATA; if (SUBPREFERENCE) { @@ -200,7 +315,6 @@ export function forget(preference, subpreference, CLOUD = 0) { @param {dictionary} data this build's managed data */ export function init(data) { - let PREFERENCES_ALL = {}; PREFERENCES_ALL[`build`] = data;