yomichan/ext/js/media/media-loader.js

97 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-04-11 18:23:49 +00:00
/*
* Copyright (C) 2020-2021 Yomichan Authors
2020-04-11 18:23:49 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* global
* MediaUtil
*/
2020-04-11 18:23:49 +00:00
class MediaLoader {
constructor() {
this._token = {};
this._mediaCache = new Map();
this._loadMediaData = [];
}
async loadMedia(path, dictionary, onLoad, onUnload) {
const token = this._token;
2020-04-11 18:23:49 +00:00
const data = {onUnload, loaded: false};
this._loadMediaData.push(data);
const media = await this.getMedia(path, dictionary);
if (token !== this._token) { return; }
2020-04-11 18:23:49 +00:00
onLoad(media.url);
data.loaded = true;
}
unloadAll() {
for (const {onUnload, loaded} of this._loadMediaData) {
if (typeof onUnload === 'function') {
onUnload(loaded);
}
}
this._loadMediaData = [];
for (const map of this._mediaCache.values()) {
for (const {url} of map.values()) {
if (url !== null) {
URL.revokeObjectURL(url);
}
}
}
this._mediaCache.clear();
this._token = {};
}
async getMedia(path, dictionary) {
2020-04-11 18:23:49 +00:00
let cachedData;
let dictionaryCache = this._mediaCache.get(dictionary);
2020-04-11 18:23:49 +00:00
if (typeof dictionaryCache !== 'undefined') {
cachedData = dictionaryCache.get(path);
} else {
dictionaryCache = new Map();
this._mediaCache.set(dictionary, dictionaryCache);
2020-04-11 18:23:49 +00:00
}
if (typeof cachedData === 'undefined') {
cachedData = {
promise: null,
data: null,
url: null
};
dictionaryCache.set(path, cachedData);
cachedData.promise = this._getMediaData(path, dictionary, cachedData);
2020-04-11 18:23:49 +00:00
}
return cachedData.promise;
}
async _getMediaData(path, dictionary, cachedData) {
2020-04-11 18:23:49 +00:00
const token = this._token;
const data = (await yomichan.api.getMedia([{path, dictionary}]))[0];
2020-04-11 18:23:49 +00:00
if (token === this._token && data !== null) {
const blob = MediaUtil.createBlobFromBase64Content(data.content, data.mediaType);
2020-04-11 18:23:49 +00:00
const url = URL.createObjectURL(blob);
cachedData.data = data;
cachedData.url = url;
}
return cachedData;
}
}