diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-03-13 11:25:10 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-13 11:25:10 -0500 |
commit | a00ed41838da08f5f81813443c0471a1d6be1291 (patch) | |
tree | 39d308b3fd744e70e33d2877a1d7a1b7dd2de4ff /ext/js/language/dictionary-database.js | |
parent | 638e5783acc20a58ec4de258848e44d4470bc6cf (diff) |
Database API updates (#1520)
* Add Database.findFirst
* Add DictionaryDatabase.findTagMetaBulk
Diffstat (limited to 'ext/js/language/dictionary-database.js')
-rw-r--r-- | ext/js/language/dictionary-database.js | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/ext/js/language/dictionary-database.js b/ext/js/language/dictionary-database.js index 62c22289..40f5362b 100644 --- a/ext/js/language/dictionary-database.js +++ b/ext/js/language/dictionary-database.js @@ -294,9 +294,14 @@ class DictionaryDatabase { return this._findGenericBulk('kanjiMeta', 'character', kanjiList, dictionaries, this._createKanjiMeta.bind(this)); } + findTagMetaBulk(items) { + const predicate = (row, item) => (row.dictionary === item.dictionary); + return this._findFirstBulk('tagMeta', 'name', items, predicate, this._createTagMeta.bind(this)); + } + findTagForTitle(name, title) { const query = IDBKeyRange.only(name); - return this._db.find('tagMeta', 'name', query, (row) => (row.dictionary === title), null); + return this._db.find('tagMeta', 'name', query, (row) => (row.dictionary === title), null, null); } getMedia(targets) { @@ -393,7 +398,7 @@ class DictionaryDatabase { async dictionaryExists(title) { const query = IDBKeyRange.only(title); - const result = await this._db.find('dictionaries', 'title', query); + const result = await this._db.find('dictionaries', 'title', query, null, null, void 0); return typeof result !== 'undefined'; } @@ -437,6 +442,36 @@ class DictionaryDatabase { }); } + _findFirstBulk(objectStoreName, indexName, items, predicate, createResult) { + return new Promise((resolve, reject) => { + const count = items.length; + const results = new Array(count); + if (count === 0) { + resolve(results); + return; + } + + const transaction = this._db.transaction([objectStoreName], 'readonly'); + const terms = transaction.objectStore(objectStoreName); + const index = terms.index(indexName); + + let completeCount = 0; + for (let i = 0; i < count; ++i) { + const itemIndex = i; + const item = items[i]; + const query = IDBKeyRange.only(item.query); + + const onFind = (row) => { + results[itemIndex] = createResult(row, itemIndex); + if (++completeCount >= count) { + resolve(results); + } + }; + this._db.findFirst(index, query, onFind, reject, predicate, item, void 0); + } + }); + } + _createTerm(row, index) { return { index, @@ -466,6 +501,10 @@ class DictionaryDatabase { }; } + _createTagMeta(row, index) { + return {row, index}; + } + _createTermMeta({expression, mode, data, dictionary}, index) { return {expression, mode, data, dictionary, index}; } |