summaryrefslogtreecommitdiff
path: root/ext/bg/js/database.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-11-12 19:34:02 -0800
committerAlex Yatskov <alex@foosoft.net>2016-11-12 19:34:02 -0800
commit371c07ab1fb6125c065589d71c93477e0456aada (patch)
tree7260b408b9e1f9247f92b4ac9cb467fce054ba82 /ext/bg/js/database.js
parentb5cc47a9d1d7df7ec5a596fb72fad21f79d2e01c (diff)
Revert "Delete dictionary deletion"
This reverts commit 0ff41d5843b3bf39994ae32e4ab623bfe0e4e3ca.
Diffstat (limited to 'ext/bg/js/database.js')
-rw-r--r--ext/bg/js/database.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index 7f4c6ac6..06786881 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -148,6 +148,79 @@ class Database {
return this.db.dictionaries.toArray();
}
+ deleteDictionary(title, callback) {
+ if (this.db === null) {
+ return Promise.reject('database not initialized');
+ }
+
+ return 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) {
+ const termDeleterFunc = () => {
+ return this.db.terms.where('dictionary').equals(title).limit(1000).delete().then(count => {
+ if (count === 0) {
+ return Promise.resolve();
+ }
+
+ deletedCount += count;
+ if (callback) {
+ callback(totalCount, deletedCount);
+ }
+
+ return termDeleterFunc();
+ });
+ };
+
+ termDeleter = termDeleterFunc();
+ }
+
+ let kanjiDeleter = Promise.resolve();
+ if (info.hasKanji) {
+ const kanjiDeleterFunc = () => {
+ return this.db.kanji.where('dictionary').equals(title).limit(1000).delete().then(count => {
+ if (count === 0) {
+ return Promise.resolve();
+ }
+
+ deletedCount += count;
+ if (callback) {
+ callback(totalCount, deletedCount);
+ }
+
+ return kanjiDeleterFunc();
+ });
+ };
+
+ kanjiDeleter = kanjiDeleterFunc();
+ }
+
+ return Promise.all([termDeleter, kanjiDeleter]);
+ });
+ }).then(() => {
+ return this.db.entities.where('dictionary').equals(title).delete();
+ }).then(() => {
+ return this.db.dictionaries.where('title').equals(title).delete();
+ });
+ }
+
importDictionary(indexUrl, callback) {
if (this.db === null) {
return Promise.reject('database not initialized');