From 76805bc0fc65452ca830623aa810888f9c476a2b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 28 Dec 2023 00:48:33 -0500 Subject: API type safety updates (#457) * Update message handlers in SearchDisplayController * Update types * Updates * Updates * Simplify * Updates * Updates * Rename * Improve types * Improve types * Resolve TODOs --- ext/js/app/frontend.js | 53 +++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) (limited to 'ext/js/app/frontend.js') diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js index b68b55f3..b093ec33 100644 --- a/ext/js/app/frontend.js +++ b/ext/js/app/frontend.js @@ -16,7 +16,8 @@ * along with this program. If not, see . */ -import {EventListenerCollection, invokeMessageHandler, log, promiseAnimationFrame} from '../core.js'; +import {EventListenerCollection, log, promiseAnimationFrame} from '../core.js'; +import {createApiMap, invokeApiMapHandler} from '../core/api-map.js'; import {DocumentUtil} from '../dom/document-util.js'; import {TextSourceElement} from '../dom/text-source-element.js'; import {TextSourceRange} from '../dom/text-source-range.js'; @@ -106,12 +107,12 @@ export class Frontend { this._optionsContextOverride = null; /* eslint-disable no-multi-spaces */ - /** @type {import('core').MessageHandlerMap} */ - this._runtimeMessageHandlers = new Map(/** @type {import('core').MessageHandlerMapInit} */ ([ - ['Frontend.requestReadyBroadcast', this._onMessageRequestFrontendReadyBroadcast.bind(this)], - ['Frontend.setAllVisibleOverride', this._onApiSetAllVisibleOverride.bind(this)], - ['Frontend.clearAllVisibleOverride', this._onApiClearAllVisibleOverride.bind(this)] - ])); + /** @type {import('application').ApiMap} */ + this._runtimeApiMap = createApiMap([ + ['frontendRequestReadyBroadcast', this._onMessageRequestFrontendReadyBroadcast.bind(this)], + ['frontendSetAllVisibleOverride', this._onApiSetAllVisibleOverride.bind(this)], + ['frontendClearAllVisibleOverride', this._onApiClearAllVisibleOverride.bind(this)] + ]); this._hotkeyHandler.registerActions([ ['scanSelectedText', this._onActionScanSelectedText.bind(this)], @@ -239,9 +240,7 @@ export class Frontend { // Message handlers - /** - * @param {import('frontend').FrontendRequestReadyBroadcastParams} params - */ + /** @type {import('application').ApiHandler<'frontendRequestReadyBroadcast'>} */ _onMessageRequestFrontendReadyBroadcast({frameId}) { this._signalFrontendReady(frameId); } @@ -313,10 +312,7 @@ export class Frontend { }; } - /** - * @param {{value: boolean, priority: number, awaitFrame: boolean}} params - * @returns {Promise} - */ + /** @type {import('application').ApiHandler<'frontendSetAllVisibleOverride'>} */ async _onApiSetAllVisibleOverride({value, priority, awaitFrame}) { const result = await this._popupFactory.setAllVisibleOverride(value, priority); if (awaitFrame) { @@ -325,10 +321,7 @@ export class Frontend { return result; } - /** - * @param {{token: import('core').TokenString}} params - * @returns {Promise} - */ + /** @type {import('application').ApiHandler<'frontendClearAllVisibleOverride'>} */ async _onApiClearAllVisibleOverride({token}) { return await this._popupFactory.clearAllVisibleOverride(token); } @@ -342,11 +335,9 @@ export class Frontend { this._updatePopupPosition(); } - /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ - _onRuntimeMessage({action, params}, sender, callback) { - const messageHandler = this._runtimeMessageHandlers.get(action); - if (typeof messageHandler === 'undefined') { return false; } - return invokeMessageHandler(messageHandler, params, callback, sender); + /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ + _onRuntimeMessage({action, params}, _sender, callback) { + return invokeApiMapHandler(this._runtimeApiMap, action, params, [], callback); } /** @@ -827,12 +818,12 @@ export class Frontend { * @param {?number} targetFrameId */ _signalFrontendReady(targetFrameId) { - /** @type {import('frontend').FrontendReadyDetails} */ - const params = {frameId: this._frameId}; + /** @type {import('application').ApiMessageNoFrameId<'frontendReady'>} */ + const message = {action: 'frontendReady', params: {frameId: this._frameId}}; if (targetFrameId === null) { - yomitan.api.broadcastTab('frontendReady', params); + yomitan.api.broadcastTab(message); } else { - yomitan.api.sendMessageToFrame(targetFrameId, 'frontendReady', params); + yomitan.api.sendMessageToFrame(targetFrameId, message); } } @@ -853,11 +844,11 @@ export class Frontend { } chrome.runtime.onMessage.removeListener(onMessage); }; - /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ + /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ const onMessage = (message, _sender, sendResponse) => { try { - const {action, params} = message; - if (action === 'frontendReady' && /** @type {import('frontend').FrontendReadyDetails} */ (params).frameId === frameId) { + const {action} = message; + if (action === 'frontendReady' && message.params.frameId === frameId) { cleanup(); resolve(); sendResponse(); @@ -876,7 +867,7 @@ export class Frontend { } chrome.runtime.onMessage.addListener(onMessage); - yomitan.api.broadcastTab('Frontend.requestReadyBroadcast', {frameId: this._frameId}); + yomitan.api.broadcastTab({action: 'frontendRequestReadyBroadcast', params: {frameId: this._frameId}}); }); } -- cgit v1.2.3