aboutsummaryrefslogtreecommitdiff
path: root/ext/js/yomitan.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-12-28 00:48:33 -0500
committerGitHub <noreply@github.com>2023-12-28 05:48:33 +0000
commit76805bc0fc65452ca830623aa810888f9c476a2b (patch)
treed91e257fd335c75dfca1a37784eb12769fbb5a66 /ext/js/yomitan.js
parentfc2123a45b3ceacc2ec887d24e5e752dca59bb4f (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/yomitan.js')
-rw-r--r--ext/js/yomitan.js56
1 files changed, 22 insertions, 34 deletions
diff --git a/ext/js/yomitan.js b/ext/js/yomitan.js
index 7505c0ca..621e9cf0 100644
--- a/ext/js/yomitan.js
+++ b/ext/js/yomitan.js
@@ -18,7 +18,8 @@
import {API} from './comm/api.js';
import {CrossFrameAPI} from './comm/cross-frame-api.js';
-import {EventDispatcher, deferPromise, invokeMessageHandler, log} from './core.js';
+import {EventDispatcher, deferPromise, log} from './core.js';
+import {createApiMap, invokeApiMapHandler} from './core/api-map.js';
import {ExtensionError} from './core/extension-error.js';
/**
@@ -95,15 +96,15 @@ export class Yomitan extends EventDispatcher {
this._isBackendReadyPromiseResolve = resolve;
/* eslint-disable no-multi-spaces */
- /** @type {import('core').MessageHandlerMap} */
- this._messageHandlers = new Map(/** @type {import('core').MessageHandlerMapInit} */ ([
- ['Yomitan.isReady', this._onMessageIsReady.bind(this)],
- ['Yomitan.backendReady', this._onMessageBackendReady.bind(this)],
- ['Yomitan.getUrl', this._onMessageGetUrl.bind(this)],
- ['Yomitan.optionsUpdated', this._onMessageOptionsUpdated.bind(this)],
- ['Yomitan.databaseUpdated', this._onMessageDatabaseUpdated.bind(this)],
- ['Yomitan.zoomChanged', this._onMessageZoomChanged.bind(this)]
- ]));
+ /** @type {import('application').ApiMap} */
+ this._apiMap = createApiMap([
+ ['applicationIsReady', this._onMessageIsReady.bind(this)],
+ ['applicationBackendReady', this._onMessageBackendReady.bind(this)],
+ ['applicationGetUrl', this._onMessageGetUrl.bind(this)],
+ ['applicationOptionsUpdated', this._onMessageOptionsUpdated.bind(this)],
+ ['applicationDatabaseUpdated', this._onMessageDatabaseUpdated.bind(this)],
+ ['applicationZoomChanged', this._onMessageZoomChanged.bind(this)]
+ ]);
/* eslint-enable no-multi-spaces */
}
@@ -171,7 +172,7 @@ export class Yomitan extends EventDispatcher {
*/
ready() {
this._isReady = true;
- this.sendMessage({action: 'yomitanReady'});
+ this.sendMessage({action: 'applicationReady'});
}
/**
@@ -183,6 +184,7 @@ export class Yomitan extends EventDispatcher {
return this._extensionUrlBase !== null && url.startsWith(this._extensionUrlBase);
}
+ // TODO : this function needs type safety
/**
* Runs `chrome.runtime.sendMessage()` with additional exception handling events.
* @param {import('extension').ChromeRuntimeSendMessageArgs} args The arguments to be passed to `chrome.runtime.sendMessage()`.
@@ -221,55 +223,41 @@ export class Yomitan extends EventDispatcher {
return location.href;
}
- /** @type {import('extension').ChromeRuntimeOnMessageCallback} */
- _onMessage({action, params}, sender, callback) {
- const messageHandler = this._messageHandlers.get(action);
- if (typeof messageHandler === 'undefined') { return false; }
- return invokeMessageHandler(messageHandler, params, callback, sender);
+ /** @type {import('extension').ChromeRuntimeOnMessageCallback<import('application').ApiMessageAny>} */
+ _onMessage({action, params}, _sender, callback) {
+ return invokeApiMapHandler(this._apiMap, action, params, [], callback);
}
- /**
- * @returns {boolean}
- */
+ /** @type {import('application').ApiHandler<'applicationIsReady'>} */
_onMessageIsReady() {
return this._isReady;
}
- /**
- * @returns {void}
- */
+ /** @type {import('application').ApiHandler<'applicationBackendReady'>} */
_onMessageBackendReady() {
if (this._isBackendReadyPromiseResolve === null) { return; }
this._isBackendReadyPromiseResolve();
this._isBackendReadyPromiseResolve = null;
}
- /**
- * @returns {{url: string}}
- */
+ /** @type {import('application').ApiHandler<'applicationGetUrl'>} */
_onMessageGetUrl() {
return {url: this._getUrl()};
}
- /**
- * @param {{source: string}} params
- */
+ /** @type {import('application').ApiHandler<'applicationOptionsUpdated'>} */
_onMessageOptionsUpdated({source}) {
if (source !== 'background') {
this.trigger('optionsUpdated', {source});
}
}
- /**
- * @param {{type: string, cause: string}} params
- */
+ /** @type {import('application').ApiHandler<'applicationDatabaseUpdated'>} */
_onMessageDatabaseUpdated({type, cause}) {
this.trigger('databaseUpdated', {type, cause});
}
- /**
- * @param {{oldZoomFactor: number, newZoomFactor: number}} params
- */
+ /** @type {import('application').ApiHandler<'applicationZoomChanged'>} */
_onMessageZoomChanged({oldZoomFactor, newZoomFactor}) {
this.trigger('zoomChanged', {oldZoomFactor, newZoomFactor});
}