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

View File

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

View File

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

View File

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