summaryrefslogtreecommitdiff
path: root/ext/js/yomitan.js
diff options
context:
space:
mode:
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});
}