Progress callback for dictionary loading

This commit is contained in:
Alex Yatskov 2016-09-13 20:36:13 -07:00
parent 0e89d0e7e6
commit 17366e521a
2 changed files with 43 additions and 11 deletions

View File

@ -130,7 +130,7 @@ class Dictionary {
}); });
} }
importTermDict(indexUrl) { importTermDict(indexUrl, callback) {
if (this.db === null) { if (this.db === null) {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
@ -161,7 +161,11 @@ class Dictionary {
rows.push({expression, reading, tags, glossary}); rows.push({expression, reading, tags, glossary});
} }
return this.db.terms.bulkAdd(rows); return this.db.terms.bulkAdd(rows).then(() => {
if (callback) {
callback(i, index.banks);
}
});
}); });
}); });
} }
@ -176,7 +180,7 @@ class Dictionary {
}); });
} }
importKanjiDict(indexUrl) { importKanjiDict(indexUrl, callback) {
if (this.db === null) { if (this.db === null) {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
@ -193,7 +197,11 @@ class Dictionary {
rows.push({character, onyomi, kunyomi, tags, meanings}); rows.push({character, onyomi, kunyomi, tags, meanings});
} }
return this.db.kanji.bulkAdd(rows); return this.db.kanji.bulkAdd(rows).then(() => {
if (callback) {
callback(i, index.banks);
}
});
}); });
}); });
} }

View File

@ -25,7 +25,7 @@ class Translator {
this.deinflector = new Deinflector(); this.deinflector = new Deinflector();
} }
loadData() { loadData(callback) {
if (this.loaded) { if (this.loaded) {
return Promise.resolve(); return Promise.resolve();
} }
@ -37,13 +37,37 @@ class Translator {
this.tagMeta = tagMeta; this.tagMeta = tagMeta;
return this.dictionary.prepareDb(); return this.dictionary.prepareDb();
}).then(exists => { }).then(exists => {
if (!exists) { if (exists) {
return Promise.all([ return;
this.dictionary.importKanjiDict('bg/data/kanjidic/index.json'),
this.dictionary.importTermDict('bg/data/edict/index.json'),
this.dictionary.importTermDict('bg/data/enamdict/index.json')
]).then(() => this.dictionary.sealDb());
} }
if (callback) {
callback({state: 'begin', progress: 0});
}
let banksLoaded = 0;
let banksTotal = 0;
const bankCallback = (loaded, total) => {
banksLoaded += loaded;
banksTotal += total;
if (callback) {
callback({state: 'update', progress: Math.ceil(100 * banksLoaded / banksTotal)});
}
};
return Promise.all([
this.dictionary.importTermDict('bg/data/edict/index.json', bankCallback),
this.dictionary.importTermDict('bg/data/enamdict/index.json', bankCallback),
this.dictionary.importKanjiDict('bg/data/kanjidic/index.json', bankCallback),
]).then(() => {
return this.dictionary.sealDb();
}).then(() => {
if (callback) {
callback({state: 'end', progress: 100});
}
});
}).then(() => { }).then(() => {
this.loaded = true; this.loaded = true;
}); });