aboutsummaryrefslogtreecommitdiff
path: root/ext/js/display/display-content-manager.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2022-03-14 22:32:08 -0400
committerGitHub <noreply@github.com>2022-03-14 22:32:08 -0400
commit8aa060337cea2bb8fce7864d509d07df4688f1c2 (patch)
tree7bd73b2328c84cd537f010a5bb6d7ddb269e2545 /ext/js/display/display-content-manager.js
parent4e15f923297599869e9d695940c9c56d81b0307d (diff)
AnkiTemplateRendererContentManager (#2088)
* Make function private * Update comments * Refactor the content manager that is provided to StructuredContentGenerator in AnkiTemplateRenderer * Update function to not be async, for API parity
Diffstat (limited to 'ext/js/display/display-content-manager.js')
-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') {