aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-12-22 20:23:51 -0500
committerGitHub <noreply@github.com>2023-12-23 01:23:51 +0000
commitcb4499fd8ab0322fa3ab706adfb46caf21c57eec (patch)
tree26e9947475de836465ef77cb9e62be9014098bb7
parent33886cf39bd8c128296834a6825992f8b8341fe7 (diff)
Update offscreen to use invokeApiMapHandler (#427)
* Update offscreen to use invokeApiMapHandler * Simplify init
-rw-r--r--ext/js/background/offscreen.js21
-rw-r--r--types/ext/offscreen.d.ts2
2 files changed, 10 insertions, 13 deletions
diff --git a/ext/js/background/offscreen.js b/ext/js/background/offscreen.js
index 1cab5929..7e3576ba 100644
--- a/ext/js/background/offscreen.js
+++ b/ext/js/background/offscreen.js
@@ -18,8 +18,7 @@
import * as wanakana from '../../lib/wanakana.js';
import {ClipboardReader} from '../comm/clipboard-reader.js';
-import {invokeMessageHandler} from '../core.js';
-import {createApiMap, getApiMapHandler} from '../core/api-map.js';
+import {createApiMap, invokeApiMapHandler} from '../core/api-map.js';
import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js';
import {DictionaryDatabase} from '../language/dictionary-database.js';
import {JapaneseUtil} from '../language/sandbox/japanese-util.js';
@@ -53,8 +52,8 @@ export class Offscreen {
/* eslint-disable no-multi-spaces */
- /** @type {import('offscreen').OffscreenApiMapInit} */
- const messageHandlersInit = [
+ /** @type {import('offscreen').OffscreenApiMap} */
+ this._apiMap = createApiMap([
['clipboardGetTextOffscreen', this._getTextHandler.bind(this)],
['clipboardGetImageOffscreen', this._getImageHandler.bind(this)],
['clipboardSetBrowserOffscreen', this._setClipboardBrowser.bind(this)],
@@ -67,10 +66,8 @@ export class Offscreen {
['findTermsOffscreen', this._findTermsHandler.bind(this)],
['getTermFrequenciesOffscreen', this._getTermFrequenciesHandler.bind(this)],
['clearDatabaseCachesOffscreen', this._clearDatabaseCachesHandler.bind(this)]
- ];
-
- /** @type {import('offscreen').OffscreenApiMap} */
- this._messageHandlers = createApiMap(messageHandlersInit);
+ ]);
+ /* eslint-enable no-multi-spaces */
/** @type {?Promise<void>} */
this._prepareDatabasePromise = null;
@@ -174,10 +171,8 @@ export class Offscreen {
this._translator.clearDatabaseCaches();
}
- /** @type {import('extension').ChromeRuntimeOnMessageCallback} */
- _onMessage({action, params}, sender, callback) {
- const messageHandler = getApiMapHandler(this._messageHandlers, action);
- if (typeof messageHandler === 'undefined') { return false; }
- return invokeMessageHandler(messageHandler, params, callback, sender);
+ /** @type {import('extension').ChromeRuntimeOnMessageCallback<import('offscreen').MessageAny>} */
+ _onMessage({action, params}, _sender, callback) {
+ return invokeApiMapHandler(this._apiMap, action, params, [], callback);
}
}
diff --git a/types/ext/offscreen.d.ts b/types/ext/offscreen.d.ts
index d67733bb..2046bfe0 100644
--- a/types/ext/offscreen.d.ts
+++ b/types/ext/offscreen.d.ts
@@ -131,3 +131,5 @@ export type OffscreenApiHandler<TName extends MessageType> = ApiHandler<Offscree
export type OffscreenApiParams<TName extends MessageType> = ApiParams<OffscreenApiSurface[TName]>;
export type OffscreenApiReturn<TName extends MessageType> = ApiReturn<OffscreenApiSurface[TName]>;
+
+export type MessageAny = Message<MessageType>;