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:
parent
f1e7288c11
commit
b52074b3f0
@ -27,13 +27,12 @@
|
||||
* JsonSchema
|
||||
* Mecab
|
||||
* ObjectPropertyAccessor
|
||||
* OptionsUtil
|
||||
* TemplateRenderer
|
||||
* Translator
|
||||
* conditionsTestValue
|
||||
* dictTermsSort
|
||||
* jp
|
||||
* optionsLoad
|
||||
* optionsSave
|
||||
* profileConditionsDescriptor
|
||||
* profileConditionsDescriptorPromise
|
||||
* requestJson
|
||||
@ -202,7 +201,7 @@ class Backend {
|
||||
|
||||
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._options = await optionsLoad();
|
||||
this._options = await OptionsUtil.load();
|
||||
this._options = JsonSchema.getValidValueOrDefault(this._optionsSchema, this._options);
|
||||
|
||||
this._applyOptions('background');
|
||||
@ -396,7 +395,7 @@ class Backend {
|
||||
|
||||
async _onApiOptionsSave({source}) {
|
||||
const options = this.getFullOptions();
|
||||
await optionsSave(options);
|
||||
await OptionsUtil.save(options);
|
||||
this._applyOptions(source);
|
||||
}
|
||||
|
||||
|
@ -15,45 +15,105 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Generic options functions
|
||||
*/
|
||||
|
||||
function optionsGetStringHashCode(string) {
|
||||
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;
|
||||
class OptionsUtil {
|
||||
static async update(options) {
|
||||
// Invalid options
|
||||
if (!isObject(options)) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
return hashCode;
|
||||
// Check for legacy options
|
||||
let defaultProfileOptions = {};
|
||||
if (!Array.isArray(options.profiles)) {
|
||||
defaultProfileOptions = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
function optionsGenericApplyUpdates(options, updates) {
|
||||
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);
|
||||
// Ensure profiles is an array
|
||||
if (!Array.isArray(options.profiles)) {
|
||||
options.profiles = [];
|
||||
}
|
||||
|
||||
// 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;
|
||||
return options;
|
||||
// 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;
|
||||
}
|
||||
|
||||
/*
|
||||
* Per-profile options
|
||||
*/
|
||||
// Version
|
||||
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,
|
||||
@ -72,12 +132,12 @@ const profileOptionsVersionUpdates = [
|
||||
options.anki.fieldTemplates = null;
|
||||
},
|
||||
(options) => {
|
||||
if (optionsGetStringHashCode(options.anki.fieldTemplates) === 1285806040) {
|
||||
if (this._getStringHashCode(options.anki.fieldTemplates) === 1285806040) {
|
||||
options.anki.fieldTemplates = null;
|
||||
}
|
||||
},
|
||||
(options) => {
|
||||
if (optionsGetStringHashCode(options.anki.fieldTemplates) === -250091611) {
|
||||
if (this._getStringHashCode(options.anki.fieldTemplates) === -250091611) {
|
||||
options.anki.fieldTemplates = null;
|
||||
}
|
||||
},
|
||||
@ -96,7 +156,7 @@ const profileOptionsVersionUpdates = [
|
||||
(options) => {
|
||||
// Version 12 changes:
|
||||
// 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;
|
||||
}
|
||||
},
|
||||
@ -141,8 +201,9 @@ const profileOptionsVersionUpdates = [
|
||||
options.anki.fieldTemplates = fieldTemplates;
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function profileOptionsCreateDefaults() {
|
||||
static _legacyProfileUpdateGetDefaults() {
|
||||
return {
|
||||
general: {
|
||||
enable: true,
|
||||
@ -241,8 +302,8 @@ function profileOptionsCreateDefaults() {
|
||||
};
|
||||
}
|
||||
|
||||
function profileOptionsSetDefaults(options) {
|
||||
const defaults = profileOptionsCreateDefaults();
|
||||
static _legacyProfileUpdateAssignDefaults(options) {
|
||||
const defaults = this._legacyProfileUpdateGetDefaults();
|
||||
|
||||
const combine = (target, source) => {
|
||||
for (const key in source) {
|
||||
@ -262,136 +323,88 @@ function profileOptionsSetDefaults(options) {
|
||||
return options;
|
||||
}
|
||||
|
||||
function profileOptionsUpdateVersion(options) {
|
||||
profileOptionsSetDefaults(options);
|
||||
return optionsGenericApplyUpdates(options, profileOptionsVersionUpdates);
|
||||
static _legacyProfileUpdateUpdateVersion(options) {
|
||||
const updates = this._legacyProfileUpdateGetUpdates();
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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"
|
||||
* },
|
||||
* // ...
|
||||
* ]
|
||||
* },
|
||||
* // ...
|
||||
* ]
|
||||
*/
|
||||
// Private
|
||||
|
||||
const optionsVersionUpdates = [
|
||||
(options) => {
|
||||
static _getStringHashCode(string) {
|
||||
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 = {
|
||||
database: {
|
||||
prefixWildcardsSupported: false
|
||||
}
|
||||
};
|
||||
return options;
|
||||
}
|
||||
];
|
||||
|
||||
function optionsUpdateVersion(options, defaultProfileOptions) {
|
||||
// Ensure profiles is an array
|
||||
if (!Array.isArray(options.profiles)) {
|
||||
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) {
|
||||
},
|
||||
{
|
||||
async: false,
|
||||
update: (options) => {
|
||||
// Version 2 changes:
|
||||
// Legacy profile update process moved into this upgrade function.
|
||||
for (const profile of options.profiles) {
|
||||
if (!Array.isArray(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 {};
|
||||
}).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({}, {});
|
||||
}
|
||||
|
@ -16,9 +16,8 @@
|
||||
*/
|
||||
|
||||
/* global
|
||||
* OptionsUtil
|
||||
* api
|
||||
* optionsGetDefault
|
||||
* optionsUpdateVersion
|
||||
*/
|
||||
|
||||
class SettingsBackup {
|
||||
@ -323,7 +322,7 @@ class SettingsBackup {
|
||||
}
|
||||
|
||||
// Upgrade options
|
||||
optionsFull = optionsUpdateVersion(optionsFull, {});
|
||||
optionsFull = await OptionsUtil.update(optionsFull);
|
||||
|
||||
// Check for warnings
|
||||
const sanitizationWarnings = this._settingsImportSanitizeOptions(optionsFull, true);
|
||||
@ -369,7 +368,7 @@ class SettingsBackup {
|
||||
$('#settings-reset-modal').modal('hide');
|
||||
|
||||
// Get default options
|
||||
const optionsFull = optionsGetDefault();
|
||||
const optionsFull = await OptionsUtil.getDefault();
|
||||
|
||||
// Assign options
|
||||
await this._settingsImportSetOptionsFull(optionsFull);
|
||||
|
Loading…
Reference in New Issue
Block a user