aboutsummaryrefslogtreecommitdiff
path: root/ext/js/display
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/display')
-rw-r--r--ext/js/display/display-content-manager.js57
1 files changed, 45 insertions, 12 deletions
diff --git a/ext/js/display/display-content-manager.js b/ext/js/display/display-content-manager.js
index a0dd7dd6..0818f915 100644
--- a/ext/js/display/display-content-manager.js
+++ b/ext/js/display/display-content-manager.js
@@ -19,26 +19,46 @@
* StringUtil
*/
+/**
+ * A callback used when a media file has been loaded.
+ * @callback DisplayContentManager.OnLoadCallback
+ * @param {string} url The URL of the media that was loaded.
+ */
+
+/**
+ * A callback used when a media file should be unloaded.
+ * @callback DisplayContentManager.OnUnloadCallback
+ * @param {boolean} fullyLoaded Whether or not the media was fully loaded.
+ */
+
+/**
+ * The content manager which is used when generating HTML display content.
+ */
class DisplayContentManager {
+ /**
+ * Creates a new instance of the class.
+ */
constructor() {
this._token = {};
this._mediaCache = new Map();
this._loadMediaData = [];
}
- async loadMedia(path, dictionary, onLoad, onUnload) {
- const token = this._token;
- const data = {onUnload, loaded: false};
-
- this._loadMediaData.push(data);
-
- const media = await this.getMedia(path, dictionary);
- if (token !== this._token) { return; }
-
- onLoad(media.url);
- data.loaded = true;
+ /**
+ * Attempts to load the media file from a given dictionary.
+ * @param {string} path The path to the media file in the dictionary.
+ * @param {string} dictionary The name of the dictionary.
+ * @param {DisplayContentManager.OnLoadCallback} onLoad The callback that is executed if the media was loaded successfully.
+ * No assumptions should be made about the synchronicity of this callback.
+ * @param {DisplayContentManager.OnUnloadCallback} onUnload The callback that is executed when the media should be unloaded.
+ */
+ loadMedia(path, dictionary, onLoad, onUnload) {
+ this._loadMedia(path, dictionary, onLoad, onUnload);
}
+ /**
+ * Unloads all media that has been loaded.
+ */
unloadAll() {
for (const {onUnload, loaded} of this._loadMediaData) {
if (typeof onUnload === 'function') {
@@ -59,7 +79,20 @@ class DisplayContentManager {
this._token = {};
}
- async getMedia(path, dictionary) {
+ async _loadMedia(path, dictionary, onLoad, onUnload) {
+ const token = this._token;
+ const data = {onUnload, loaded: false};
+
+ this._loadMediaData.push(data);
+
+ const media = await this._getMedia(path, dictionary);
+ if (token !== this._token) { return; }
+
+ onLoad(media.url);
+ data.loaded = true;
+ }
+
+ async _getMedia(path, dictionary) {
let cachedData;
let dictionaryCache = this._mediaCache.get(dictionary);
if (typeof dictionaryCache !== 'undefined') {