Working with IndexDb

This commit is contained in:
Alex Yatskov 2016-09-11 12:29:18 -07:00
parent 1ac14cd633
commit d5ea03171e
4 changed files with 43 additions and 36 deletions

View File

@ -26,14 +26,14 @@ class Deinflection {
}
validate(validator) {
return validator(this.term).then(tagSets => {
for (const tags of tagSets) {
return validator(this.term).then(sets => {
for (const tags of sets) {
if (this.tags.length === 0) {
return true;
}
for (const tag of this.tags) {
if (tags.indexOf(tag) !== -1) {
if (tags.includes(tag)) {
return true;
}
}
@ -55,7 +55,7 @@ class Deinflection {
for (const variant of rules[rule]) {
let allowed = this.tags.length === 0;
for (const tag of this.tags) {
if (variant.ti.indexOf(tag) !== -1) {
if (variant.ti.includes(tag)) {
allowed = true;
break;
}
@ -115,8 +115,6 @@ class Deinflector {
deinflect(term, validator) {
const node = new Deinflection(term);
return node.deinflect(validator, this.rules).then(success => {
return success ? node.gather() : [];
});
return node.deinflect(validator, this.rules).then(success => success ? node.gather() : []);
}
}

View File

@ -59,15 +59,15 @@ class Dictionary {
findKanji(kanji) {
const results = [];
return this.db.kanji.where('c').equals(kanji).each(row => {
return this.db.kanji.where('character').equals(kanji).each(row => {
results.push({
character: row.c,
onyomi: row.o.split(' '),
kunyomi: row.k.split(' '),
tags: row.t.split(' '),
glossary: row.m
character: row.character,
onyomi: row.onyomi.split(' '),
kunyomi: row.kunyomi.split(' '),
tags: row.tags.split(' '),
glossary: row.meanings
});
});
}).then(() => results);
}
getEntities(tags) {

View File

@ -55,17 +55,23 @@ class Translator {
}
findTermGroups(text) {
const groups = {};
const deinflectGroups = {};
const deinflectPromises = [];
for (let i = text.length; i > 0; --i) {
deinflectPromises.push(
this.deinflector.deinflect(text.slice(0, i), term => {
return this.dictionary.findTerm(term).then(definitions => definitions.map(definition => definition.tags));
}).then(inflects => {
}).then(deinflects => {
const processPromises = [];
for (const inflect of inflects) {
processPromises.push(this.processTerm(groups, inflect.source, inflect.tags, inflect.rules, inflect.root));
for (const deinflect of deinflects) {
processPromises.push(this.processTerm(
deinflectGroups,
deinflect.source,
deinflect.tags,
deinflect.rules,
deinflect.root
));
}
return Promise.all(processPromises);
@ -73,14 +79,14 @@ class Translator {
);
}
return Promise.all(deinflectPromises).then(() => groups);
return Promise.all(deinflectPromises).then(() => deinflectGroups);
}
findTerm(text) {
return this.findTermGroups(text).then(groups => {
return this.findTermGroups(text).then(deinflectGroups => {
let definitions = [];
for (const key in groups) {
definitions.push(groups[key]);
for (const key in deinflectGroups) {
definitions.push(deinflectGroups[key]);
}
definitions = definitions.sort((v1, v2) => {
@ -121,17 +127,20 @@ class Translator {
}
findKanji(text) {
let definitions = [];
const processed = {};
const promises = [];
for (const c of text) {
if (!processed[c]) {
definitions = definitions.concat(this.dictionary.findKanji(c));
promises.push(this.dictionary.findKanji(c).then((definitions) => definitions));
processed[c] = true;
}
}
return this.processKanji(definitions);
return Promise.all(promises).then((sets) => {
const definitions = sets.reduce((a, b) => a.concat(b));
return this.processKanji(definitions);
});
}
processTerm(groups, source, tags, rules, root) {
@ -143,7 +152,7 @@ class Translator {
let matched = tags.length === 0;
for (const tag of tags) {
if (definition.tags.indexOf(tag) !== -1) {
if (definition.tags.includes(tag)) {
matched = true;
break;
}

View File

@ -41,10 +41,10 @@ class Yomichan {
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
chrome.tabs.onCreated.addListener((tab) => this.onTabReady(tab.id));
chrome.tabs.onCreated.addListener(tab => this.onTabReady(tab.id));
chrome.tabs.onUpdated.addListener(this.onTabReady.bind(this));
loadOptions((opts) => {
loadOptions(opts => {
this.setOptions(opts);
if (this.options.activateOnStartup) {
this.setState('loading');
@ -118,7 +118,7 @@ class Yomichan {
}
tabInvokeAll(action, params) {
chrome.tabs.query({}, (tabs) => {
chrome.tabs.query({}, tabs => {
for (const tab of tabs) {
this.tabInvoke(tab.id, action, params);
}
@ -133,7 +133,7 @@ class Yomichan {
if (this.ankiConnectVer === this.getApiVersion()) {
this.ankiInvoke(action, params, pool, callback);
} else {
this.api_getVersion({callback: (version) => {
this.api_getVersion({callback: version => {
if (version === this.getApiVersion()) {
this.ankiConnectVer = version;
this.ankiInvoke(action, params, pool, callback);
@ -209,7 +209,7 @@ class Yomichan {
break;
case 'tags':
if (definition.tags) {
value = definition.tags.map((t) => t.name);
value = definition.tags.map(t => t.name);
}
break;
}
@ -244,7 +244,7 @@ class Yomichan {
};
for (const name in fields) {
if (fields[name].indexOf('{audio}') !== -1) {
if (fields[name].includes('{audio}')) {
audio.fields.push(name);
}
}
@ -274,7 +274,7 @@ class Yomichan {
}
}
this.ankiInvokeSafe('canAddNotes', {notes}, 'notes', (results) => {
this.ankiInvokeSafe('canAddNotes', {notes}, 'notes', results => {
const states = [];
if (results !== null) {
@ -293,11 +293,11 @@ class Yomichan {
}
api_findKanji({text, callback}) {
callback(this.translator.findKanji(text));
this.translator.findKanji(text).then(result => callback(result));
}
api_findTerm({text, callback}) {
this.translator.findTerm(text).then((result) => callback(result));
this.translator.findTerm(text).then(result => callback(result));
}
api_getDeckNames({callback}) {