summaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-09-04 17:44:00 -0400
committerGitHub <noreply@github.com>2020-09-04 17:44:00 -0400
commitf3dd2270a5e93614f1b16a0f217b67b7f23059d9 (patch)
tree89a997c9a8ea8fdc4c0c4aeecdfd898a5e19224f /ext/bg/js/backend.js
parent74edf462ab18e851e1e9ff0535b9979909dd98f7 (diff)
Json schema profile conditions (#758)
* Add clearCache function * Add upgrade * Use schema-based profile condition validation * Update profile conditions settings controller * Remove unnecessary async * Remove old * Remove unused templates
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js46
1 files changed, 21 insertions, 25 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 810370c4..7f85d9a5 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -28,13 +28,11 @@
* Mecab
* ObjectPropertyAccessor
* OptionsUtil
+ * ProfileConditions
* RequestBuilder
* TemplateRenderer
* Translator
- * conditionsTestValue
* jp
- * profileConditionsDescriptor
- * profileConditionsDescriptorPromise
*/
class Backend {
@@ -49,6 +47,8 @@ class Backend {
this._options = null;
this._optionsSchema = null;
this._optionsSchemaValidator = new JsonSchemaValidator();
+ this._profileConditionsSchemaCache = [];
+ this._profileConditionsUtil = new ProfileConditions();
this._defaultAnkiFieldTemplates = null;
this._requestBuilder = new RequestBuilder();
this._audioUriBuilder = new AudioUriBuilder({
@@ -200,8 +200,6 @@ class Backend {
}
await this._translator.prepare();
- await profileConditionsDescriptorPromise;
-
this._optionsSchema = await this._fetchAsset('/bg/data/options-schema.json', true);
this._defaultAnkiFieldTemplates = (await this._fetchAsset('/bg/data/default-anki-field-templates.handlebars')).trim();
this._options = await OptionsUtil.load();
@@ -397,6 +395,7 @@ class Backend {
}
async _onApiOptionsSave({source}) {
+ this._clearProfileConditionsSchemaCache();
const options = this.getFullOptions();
await OptionsUtil.save(options);
this._applyOptions(source);
@@ -1006,35 +1005,32 @@ class Backend {
}
_getProfileFromContext(options, optionsContext) {
+ optionsContext = this._profileConditionsUtil.normalizeContext(optionsContext);
+
+ let index = 0;
for (const profile of options.profiles) {
const conditionGroups = profile.conditionGroups;
- if (conditionGroups.length > 0 && this._testConditionGroups(conditionGroups, optionsContext)) {
- return profile;
- }
- }
- return null;
- }
- _testConditionGroups(conditionGroups, data) {
- if (conditionGroups.length === 0) { return false; }
+ let schema;
+ if (index < this._profileConditionsSchemaCache.length) {
+ schema = this._profileConditionsSchemaCache[index];
+ } else {
+ schema = this._profileConditionsUtil.createSchema(conditionGroups);
+ this._profileConditionsSchemaCache.push(schema);
+ }
- for (const conditionGroup of conditionGroups) {
- const conditions = conditionGroup.conditions;
- if (conditions.length > 0 && this._testConditions(conditions, data)) {
- return true;
+ if (conditionGroups.length > 0 && this._optionsSchemaValidator.isValid(optionsContext, schema)) {
+ return profile;
}
+ ++index;
}
- return false;
+ return null;
}
- _testConditions(conditions, data) {
- for (const condition of conditions) {
- if (!conditionsTestValue(profileConditionsDescriptor, condition.type, condition.operator, condition.value, data)) {
- return false;
- }
- }
- return true;
+ _clearProfileConditionsSchemaCache() {
+ this._profileConditionsSchemaCache = [];
+ this._optionsSchemaValidator.clearCache();
}
_checkLastError() {