aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
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() {