Fix progress counting

This commit is contained in:
Alex Yatskov 2016-09-13 22:43:16 -07:00
parent 17366e521a
commit ff3896ed01
3 changed files with 20 additions and 9 deletions

View File

@ -163,7 +163,7 @@ class Dictionary {
return this.db.terms.bulkAdd(rows).then(() => { return this.db.terms.bulkAdd(rows).then(() => {
if (callback) { if (callback) {
callback(i, index.banks); callback(indexUrl, i, index.banks);
} }
}); });
}); });
@ -199,7 +199,7 @@ class Dictionary {
return this.db.kanji.bulkAdd(rows).then(() => { return this.db.kanji.bulkAdd(rows).then(() => {
if (callback) { if (callback) {
callback(i, index.banks); callback(indexUrl, i, index.banks);
} }
}); });
}); });

View File

@ -45,14 +45,21 @@ class Translator {
callback({state: 'begin', progress: 0}); callback({state: 'begin', progress: 0});
} }
let banksLoaded = 0; const banks = {};
let banksTotal = 0; const bankCallback = (indexUrl, loaded, total) => {
banks[indexUrl] = {loaded: loaded, total: total};
if (Object.keys(banks).length !== 3) {
return;
}
const bankCallback = (loaded, total) => { let banksLoaded = 0;
banksLoaded += loaded; let banksTotal = 0;
banksTotal += total; for (const url in banks) {
banksLoaded += banks[url].loaded;
banksTotal += banks[url].total;
}
if (callback) { if (callback && banksTotal > 0) {
callback({state: 'update', progress: Math.ceil(100 * banksLoaded / banksTotal)}); callback({state: 'update', progress: Math.ceil(100 * banksLoaded / banksTotal)});
} }
}; };

View File

@ -47,6 +47,10 @@ class Yomichan {
} }
} }
onImport({state, progress}) {
console.log(`${state}: ${progress}`);
}
onMessage(request, sender, callback) { onMessage(request, sender, callback) {
const {action, params} = request, method = this['api_' + action]; const {action, params} = request, method = this['api_' + action];
@ -90,7 +94,7 @@ class Yomichan {
break; break;
case 'loading': case 'loading':
chrome.browserAction.setBadgeText({text: '...'}); chrome.browserAction.setBadgeText({text: '...'});
this.translator.loadData().then(() => this.setState('enabled')); this.translator.loadData(this.onImport.bind(this)).then(() => this.setState('enabled'));
break; break;
} }