aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-08-09 21:23:05 -0700
committerAlex Yatskov <alex@foosoft.net>2016-08-09 21:23:05 -0700
commite6d821b73143c4825ecf915a7bbf838aa8aa0b9a (patch)
treeae052e2567f47f2de15e26ea2901127717a75196 /ext
parentdcce58fc2eab1f3b23abb90f5ce30536d7dbf1c1 (diff)
Cleanup using promises
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/translator.js20
-rw-r--r--ext/fg/js/api.js24
-rw-r--r--ext/fg/js/client.js95
-rw-r--r--ext/fg/js/source-range.js2
4 files changed, 74 insertions, 67 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 7b776383..2d15f05b 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -65,7 +65,7 @@ class Translator {
const dfs = this.deinflector.deinflect(term, t => {
const tags = [];
- for (let d of this.dictionary.findTerm(t)) {
+ for (const d of this.dictionary.findTerm(t)) {
tags.push(d.tags);
}
@@ -76,13 +76,13 @@ class Translator {
continue;
}
- for (let df of dfs) {
+ for (const df of dfs) {
this.processTerm(groups, df.source, df.tags, df.rules, df.root);
}
}
let definitions = [];
- for (let key in groups) {
+ for (const key in groups) {
definitions.push(groups[key]);
}
@@ -115,7 +115,7 @@ class Translator {
});
let length = 0;
- for (let result of definitions) {
+ for (const result of definitions) {
length = Math.max(length, result.source.length);
}
@@ -126,7 +126,7 @@ class Translator {
let definitions = [];
const processed = {};
- for (let c of text) {
+ for (const c of text) {
if (!processed[c]) {
definitions = definitions.concat(this.dictionary.findKanji(c));
processed[c] = true;
@@ -137,13 +137,13 @@ class Translator {
}
processTerm(groups, dfSource, dfTags, dfRules=[], dfRoot='') {
- for (let entry of this.dictionary.findTerm(dfRoot)) {
+ for (const entry of this.dictionary.findTerm(dfRoot)) {
if (entry.id in groups) {
continue;
}
let matched = dfTags.length === 0;
- for (let t of dfTags) {
+ for (const t of dfTags) {
if (entry.tags.indexOf(t) !== -1) {
matched = true;
break;
@@ -157,7 +157,7 @@ class Translator {
let popular = false;
let tags = [];
- for (let t of entry.tags) {
+ for (const t of entry.tags) {
const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: entry.entities[t] || '', name: t};
this.applyTagMeta(tag);
tags.push(tag);
@@ -186,9 +186,9 @@ class Translator {
processKanji(entries) {
const processed = [];
- for (let entry of entries) {
+ for (const entry of entries) {
const tags = [];
- for (let t of entry.tags) {
+ for (const t of entry.tags) {
const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: '', name: t};
this.applyTagMeta(tag);
tags.push(tag);
diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js
index 7316c604..643d0360 100644
--- a/ext/fg/js/api.js
+++ b/ext/fg/js/api.js
@@ -17,26 +17,26 @@
*/
-function bgSendMessage(action, params, callback) {
- chrome.runtime.sendMessage({action, params}, callback);
+function bgSendMessage(action, params) {
+ return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve));
}
-function bgFindTerm(text, callback) {
- bgSendMessage('findTerm', {text}, callback);
+function bgFindTerm(text) {
+ return bgSendMessage('findTerm', {text});
}
-function bgFindKanji(text, callback) {
- bgSendMessage('findKanji', {text}, callback);
+function bgFindKanji(text) {
+ return bgSendMessage('findKanji', {text});
}
-function bgRenderText(data, template, callback) {
- bgSendMessage('renderText', {data, template}, callback);
+function bgRenderText(data, template) {
+ return bgSendMessage('renderText', {data, template});
}
-function bgCanAddDefinitions(definitions, modes, callback) {
- bgSendMessage('canAddDefinitions', {definitions, modes}, callback);
+function bgCanAddDefinitions(definitions, modes) {
+ return bgSendMessage('canAddDefinitions', {definitions, modes});
}
-function bgAddDefinition(definition, mode, callback) {
- bgSendMessage('addDefinition', {definition, mode}, callback);
+function bgAddDefinition(definition, mode) {
+ return bgSendMessage('addDefinition', {definition, mode});
}
diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js
index 34a1a605..202d6dbd 100644
--- a/ext/fg/js/client.js
+++ b/ext/fg/js/client.js
@@ -90,10 +90,16 @@ class Client {
}
textSource.setEndOffset(this.options.scanLength);
- bgFindTerm(textSource.text(), ({definitions, length}) => {
- if (length === 0) {
- this.hidePopup();
- } else {
+
+ let defs = [];
+ let seq = -1;
+
+ bgFindTerm(textSource.text())
+ .then(({definitions, length}) => {
+ if (length === 0) {
+ return Promise.reject();
+ }
+
textSource.setEndOffset(length);
const sentence = Client.extractSentence(textSource, this.options.sentenceExtent);
@@ -102,23 +108,22 @@ class Client {
definition.sentence = sentence;
});
- const sequence = ++this.sequence;
- bgRenderText(
- {definitions, root: this.fgRoot, options: this.options, sequence},
- 'term-list.html',
- (content) => {
- this.definitions = definitions;
- this.showPopup(textSource, content);
-
- bgCanAddDefinitions(definitions, ['vocab_kanji', 'vocab_kana'], (states) => {
- if (states !== null) {
- states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence}));
- }
- });
- }
- );
- }
- });
+ defs = definitions;
+ seq = ++this.sequence;
+
+ return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'term-list.html');
+ })
+ .then((content) => {
+ this.definitions = defs;
+ this.showPopup(textSource, content);
+
+ return bgCanAddDefinitions(defs, ['vocab_kanji', 'vocab_kana']);
+ })
+ .then((states) => {
+ if (states !== null) {
+ states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
+ }
+ }, () => this.hidePopup());
}
showPopup(textSource, content) {
@@ -138,7 +143,7 @@ class Client {
this.lastTextSource.deselect();
}
- this.lastTextSource = null;
+ this.lastTextSource = null;
this.definitions = null;
}
@@ -156,7 +161,7 @@ class Client {
const state = {};
state[mode] = false;
- bgAddDefinition(this.definitions[index], mode, (success) => {
+ bgAddDefinition(this.definitions[index], mode).then((success) => {
if (success) {
this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence});
} else {
@@ -173,7 +178,7 @@ class Client {
url += `&kana=${encodeURIComponent(definition.reading)}`;
}
- for (let key in this.audio) {
+ for (const key in this.audio) {
this.audio[key].pause();
}
@@ -185,27 +190,29 @@ class Client {
}
api_displayKanji(kanji) {
- bgFindKanji(kanji, (definitions) => {
- definitions.forEach((definition) => {
- definition.url = window.location.href;
- });
-
- const sequence = ++this.sequence;
- bgRenderText(
- {definitions, root: this.fgRoot, options: this.options, sequence},
- 'kanji-list.html',
- (content) => {
- this.definitions = definitions;
- this.popup.setContent(content, definitions);
-
- bgCanAddDefinitions(definitions, ['kanji'], (states) => {
- if (states !== null) {
- states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence}));
- }
- });
+ let defs = [];
+ let seq = -1;
+
+ bgFindKanji(kanji)
+ .then((definitions) => {
+ definitions.forEach((definition) => definition.url = window.location.href);
+
+ defs = definitions;
+ seq = ++this.sequence;
+
+ return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'kanji-list.html');
+ })
+ .then((content) => {
+ this.definitions = defs;
+ this.popup.setContent(content, defs);
+
+ return bgCanAddDefinitions(defs, ['kanji']);
+ })
+ .then((states) => {
+ if (states !== null) {
+ states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
}
- );
- });
+ });
}
static textSourceFromPoint(point) {
diff --git a/ext/fg/js/source-range.js b/ext/fg/js/source-range.js
index 879b949c..a25b3fdc 100644
--- a/ext/fg/js/source-range.js
+++ b/ext/fg/js/source-range.js
@@ -77,7 +77,7 @@ class TextSourceRange {
}
equals(other) {
- return other.rng && other.rng.compareBoundaryPoints(Range.START_TO_START, this.rng) == 0;
+ return other.rng && other.rng.compareBoundaryPoints(Range.START_TO_START, this.rng) === 0;
}
static seekForward(node, length) {