WIP
This commit is contained in:
parent
58357552f8
commit
aab3803786
@ -141,13 +141,64 @@ class Database {
|
|||||||
return this.db.dictionaries.toArray();
|
return this.db.dictionaries.toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteDictionary(title) {
|
deleteDictionary(title, callback) {
|
||||||
if (this.db === null) {
|
if (this.db === null) {
|
||||||
return Promise.reject('database not initialized');
|
return Promise.reject('database not initialized');
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.db.terms.where('dictionary').equals(title).delete().then(() => {
|
this.db.dictionaries.where('title').equals(title).first(info => {
|
||||||
return this.db.kanji.where('dictionary').equals(title).delete();
|
if (!info) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let termCounter = Promise.resolve(0);
|
||||||
|
if (info.hasTerms) {
|
||||||
|
termCounter = this.db.terms.where('dictionary').equals(title).count();
|
||||||
|
}
|
||||||
|
|
||||||
|
let kanjiCounter = Promise.resolve(0);
|
||||||
|
if (info.hasKanji) {
|
||||||
|
kanjiCounter = this.db.kanji.where('dictionary').equals(title).count();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.all([termCounter, kanjiCounter]).then(([termCount, kanjiCount]) => {
|
||||||
|
const totalCount = termCount + kanjiCount;
|
||||||
|
let deletedCount = 0;
|
||||||
|
|
||||||
|
let termDeleter = Promise.resolve();
|
||||||
|
if (info.hasTerms) {
|
||||||
|
termDeleter = () => {
|
||||||
|
this.db.terms.where('dictionary').equals(title).limit(1000).delete(count => {
|
||||||
|
if (count > 0) {
|
||||||
|
return termDeleter();
|
||||||
|
} else {
|
||||||
|
deletedCount += count;
|
||||||
|
if (callback) {
|
||||||
|
callback(deletedCount / totalCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let kanjiDeleter = Promise.resolve();
|
||||||
|
if (info.hasKanji) {
|
||||||
|
kanjiDeleter = () => {
|
||||||
|
this.db.terms.where('dictionary').equals(title).limit(1000).delete(count => {
|
||||||
|
if (count > 0) {
|
||||||
|
return kanjiDeleter();
|
||||||
|
} else {
|
||||||
|
deletedCount += count;
|
||||||
|
if (callback) {
|
||||||
|
callback(deletedCount / totalCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.all([termDeleter, kanjiDeleter]);
|
||||||
|
});
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
return this.db.entities.where('dictionary').equals(title).delete();
|
return this.db.entities.where('dictionary').equals(title).delete();
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
|
Loading…
Reference in New Issue
Block a user