From c8ac0d45bfb94deabd933117ba55d365f43fb1c5 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 14:08:39 -0500 Subject: Updates --- .../templates/__mocks__/template-renderer-proxy.js | 53 ++++++++-------------- .../anki-template-renderer-content-manager.js | 6 +-- ext/js/templates/sandbox/anki-template-renderer.js | 8 ++-- .../sandbox/template-renderer-frame-api.js | 4 +- ext/js/templates/sandbox/template-renderer.js | 8 ++-- 5 files changed, 31 insertions(+), 48 deletions(-) (limited to 'ext') diff --git a/ext/js/templates/__mocks__/template-renderer-proxy.js b/ext/js/templates/__mocks__/template-renderer-proxy.js index bcacf13b..8823e8f3 100644 --- a/ext/js/templates/__mocks__/template-renderer-proxy.js +++ b/ext/js/templates/__mocks__/template-renderer-proxy.js @@ -20,20 +20,35 @@ import {AnkiTemplateRenderer} from '../sandbox/anki-template-renderer.js'; export class TemplateRendererProxy { constructor() { + /** @type {?Promise} */ this._preparePromise = null; + /** @type {AnkiTemplateRenderer} */ this._ankiTemplateRenderer = new AnkiTemplateRenderer(); } + /** + * @param {string} template + * @param {import('template-renderer').PartialOrCompositeRenderData} data + * @param {import('anki-templates').RenderMode} type + * @returns {Promise} + */ async render(template, data, type) { await this._prepare(); - return await this._ankiTemplateRenderer.templateRenderer.render(template, data, type); + return this._ankiTemplateRenderer.templateRenderer.render(template, data, type); } + /** + * @param {import('template-renderer').RenderMultiItem[]} items + * @returns {Promise[]>} + */ async renderMulti(items) { await this._prepare(); - return await this._serializeMulti(this._ankiTemplateRenderer.templateRenderer.renderMulti(items)); + return this._ankiTemplateRenderer.templateRenderer.renderMulti(items); } + /** + * @returns {Promise} + */ _prepare() { if (this._preparePromise === null) { this._preparePromise = this._prepareInternal(); @@ -41,40 +56,8 @@ export class TemplateRendererProxy { return this._preparePromise; } + /** */ async _prepareInternal() { await this._ankiTemplateRenderer.prepare(); } - - _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; - } } diff --git a/ext/js/templates/sandbox/anki-template-renderer-content-manager.js b/ext/js/templates/sandbox/anki-template-renderer-content-manager.js index be80c211..4989ced3 100644 --- a/ext/js/templates/sandbox/anki-template-renderer-content-manager.js +++ b/ext/js/templates/sandbox/anki-template-renderer-content-manager.js @@ -22,12 +22,12 @@ export class AnkiTemplateRendererContentManager { /** * Creates a new instance of the class. - * @param {TemplateRendererMediaProvider} mediaProvider The media provider for the object. + * @param {import('./template-renderer-media-provider.js').TemplateRendererMediaProvider} mediaProvider The media provider for the object. * @param {import('anki-templates').NoteData} data The data object passed to the Handlebars template renderer. - * See {@link AnkiNoteDataCreator.create}'s return value for structure information. + * See AnkiNoteDataCreator.create's return value for structure information. */ constructor(mediaProvider, data) { - /** @type {TemplateRendererMediaProvider} */ + /** @type {import('./template-renderer-media-provider.js').TemplateRendererMediaProvider} */ this._mediaProvider = mediaProvider; /** @type {import('anki-templates').NoteData} */ this._data = data; diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index ce8e3200..9f4bf6ff 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -151,8 +151,8 @@ export class AnkiTemplateRenderer { } /** - * - * @param text + * @param {string} text + * @returns {string} */ _safeString(text) { return new Handlebars.SafeString(text); @@ -221,8 +221,8 @@ export class AnkiTemplateRenderer { } /** - * - * @param string + * @param {string} string + * @returns {string} */ _stringToMultiLineHtml(string) { return string.split('\n').join('
'); diff --git a/ext/js/templates/sandbox/template-renderer-frame-api.js b/ext/js/templates/sandbox/template-renderer-frame-api.js index 91400ab8..94ebf7fe 100644 --- a/ext/js/templates/sandbox/template-renderer-frame-api.js +++ b/ext/js/templates/sandbox/template-renderer-frame-api.js @@ -20,10 +20,10 @@ import {ExtensionError} from '../../core/extension-error.js'; export class TemplateRendererFrameApi { /** - * @param {TemplateRenderer} templateRenderer + * @param {import('./template-renderer.js').TemplateRenderer} templateRenderer */ constructor(templateRenderer) { - /** @type {TemplateRenderer} */ + /** @type {import('./template-renderer.js').TemplateRenderer} */ this._templateRenderer = templateRenderer; /** @type {import('core').MessageHandlerMap} */ this._windowMessageHandlers = new Map(/** @type {import('core').MessageHandlerArray} */ ([ diff --git a/ext/js/templates/sandbox/template-renderer.js b/ext/js/templates/sandbox/template-renderer.js index 716e3ccc..fe240b5f 100644 --- a/ext/js/templates/sandbox/template-renderer.js +++ b/ext/js/templates/sandbox/template-renderer.js @@ -21,7 +21,7 @@ import {ExtensionError} from '../../core/extension-error.js'; export class TemplateRenderer { constructor() { - /** @type {Map>} */ + /** @type {Map>} */ this._cache = new Map(); /** @type {number} */ this._cacheMaxSize = 5; @@ -110,14 +110,14 @@ export class TemplateRenderer { /** * @param {string} template - * @returns {HandlebarsTemplateDelegate} + * @returns {import('handlebars').TemplateDelegate} */ _getTemplateInstance(template) { const cache = this._cache; let instance = cache.get(template); if (typeof instance === 'undefined') { this._updateCacheSize(this._cacheMaxSize - 1); - instance = /** @type {HandlebarsTemplateDelegate} */ (Handlebars.compileAST(template)); + instance = /** @type {import('handlebars').TemplateDelegate} */ (Handlebars.compileAST(template)); cache.set(template, instance); } @@ -125,7 +125,7 @@ export class TemplateRenderer { } /** - * @param {HandlebarsTemplateDelegate} instance + * @param {import('handlebars').TemplateDelegate} instance * @param {import('anki-templates').NoteData} data * @returns {import('template-renderer').RenderResult} */ -- cgit v1.2.3