summaryrefslogtreecommitdiff
path: root/ext/js/templates/sandbox/template-renderer-frame-api.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-11-27 12:48:14 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-11-27 12:48:14 -0500
commit4da4827bcbcdd1ef163f635d9b29416ff272b0bb (patch)
treea8a0f1a8befdb78a554e1be91f2c6059ca3ad5f9 /ext/js/templates/sandbox/template-renderer-frame-api.js
parentfd6bba8a2a869eaf2b2c1fa49001f933fce3c618 (diff)
Add JSDoc type annotations to project (rebased)
Diffstat (limited to 'ext/js/templates/sandbox/template-renderer-frame-api.js')
-rw-r--r--ext/js/templates/sandbox/template-renderer-frame-api.js86
1 files changed, 47 insertions, 39 deletions
diff --git a/ext/js/templates/sandbox/template-renderer-frame-api.js b/ext/js/templates/sandbox/template-renderer-frame-api.js
index 7ce2d909..31ba4500 100644
--- a/ext/js/templates/sandbox/template-renderer-frame-api.js
+++ b/ext/js/templates/sandbox/template-renderer-frame-api.js
@@ -17,15 +17,23 @@
*/
export class TemplateRendererFrameApi {
+ /**
+ * @param {TemplateRenderer} templateRenderer
+ */
constructor(templateRenderer) {
+ /** @type {TemplateRenderer} */
this._templateRenderer = templateRenderer;
- this._windowMessageHandlers = new Map([
+ /** @type {import('core').MessageHandlerMap} */
+ this._windowMessageHandlers = new Map(/** @type {import('core').MessageHandlerArray} */ ([
['render', {async: false, handler: this._onRender.bind(this)}],
['renderMulti', {async: false, handler: this._onRenderMulti.bind(this)}],
['getModifiedData', {async: false, handler: this._onGetModifiedData.bind(this)}]
- ]);
+ ]));
}
+ /**
+ * @returns {void}
+ */
prepare() {
window.addEventListener('message', this._onWindowMessage.bind(this), false);
this._postMessage(window.parent, 'ready', {}, null);
@@ -33,14 +41,24 @@ export class TemplateRendererFrameApi {
// Private
+ /**
+ * @param {MessageEvent<import('template-renderer-frame-api').MessageData>} e
+ */
_onWindowMessage(e) {
const {source, data: {action, params, id}} = e;
const messageHandler = this._windowMessageHandlers.get(action);
if (typeof messageHandler === 'undefined') { return; }
- this._onWindowMessageInner(messageHandler, action, params, source, id);
+ this._onWindowMessageInner(messageHandler, action, params, /** @type {Window} */ (source), id);
}
+ /**
+ * @param {import('core').MessageHandlerDetails} handlerItem
+ * @param {string} action
+ * @param {import('core').SerializableObject} params
+ * @param {Window} source
+ * @param {?string} id
+ */
async _onWindowMessageInner({handler, async}, action, params, source, id) {
let response;
try {
@@ -50,64 +68,54 @@ export class TemplateRendererFrameApi {
}
response = {result};
} catch (error) {
- response = {error: this._serializeError(error)};
+ response = {error: ExtensionError.serialize(error)};
}
if (typeof id === 'undefined') { return; }
this._postMessage(source, `${action}.response`, response, id);
}
+ /**
+ * @param {{template: string, data: import('template-renderer').PartialOrCompositeRenderData, type: import('anki-templates').RenderMode}} event
+ * @returns {import('template-renderer').RenderResult}
+ */
_onRender({template, data, type}) {
return this._templateRenderer.render(template, data, type);
}
+ /**
+ * @param {{items: import('template-renderer').RenderMultiItem[]}} event
+ * @returns {import('core').Response<import('template-renderer').RenderResult>[]}
+ */
_onRenderMulti({items}) {
- return this._serializeMulti(this._templateRenderer.renderMulti(items));
+ return this._templateRenderer.renderMulti(items);
}
+ /**
+ * @param {{data: import('template-renderer').CompositeRenderData, type: import('anki-templates').RenderMode}} event
+ * @returns {import('anki-templates').NoteData}
+ */
_onGetModifiedData({data, type}) {
const result = this._templateRenderer.getModifiedData(data, type);
return this._clone(result);
}
- _serializeError(error) {
- try {
- if (typeof error === 'object' && error !== null) {
- const result = {
- name: error.name,
- message: error.message,
- stack: error.stack
- };
- if (Object.prototype.hasOwnProperty.call(error, 'data')) {
- result.data = error.data;
- }
- return result;
- }
- } catch (e) {
- // NOP
- }
- return {
- value: error,
- hasValue: true
- };
- }
-
- _serializeMulti(array) {
- for (let i = 0, ii = array.length; i < ii; ++i) {
- const value = array[i];
- const {error} = value;
- if (typeof error !== 'undefined') {
- value.error = this._serializeError(error);
- }
- }
- return array;
- }
-
+ /**
+ * @template [T=unknown]
+ * @param {T} value
+ * @returns {T}
+ */
_clone(value) {
return JSON.parse(JSON.stringify(value));
}
+ /**
+ * @param {Window} target
+ * @param {string} action
+ * @param {import('core').SerializableObject} params
+ * @param {?string} id
+ */
_postMessage(target, action, params, id) {
- return target.postMessage({action, params, id}, '*');
+ target.postMessage(/** @type {import('template-renderer-frame-api').MessageData} */ ({action, params, id}), '*');
}
}