Add API for getting media data
This commit is contained in:
parent
8106f4744b
commit
fd6ea0e404
@ -111,7 +111,8 @@ class Backend {
|
|||||||
['getAnkiModelFieldNames', {handler: this._onApiGetAnkiModelFieldNames.bind(this), async: true}],
|
['getAnkiModelFieldNames', {handler: this._onApiGetAnkiModelFieldNames.bind(this), async: true}],
|
||||||
['getDictionaryInfo', {handler: this._onApiGetDictionaryInfo.bind(this), async: true}],
|
['getDictionaryInfo', {handler: this._onApiGetDictionaryInfo.bind(this), async: true}],
|
||||||
['getDictionaryCounts', {handler: this._onApiGetDictionaryCounts.bind(this), async: true}],
|
['getDictionaryCounts', {handler: this._onApiGetDictionaryCounts.bind(this), async: true}],
|
||||||
['purgeDatabase', {handler: this._onApiPurgeDatabase.bind(this), async: true}]
|
['purgeDatabase', {handler: this._onApiPurgeDatabase.bind(this), async: true}],
|
||||||
|
['getMedia', {handler: this._onApiGetMedia.bind(this), async: true}]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
this._commandHandlers = new Map([
|
this._commandHandlers = new Map([
|
||||||
@ -762,6 +763,10 @@ class Backend {
|
|||||||
return await this.translator.purgeDatabase();
|
return await this.translator.purgeDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _onApiGetMedia({targets}) {
|
||||||
|
return await this.database.getMedia(targets);
|
||||||
|
}
|
||||||
|
|
||||||
// Command handlers
|
// Command handlers
|
||||||
|
|
||||||
async _onCommandSearch(params) {
|
async _onCommandSearch(params) {
|
||||||
|
@ -277,6 +277,34 @@ class Database {
|
|||||||
return result;
|
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() {
|
async getDictionaryInfo() {
|
||||||
this._validate();
|
this._validate();
|
||||||
|
|
||||||
@ -441,6 +469,10 @@ class Database {
|
|||||||
return {character, mode, data, dictionary, index};
|
return {character, mode, data, dictionary, index};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static _createMedia(row, index) {
|
||||||
|
return Object.assign({}, row, {index});
|
||||||
|
}
|
||||||
|
|
||||||
static _getAll(dbIndex, query, context, processRow) {
|
static _getAll(dbIndex, query, context, processRow) {
|
||||||
const fn = typeof dbIndex.getAll === 'function' ? Database._getAllFast : Database._getAllUsingCursor;
|
const fn = typeof dbIndex.getAll === 'function' ? Database._getAllFast : Database._getAllUsingCursor;
|
||||||
return fn(dbIndex, query, context, processRow);
|
return fn(dbIndex, query, context, processRow);
|
||||||
|
@ -140,6 +140,10 @@ function apiPurgeDatabase() {
|
|||||||
return _apiInvoke('purgeDatabase');
|
return _apiInvoke('purgeDatabase');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function apiGetMedia(targets) {
|
||||||
|
return _apiInvoke('getMedia', {targets});
|
||||||
|
}
|
||||||
|
|
||||||
function _apiInvoke(action, params={}) {
|
function _apiInvoke(action, params={}) {
|
||||||
const data = {action, params};
|
const data = {action, params};
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user