diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2023-12-28 00:48:33 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-28 05:48:33 +0000 |
commit | 76805bc0fc65452ca830623aa810888f9c476a2b (patch) | |
tree | d91e257fd335c75dfca1a37784eb12769fbb5a66 /ext/js/app/frontend.js | |
parent | fc2123a45b3ceacc2ec887d24e5e752dca59bb4f (diff) |
API type safety updates (#457)
* Update message handlers in SearchDisplayController
* Update types
* Updates
* Updates
* Simplify
* Updates
* Updates
* Rename
* Improve types
* Improve types
* Resolve TODOs
Diffstat (limited to 'ext/js/app/frontend.js')
-rw-r--r-- | ext/js/app/frontend.js | 53 |
1 files changed, 22 insertions, 31 deletions
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 <https://www.gnu.org/licenses/>. */ -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<import('core').TokenString>} - */ + /** @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<boolean>} - */ + /** @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<import('application').ApiMessageAny>} */ + _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<import('application').ApiMessageAny>} */ 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}}); }); } |