summaryrefslogtreecommitdiff
path: root/ext/mixed/js/media-loader.js
blob: 5974e31a15eeba40a4645306ded3cb9a4e71dd34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2020-2021  Yomichan Authors
 *
 * 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
 * api
 */

class MediaLoader {
    constructor() {
        this._token = {};
        this._mediaCache = new Map();
        this._loadMediaData = [];
    }

    async loadMedia(path, dictionaryName, onLoad, onUnload) {
        const token = this._token;
        const data = {onUnload, loaded: false};

        this._loadMediaData.push(data);

        const media = await this.getMedia(path, dictionaryName);
        if (token !== this._token) { return; }

        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, dictionaryName) {
        let cachedData;
        let dictionaryCache = this._mediaCache.get(dictionaryName);
        if (typeof dictionaryCache !== 'undefined') {
            cachedData = dictionaryCache.get(path);
        } else {
            dictionaryCache = new Map();
            this._mediaCache.set(dictionaryName, dictionaryCache);
        }

        if (typeof cachedData === 'undefined') {
            cachedData = {
                promise: null,
                data: null,
                url: null
            };
            dictionaryCache.set(path, cachedData);
            cachedData.promise = this._getMediaData(path, dictionaryName, cachedData);
        }

        return cachedData.promise;
    }

    async _getMediaData(path, dictionaryName, cachedData) {
        const token = this._token;
        const data = (await api.getMedia([{path, dictionaryName}]))[0];
        if (token === this._token && data !== null) {
            const contentArrayBuffer = this._base64ToArrayBuffer(data.content);
            const blob = new Blob([contentArrayBuffer], {type: data.mediaType});
            const url = URL.createObjectURL(blob);
            cachedData.data = data;
            cachedData.url = url;
        }
        return cachedData;
    }

    _base64ToArrayBuffer(content) {
        const binaryContent = window.atob(content);
        const length = binaryContent.length;
        const array = new Uint8Array(length);
        for (let i = 0; i < length; ++i) {
            array[i] = binaryContent.charCodeAt(i);
        }
        return array.buffer;
    }
}