summaryrefslogtreecommitdiff
path: root/ext/bg/js/database.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-11-09 09:21:11 -0800
committerAlex Yatskov <alex@foosoft.net>2016-11-09 09:21:11 -0800
commit8b5ee629311065bc53537d5684931dbf775c4e3e (patch)
tree0dfc8905cbc51c5747820e7f64244e3e41191fc7 /ext/bg/js/database.js
parentaab3803786d369c5dbf9b26e9ff921c5984547d4 (diff)
Better deletion
Diffstat (limited to 'ext/bg/js/database.js')
-rw-r--r--ext/bg/js/database.js46
1 files changed, 27 insertions, 19 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index 1169d5ee..e3a62886 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -146,7 +146,7 @@ class Database {
return Promise.reject('database not initialized');
}
- this.db.dictionaries.where('title').equals(title).first(info => {
+ return this.db.dictionaries.where('title').equals(title).first(info => {
if (!info) {
return;
}
@@ -167,34 +167,42 @@ class Database {
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);
- }
+ 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) {
- 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);
- }
+ 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]);