move zip import to async

This commit is contained in:
Alex Yatskov 2017-07-10 14:53:06 -07:00
parent 49352c5fa1
commit f694026827
2 changed files with 47 additions and 65 deletions

View File

@ -224,7 +224,53 @@ class Database {
await this.db.kanji.bulkAdd(rows);
};
await zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded);
await Database.importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded);
return summary;
}
static async importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded) {
const files = (await JSZip.loadAsync(archive)).files;
const indexFile = files['index.json'];
if (!indexFile) {
throw 'no dictionary index found in archive';
}
const index = JSON.parse(await indexFile.async('string'));
if (!index.title || !index.version || !index.revision) {
throw 'unrecognized dictionary format';
}
await indexLoaded(
index.title,
index.version,
index.revision,
index.tagMeta || {},
index.termBanks > 0,
index.kanjiBanks > 0
);
const banksTotal = index.termBanks + index.kanjiBanks;
let banksLoaded = 0;
for (let i = 1; i <= index.termBanks; ++i) {
const bankFile = files[`term_bank_${i}.json`];
if (bankFile) {
const bank = JSON.parse(await bankFile.async('string'));
await termsLoaded(index.title, bank, banksTotal, banksLoaded++);
} else {
throw 'missing term bank file';
}
}
for (let i = 1; i <= index.kanjiBanks; ++i) {
const bankFile = files[`kanji_bank_${i}.json`];
if (bankFile) {
const bank = JSON.parse(await bankFile.async('string'));
await kanjiLoaded(index.title, bank, banksTotal, banksLoaded++);
} else {
throw 'missing kanji bank file';
}
}
}
}

View File

@ -455,70 +455,6 @@ function jsonLoadInt(url) {
return jsonLoad(chrome.extension.getURL(url));
}
/*
* Zip
*/
function zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded) {
return JSZip.loadAsync(archive).then(files => files.files).then(files => {
const indexFile = files['index.json'];
if (!indexFile) {
return Promise.reject('no dictionary index found in archive');
}
return indexFile.async('string').then(indexJson => {
const index = JSON.parse(indexJson);
if (!index.title || !index.version || !index.revision) {
return Promise.reject('unrecognized dictionary format');
}
return indexLoaded(
index.title,
index.version,
index.revision,
index.tagMeta || {},
index.termBanks > 0,
index.kanjiBanks > 0
).then(() => index);
}).then(index => {
const loaders = [];
const banksTotal = index.termBanks + index.kanjiBanks;
let banksLoaded = 0;
for (let i = 1; i <= index.termBanks; ++i) {
const bankFile = files[`term_bank_${i}.json`];
if (!bankFile) {
return Promise.reject('missing term bank file');
}
loaders.push(() => bankFile.async('string').then(bankJson => {
const bank = JSON.parse(bankJson);
return termsLoaded(index.title, bank, banksTotal, banksLoaded++);
}));
}
for (let i = 1; i <= index.kanjiBanks; ++i) {
const bankFile = files[`kanji_bank_${i}.json`];
if (!bankFile) {
return Promise.reject('missing kanji bank file');
}
loaders.push(() => bankFile.async('string').then(bankJson => {
const bank = JSON.parse(bankJson);
return kanjiLoaded(index.title, bank, banksTotal, banksLoaded++);
}));
}
let chain = Promise.resolve();
for (const loader of loaders) {
chain = chain.then(loader);
}
return chain;
});
});
}
/*
* Helpers
*/