From 18fc8d9865406a74dbea47f49ac959a543035ec7 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 20 Aug 2016 19:15:30 -0700 Subject: [PATCH] Performance improvements --- ext/bg/js/deinflector.js | 11 +++++------ ext/bg/js/yomichan.js | 20 +++++++++++++------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index 24289b0c..0eabd0f3 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -48,22 +48,21 @@ class Deinflection { } for (const rule in rules) { - const variants = rules[rule]; - for (const v of variants) { + for (const variant of rules[rule]) { let allowed = this.tags.length === 0; for (const tag of this.tags) { - if (v.ti.indexOf(tag) !== -1) { + if (variant.ti.indexOf(tag) !== -1) { allowed = true; break; } } - if (!allowed || !this.term.endsWith(v.ki)) { + if (!allowed || !this.term.endsWith(variant.ki)) { continue; } - const term = this.term.slice(0, -v.ki.length) + v.ko; - const child = new Deinflection(term, v.to, rule); + const term = this.term.slice(0, -variant.ki.length) + variant.ko; + const child = new Deinflection(term, variant.to, rule); if (child.deinflect(validator, rules)) { this.children.push(child); } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index b8fa9593..fd9b84d3 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -35,6 +35,7 @@ class Yomichan { this.translator = new Translator(); this.asyncPools = {}; + this.ankiConnectVer = 0; this.setState('disabled'); chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); @@ -129,13 +130,18 @@ class Yomichan { } ankiInvokeSafe(action, params, pool, callback) { - this.api_getVersion({callback: (version) => { - if (version === this.getApiVersion()) { - this.ankiInvoke(action, params, pool, callback); - } else { - callback(null); - } - }}); + if (this.ankiConnectVer === this.getApiVersion()) { + this.ankiInvoke(action, params, pool, callback); + } else { + this.api_getVersion({callback: (version) => { + if (version === this.getApiVersion()) { + this.ankiConnectVer = version; + this.ankiInvoke(action, params, pool, callback); + } else { + callback(null); + } + }}); + } } ankiInvoke(action, params, pool, callback) {