Cleanup using promises

This commit is contained in:
Alex Yatskov 2016-08-09 21:23:05 -07:00
parent dcce58fc2e
commit e6d821b731
4 changed files with 71 additions and 64 deletions

View File

@ -65,7 +65,7 @@ class Translator {
const dfs = this.deinflector.deinflect(term, t => { const dfs = this.deinflector.deinflect(term, t => {
const tags = []; const tags = [];
for (let d of this.dictionary.findTerm(t)) { for (const d of this.dictionary.findTerm(t)) {
tags.push(d.tags); tags.push(d.tags);
} }
@ -76,13 +76,13 @@ class Translator {
continue; continue;
} }
for (let df of dfs) { for (const df of dfs) {
this.processTerm(groups, df.source, df.tags, df.rules, df.root); this.processTerm(groups, df.source, df.tags, df.rules, df.root);
} }
} }
let definitions = []; let definitions = [];
for (let key in groups) { for (const key in groups) {
definitions.push(groups[key]); definitions.push(groups[key]);
} }
@ -115,7 +115,7 @@ class Translator {
}); });
let length = 0; let length = 0;
for (let result of definitions) { for (const result of definitions) {
length = Math.max(length, result.source.length); length = Math.max(length, result.source.length);
} }
@ -126,7 +126,7 @@ class Translator {
let definitions = []; let definitions = [];
const processed = {}; const processed = {};
for (let c of text) { for (const c of text) {
if (!processed[c]) { if (!processed[c]) {
definitions = definitions.concat(this.dictionary.findKanji(c)); definitions = definitions.concat(this.dictionary.findKanji(c));
processed[c] = true; processed[c] = true;
@ -137,13 +137,13 @@ class Translator {
} }
processTerm(groups, dfSource, dfTags, dfRules=[], dfRoot='') { 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) { if (entry.id in groups) {
continue; continue;
} }
let matched = dfTags.length === 0; let matched = dfTags.length === 0;
for (let t of dfTags) { for (const t of dfTags) {
if (entry.tags.indexOf(t) !== -1) { if (entry.tags.indexOf(t) !== -1) {
matched = true; matched = true;
break; break;
@ -157,7 +157,7 @@ class Translator {
let popular = false; let popular = false;
let tags = []; 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}; const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: entry.entities[t] || '', name: t};
this.applyTagMeta(tag); this.applyTagMeta(tag);
tags.push(tag); tags.push(tag);
@ -186,9 +186,9 @@ class Translator {
processKanji(entries) { processKanji(entries) {
const processed = []; const processed = [];
for (let entry of entries) { for (const entry of entries) {
const tags = []; 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}; const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: '', name: t};
this.applyTagMeta(tag); this.applyTagMeta(tag);
tags.push(tag); tags.push(tag);

View File

@ -17,26 +17,26 @@
*/ */
function bgSendMessage(action, params, callback) { function bgSendMessage(action, params) {
chrome.runtime.sendMessage({action, params}, callback); return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve));
} }
function bgFindTerm(text, callback) { function bgFindTerm(text) {
bgSendMessage('findTerm', {text}, callback); return bgSendMessage('findTerm', {text});
} }
function bgFindKanji(text, callback) { function bgFindKanji(text) {
bgSendMessage('findKanji', {text}, callback); return bgSendMessage('findKanji', {text});
} }
function bgRenderText(data, template, callback) { function bgRenderText(data, template) {
bgSendMessage('renderText', {data, template}, callback); return bgSendMessage('renderText', {data, template});
} }
function bgCanAddDefinitions(definitions, modes, callback) { function bgCanAddDefinitions(definitions, modes) {
bgSendMessage('canAddDefinitions', {definitions, modes}, callback); return bgSendMessage('canAddDefinitions', {definitions, modes});
} }
function bgAddDefinition(definition, mode, callback) { function bgAddDefinition(definition, mode) {
bgSendMessage('addDefinition', {definition, mode}, callback); return bgSendMessage('addDefinition', {definition, mode});
} }

View File

@ -90,10 +90,16 @@ class Client {
} }
textSource.setEndOffset(this.options.scanLength); textSource.setEndOffset(this.options.scanLength);
bgFindTerm(textSource.text(), ({definitions, length}) => {
let defs = [];
let seq = -1;
bgFindTerm(textSource.text())
.then(({definitions, length}) => {
if (length === 0) { if (length === 0) {
this.hidePopup(); return Promise.reject();
} else { }
textSource.setEndOffset(length); textSource.setEndOffset(length);
const sentence = Client.extractSentence(textSource, this.options.sentenceExtent); const sentence = Client.extractSentence(textSource, this.options.sentenceExtent);
@ -102,23 +108,22 @@ class Client {
definition.sentence = sentence; definition.sentence = sentence;
}); });
const sequence = ++this.sequence; defs = definitions;
bgRenderText( seq = ++this.sequence;
{definitions, root: this.fgRoot, options: this.options, sequence},
'term-list.html', return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'term-list.html');
(content) => { })
this.definitions = definitions; .then((content) => {
this.definitions = defs;
this.showPopup(textSource, content); this.showPopup(textSource, content);
bgCanAddDefinitions(definitions, ['vocab_kanji', 'vocab_kana'], (states) => { return bgCanAddDefinitions(defs, ['vocab_kanji', 'vocab_kana']);
})
.then((states) => {
if (states !== null) { if (states !== null) {
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence})); states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
} }
}); }, () => this.hidePopup());
}
);
}
});
} }
showPopup(textSource, content) { showPopup(textSource, content) {
@ -156,7 +161,7 @@ class Client {
const state = {}; const state = {};
state[mode] = false; state[mode] = false;
bgAddDefinition(this.definitions[index], mode, (success) => { bgAddDefinition(this.definitions[index], mode).then((success) => {
if (success) { if (success) {
this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence}); this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence});
} else { } else {
@ -173,7 +178,7 @@ class Client {
url += `&kana=${encodeURIComponent(definition.reading)}`; url += `&kana=${encodeURIComponent(definition.reading)}`;
} }
for (let key in this.audio) { for (const key in this.audio) {
this.audio[key].pause(); this.audio[key].pause();
} }
@ -185,28 +190,30 @@ class Client {
} }
api_displayKanji(kanji) { api_displayKanji(kanji) {
bgFindKanji(kanji, (definitions) => { let defs = [];
definitions.forEach((definition) => { let seq = -1;
definition.url = window.location.href;
});
const sequence = ++this.sequence; bgFindKanji(kanji)
bgRenderText( .then((definitions) => {
{definitions, root: this.fgRoot, options: this.options, sequence}, definitions.forEach((definition) => definition.url = window.location.href);
'kanji-list.html',
(content) => {
this.definitions = definitions;
this.popup.setContent(content, definitions);
bgCanAddDefinitions(definitions, ['kanji'], (states) => { 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) { if (states !== null) {
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence})); states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
} }
}); });
} }
);
});
}
static textSourceFromPoint(point) { static textSourceFromPoint(point) {
const element = document.elementFromPoint(point.x, point.y); const element = document.elementFromPoint(point.x, point.y);

View File

@ -77,7 +77,7 @@ class TextSourceRange {
} }
equals(other) { 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) { static seekForward(node, length) {