diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-11-09 08:55:40 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-11-09 08:55:40 -0800 |
commit | aab3803786d369c5dbf9b26e9ff921c5984547d4 (patch) | |
tree | 6d0525dbcf082a09e2643af139b51e14fb3073fc /ext/bg/js/database.js | |
parent | 58357552f8fc63b0e19817d71df8d96d4bba090d (diff) |
WIP
Diffstat (limited to 'ext/bg/js/database.js')
-rw-r--r-- | ext/bg/js/database.js | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 50a6ecd7..1169d5ee 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -141,13 +141,64 @@ class Database { return this.db.dictionaries.toArray(); } - deleteDictionary(title) { + deleteDictionary(title, callback) { if (this.db === null) { return Promise.reject('database not initialized'); } - return this.db.terms.where('dictionary').equals(title).delete().then(() => { - return this.db.kanji.where('dictionary').equals(title).delete(); + this.db.dictionaries.where('title').equals(title).first(info => { + 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(() => { return this.db.entities.where('dictionary').equals(title).delete(); }).then(() => { |