Options util (#700)

* Move options functions into a class

* Rename and privatize

* Organize by public/private

* Refactor to use async function

* Simplify update function signature

* Add comment for update

* Rename

* Copy _applyUpdates into _legacyProfileUpdateUpdateVersion

* Organize

* Move profile options updates

* Refactor update details

* Add async support

* Formatting
This commit is contained in:
toasted-nutbread 2020-08-01 11:46:35 -04:00 committed by GitHub
parent f1e7288c11
commit b52074b3f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 367 additions and 356 deletions

View File

@ -27,13 +27,12 @@
* JsonSchema * JsonSchema
* Mecab * Mecab
* ObjectPropertyAccessor * ObjectPropertyAccessor
* OptionsUtil
* TemplateRenderer * TemplateRenderer
* Translator * Translator
* conditionsTestValue * conditionsTestValue
* dictTermsSort * dictTermsSort
* jp * jp
* optionsLoad
* optionsSave
* profileConditionsDescriptor * profileConditionsDescriptor
* profileConditionsDescriptorPromise * profileConditionsDescriptorPromise
* requestJson * requestJson
@ -202,7 +201,7 @@ class Backend {
this._optionsSchema = await requestJson(chrome.runtime.getURL('/bg/data/options-schema.json'), 'GET'); this._optionsSchema = await requestJson(chrome.runtime.getURL('/bg/data/options-schema.json'), 'GET');
this._defaultAnkiFieldTemplates = (await requestText(chrome.runtime.getURL('/bg/data/default-anki-field-templates.handlebars'), 'GET')).trim(); this._defaultAnkiFieldTemplates = (await requestText(chrome.runtime.getURL('/bg/data/default-anki-field-templates.handlebars'), 'GET')).trim();
this._options = await optionsLoad(); this._options = await OptionsUtil.load();
this._options = JsonSchema.getValidValueOrDefault(this._optionsSchema, this._options); this._options = JsonSchema.getValidValueOrDefault(this._optionsSchema, this._options);
this._applyOptions('background'); this._applyOptions('background');
@ -396,7 +395,7 @@ class Backend {
async _onApiOptionsSave({source}) { async _onApiOptionsSave({source}) {
const options = this.getFullOptions(); const options = this.getFullOptions();
await optionsSave(options); await OptionsUtil.save(options);
this._applyOptions(source); this._applyOptions(source);
} }

View File

@ -15,45 +15,105 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/* class OptionsUtil {
* Generic options functions static async update(options) {
*/ // Invalid options
if (!isObject(options)) {
function optionsGetStringHashCode(string) { options = {};
let hashCode = 0;
if (typeof string !== 'string') { return hashCode; }
for (let i = 0, charCode = string.charCodeAt(i); i < string.length; charCode = string.charCodeAt(++i)) {
hashCode = ((hashCode << 5) - hashCode) + charCode;
hashCode |= 0;
} }
return hashCode; // Check for legacy options
let defaultProfileOptions = {};
if (!Array.isArray(options.profiles)) {
defaultProfileOptions = options;
options = {};
} }
function optionsGenericApplyUpdates(options, updates) { // Ensure profiles is an array
const targetVersion = updates.length; if (!Array.isArray(options.profiles)) {
const currentVersion = options.version; options.profiles = [];
if (typeof currentVersion === 'number' && Number.isFinite(currentVersion)) {
for (let i = Math.max(0, Math.floor(currentVersion)); i < targetVersion; ++i) {
const update = updates[i];
if (update !== null) {
update(options);
} }
// Remove invalid profiles
const profiles = options.profiles;
for (let i = profiles.length - 1; i >= 0; --i) {
if (!isObject(profiles[i])) {
profiles.splice(i, 1);
} }
} }
options.version = targetVersion; // Require at least one profile
return options; if (profiles.length === 0) {
profiles.push({
name: 'Default',
options: defaultProfileOptions,
conditionGroups: []
});
} }
// Ensure profileCurrent is valid
const profileCurrent = options.profileCurrent;
if (!(
typeof profileCurrent === 'number' &&
Number.isFinite(profileCurrent) &&
Math.floor(profileCurrent) === profileCurrent &&
profileCurrent >= 0 &&
profileCurrent < profiles.length
)) {
options.profileCurrent = 0;
}
/* // Version
* Per-profile options if (typeof options.version !== 'number') {
*/ options.version = 0;
}
const profileOptionsVersionUpdates = [ // Generic updates
return await this._applyUpdates(options, this._getVersionUpdates());
}
static async load() {
let options = null;
try {
const optionsStr = await new Promise((resolve, reject) => {
chrome.storage.local.get(['options'], (store) => {
const error = chrome.runtime.lastError;
if (error) {
reject(new Error(error));
} else {
resolve(store.options);
}
});
});
options = JSON.parse(optionsStr);
} catch (e) {
// NOP
}
return await this.update(options);
}
static save(options) {
return new Promise((resolve, reject) => {
chrome.storage.local.set({options: JSON.stringify(options)}, () => {
const error = chrome.runtime.lastError;
if (error) {
reject(new Error(error));
} else {
resolve();
}
});
});
}
static async getDefault() {
return await this.update({});
}
// Legacy profile updating
static _legacyProfileUpdateGetUpdates() {
return [
null, null,
null, null,
null, null,
@ -72,12 +132,12 @@ const profileOptionsVersionUpdates = [
options.anki.fieldTemplates = null; options.anki.fieldTemplates = null;
}, },
(options) => { (options) => {
if (optionsGetStringHashCode(options.anki.fieldTemplates) === 1285806040) { if (this._getStringHashCode(options.anki.fieldTemplates) === 1285806040) {
options.anki.fieldTemplates = null; options.anki.fieldTemplates = null;
} }
}, },
(options) => { (options) => {
if (optionsGetStringHashCode(options.anki.fieldTemplates) === -250091611) { if (this._getStringHashCode(options.anki.fieldTemplates) === -250091611) {
options.anki.fieldTemplates = null; options.anki.fieldTemplates = null;
} }
}, },
@ -96,7 +156,7 @@ const profileOptionsVersionUpdates = [
(options) => { (options) => {
// Version 12 changes: // Version 12 changes:
// The preferred default value of options.anki.fieldTemplates has been changed to null. // The preferred default value of options.anki.fieldTemplates has been changed to null.
if (optionsGetStringHashCode(options.anki.fieldTemplates) === 1444379824) { if (this._getStringHashCode(options.anki.fieldTemplates) === 1444379824) {
options.anki.fieldTemplates = null; options.anki.fieldTemplates = null;
} }
}, },
@ -141,8 +201,9 @@ const profileOptionsVersionUpdates = [
options.anki.fieldTemplates = fieldTemplates; options.anki.fieldTemplates = fieldTemplates;
} }
]; ];
}
function profileOptionsCreateDefaults() { static _legacyProfileUpdateGetDefaults() {
return { return {
general: { general: {
enable: true, enable: true,
@ -241,8 +302,8 @@ function profileOptionsCreateDefaults() {
}; };
} }
function profileOptionsSetDefaults(options) { static _legacyProfileUpdateAssignDefaults(options) {
const defaults = profileOptionsCreateDefaults(); const defaults = this._legacyProfileUpdateGetDefaults();
const combine = (target, source) => { const combine = (target, source) => {
for (const key in source) { for (const key in source) {
@ -262,136 +323,88 @@ function profileOptionsSetDefaults(options) {
return options; return options;
} }
function profileOptionsUpdateVersion(options) { static _legacyProfileUpdateUpdateVersion(options) {
profileOptionsSetDefaults(options); const updates = this._legacyProfileUpdateGetUpdates();
return optionsGenericApplyUpdates(options, profileOptionsVersionUpdates); this._legacyProfileUpdateAssignDefaults(options);
const targetVersion = updates.length;
const currentVersion = options.version;
if (typeof currentVersion === 'number' && Number.isFinite(currentVersion)) {
for (let i = Math.max(0, Math.floor(currentVersion)); i < targetVersion; ++i) {
const update = updates[i];
if (update !== null) {
update(options);
}
}
} }
options.version = targetVersion;
return options;
}
/* // Private
* Global options
*
* Each profile has an array named "conditionGroups", which is an array of condition groups
* which enable the contextual selection of profiles. The structure of the array is as follows:
* [
* {
* conditions: [
* {
* type: "string",
* operator: "string",
* value: "string"
* },
* // ...
* ]
* },
* // ...
* ]
*/
const optionsVersionUpdates = [ static _getStringHashCode(string) {
(options) => { let hashCode = 0;
if (typeof string !== 'string') { return hashCode; }
for (let i = 0, charCode = string.charCodeAt(i); i < string.length; charCode = string.charCodeAt(++i)) {
hashCode = ((hashCode << 5) - hashCode) + charCode;
hashCode |= 0;
}
return hashCode;
}
static async _applyUpdates(options, updates) {
const targetVersion = updates.length;
let currentVersion = options.version;
if (typeof currentVersion !== 'number' || !Number.isFinite(currentVersion)) {
currentVersion = 0;
}
for (let i = Math.max(0, Math.floor(currentVersion)); i < targetVersion; ++i) {
const {update, async} = updates[i];
const result = update(options);
options = (async ? await result : result);
}
options.version = targetVersion;
return options;
}
static _getVersionUpdates() {
return [
{
async: false,
update: (options) => {
// Version 1 changes:
// Added options.global.database.prefixWildcardsSupported = false
options.global = { options.global = {
database: { database: {
prefixWildcardsSupported: false prefixWildcardsSupported: false
} }
}; };
return options;
} }
]; },
{
function optionsUpdateVersion(options, defaultProfileOptions) { async: false,
// Ensure profiles is an array update: (options) => {
if (!Array.isArray(options.profiles)) { // Version 2 changes:
options.profiles = []; // Legacy profile update process moved into this upgrade function.
} for (const profile of options.profiles) {
// Remove invalid
const profiles = options.profiles;
for (let i = profiles.length - 1; i >= 0; --i) {
if (!isObject(profiles[i])) {
profiles.splice(i, 1);
}
}
// Require at least one profile
if (profiles.length === 0) {
profiles.push({
name: 'Default',
options: defaultProfileOptions,
conditionGroups: []
});
}
// Ensure profileCurrent is valid
const profileCurrent = options.profileCurrent;
if (!(
typeof profileCurrent === 'number' &&
Number.isFinite(profileCurrent) &&
Math.floor(profileCurrent) === profileCurrent &&
profileCurrent >= 0 &&
profileCurrent < profiles.length
)) {
options.profileCurrent = 0;
}
// Update profile options
for (const profile of profiles) {
if (!Array.isArray(profile.conditionGroups)) { if (!Array.isArray(profile.conditionGroups)) {
profile.conditionGroups = []; profile.conditionGroups = [];
} }
profile.options = profileOptionsUpdateVersion(profile.options); profile.options = this._legacyProfileUpdateUpdateVersion(profile.options);
} }
// Version
if (typeof options.version !== 'number') {
options.version = 0;
}
// Generic updates
return optionsGenericApplyUpdates(options, optionsVersionUpdates);
}
function optionsLoad() {
return new Promise((resolve, reject) => {
chrome.storage.local.get(['options'], (store) => {
const error = chrome.runtime.lastError;
if (error) {
reject(new Error(error));
} else {
resolve(store.options);
}
});
}).then((optionsStr) => {
if (typeof optionsStr === 'string') {
const options = JSON.parse(optionsStr);
if (isObject(options)) {
return options; return options;
} }
} }
return {}; ];
}).catch(() => {
return {};
}).then((options) => {
return (
Array.isArray(options.profiles) ?
optionsUpdateVersion(options, {}) :
optionsUpdateVersion({}, options)
);
});
} }
function optionsSave(options) {
return new Promise((resolve, reject) => {
chrome.storage.local.set({options: JSON.stringify(options)}, () => {
const error = chrome.runtime.lastError;
if (error) {
reject(new Error(error));
} else {
resolve();
}
});
});
}
function optionsGetDefault() {
return optionsUpdateVersion({}, {});
} }

View File

@ -16,9 +16,8 @@
*/ */
/* global /* global
* OptionsUtil
* api * api
* optionsGetDefault
* optionsUpdateVersion
*/ */
class SettingsBackup { class SettingsBackup {
@ -323,7 +322,7 @@ class SettingsBackup {
} }
// Upgrade options // Upgrade options
optionsFull = optionsUpdateVersion(optionsFull, {}); optionsFull = await OptionsUtil.update(optionsFull);
// Check for warnings // Check for warnings
const sanitizationWarnings = this._settingsImportSanitizeOptions(optionsFull, true); const sanitizationWarnings = this._settingsImportSanitizeOptions(optionsFull, true);
@ -369,7 +368,7 @@ class SettingsBackup {
$('#settings-reset-modal').modal('hide'); $('#settings-reset-modal').modal('hide');
// Get default options // Get default options
const optionsFull = optionsGetDefault(); const optionsFull = await OptionsUtil.getDefault();
// Assign options // Assign options
await this._settingsImportSetOptionsFull(optionsFull); await this._settingsImportSetOptionsFull(optionsFull);