diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-04-19 11:36:05 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-19 11:36:05 -0400 |
commit | a6344f635de2464465317b2ceeb6e19aef056848 (patch) | |
tree | 3c101c3dbf38dfb26bfb74bb0a38daedc8166811 /ext/bg/js/database.js | |
parent | 3b2663ba0957c65be959ba18dc80e13625e28f02 (diff) | |
parent | 99c1a6a6bc0ce55eb2f20703a7f7f69c4bcefd9d (diff) |
Merge pull request #446 from toasted-nutbread/dictionary-images
Dictionary images
Diffstat (limited to 'ext/bg/js/database.js')
-rw-r--r-- | ext/bg/js/database.js | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 260c815a..16612403 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -33,7 +33,7 @@ class Database { } try { - this.db = await Database._open('dict', 5, (db, transaction, oldVersion) => { + this.db = await Database._open('dict', 6, (db, transaction, oldVersion) => { Database._upgrade(db, transaction, oldVersion, [ { version: 2, @@ -90,6 +90,15 @@ class Database { indices: ['dictionary', 'expression', 'reading', 'sequence', 'expressionReverse', 'readingReverse'] } } + }, + { + version: 6, + stores: { + media: { + primaryKey: {keyPath: 'id', autoIncrement: true}, + indices: ['dictionary', 'path'] + } + } } ]); }); @@ -268,6 +277,34 @@ class Database { return result; } + async getMedia(targets) { + this._validate(); + + const count = targets.length; + const promises = []; + const results = new Array(count).fill(null); + const createResult = Database._createMedia; + const processRow = (row, [index, dictionaryName]) => { + if (row.dictionary === dictionaryName) { + results[index] = createResult(row, index); + } + }; + + const transaction = this.db.transaction(['media'], 'readonly'); + const objectStore = transaction.objectStore('media'); + const index = objectStore.index('path'); + + for (let i = 0; i < count; ++i) { + const {path, dictionaryName} = targets[i]; + const only = IDBKeyRange.only(path); + promises.push(Database._getAll(index, only, [i, dictionaryName], processRow)); + } + + await Promise.all(promises); + + return results; + } + async getDictionaryInfo() { this._validate(); @@ -432,6 +469,10 @@ class Database { return {character, mode, data, dictionary, index}; } + static _createMedia(row, index) { + return Object.assign({}, row, {index}); + } + static _getAll(dbIndex, query, context, processRow) { const fn = typeof dbIndex.getAll === 'function' ? Database._getAllFast : Database._getAllUsingCursor; return fn(dbIndex, query, context, processRow); |