From 0094ff7babab6839ccef4c82fa46b8aa4f14198f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 27 Dec 2023 01:40:25 -0500 Subject: API map updates (#458) * Rename * Fix incorrect union types * Update type names * Fix types --- ext/js/background/backend.js | 4 ++-- ext/js/background/offscreen-proxy.js | 6 +++--- ext/js/background/offscreen.js | 28 ++++++++++++++-------------- ext/js/comm/api.js | 2 +- types/ext/api-map.d.ts | 4 ++-- types/ext/api.d.ts | 4 ++-- types/ext/offscreen.d.ts | 33 ++++++++++++++++++++------------- 7 files changed, 44 insertions(+), 37 deletions(-) diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index 765d17d9..5ef3c3be 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -370,7 +370,7 @@ export class Backend { }); } - /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ + /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ _onMessageWrapper(message, sender, sendResponse) { if (this._isPrepared) { return this._onMessage(message, sender, sendResponse); @@ -393,7 +393,7 @@ export class Backend { } /** - * @param {import('api').MessageAny} message + * @param {import('api').ApiMessageAny} message * @param {chrome.runtime.MessageSender} sender * @param {(response?: unknown) => void} callback * @returns {boolean} diff --git a/ext/js/background/offscreen-proxy.js b/ext/js/background/offscreen-proxy.js index 99dc0741..2335b673 100644 --- a/ext/js/background/offscreen-proxy.js +++ b/ext/js/background/offscreen-proxy.js @@ -72,9 +72,9 @@ export class OffscreenProxy { } /** - * @template {import('offscreen').MessageType} TMessageType - * @param {import('offscreen').Message} message - * @returns {Promise>} + * @template {import('offscreen').ApiNames} TMessageType + * @param {import('offscreen').ApiMessage} message + * @returns {Promise>} */ sendMessagePromise(message) { return new Promise((resolve, reject) => { diff --git a/ext/js/background/offscreen.js b/ext/js/background/offscreen.js index 05c655df..2320471c 100644 --- a/ext/js/background/offscreen.js +++ b/ext/js/background/offscreen.js @@ -52,7 +52,7 @@ export class Offscreen { /* eslint-disable no-multi-spaces */ - /** @type {import('offscreen').OffscreenApiMap} */ + /** @type {import('offscreen').ApiMap} */ this._apiMap = createApiMap([ ['clipboardGetTextOffscreen', this._getTextHandler.bind(this)], ['clipboardGetImageOffscreen', this._getImageHandler.bind(this)], @@ -78,22 +78,22 @@ export class Offscreen { chrome.runtime.onMessage.addListener(this._onMessage.bind(this)); } - /** @type {import('offscreen').OffscreenApiHandler<'clipboardGetTextOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'clipboardGetTextOffscreen'>} */ async _getTextHandler({useRichText}) { return await this._clipboardReader.getText(useRichText); } - /** @type {import('offscreen').OffscreenApiHandler<'clipboardGetImageOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'clipboardGetImageOffscreen'>} */ async _getImageHandler() { return await this._clipboardReader.getImage(); } - /** @type {import('offscreen').OffscreenApiHandler<'clipboardSetBrowserOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'clipboardSetBrowserOffscreen'>} */ _setClipboardBrowser({value}) { this._clipboardReader.browser = value; } - /** @type {import('offscreen').OffscreenApiHandler<'databasePrepareOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'databasePrepareOffscreen'>} */ _prepareDatabaseHandler() { if (this._prepareDatabasePromise !== null) { return this._prepareDatabasePromise; @@ -102,29 +102,29 @@ export class Offscreen { return this._prepareDatabasePromise; } - /** @type {import('offscreen').OffscreenApiHandler<'getDictionaryInfoOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'getDictionaryInfoOffscreen'>} */ async _getDictionaryInfoHandler() { return await this._dictionaryDatabase.getDictionaryInfo(); } - /** @type {import('offscreen').OffscreenApiHandler<'databasePurgeOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'databasePurgeOffscreen'>} */ async _purgeDatabaseHandler() { return await this._dictionaryDatabase.purge(); } - /** @type {import('offscreen').OffscreenApiHandler<'databaseGetMediaOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'databaseGetMediaOffscreen'>} */ async _getMediaHandler({targets}) { const media = await this._dictionaryDatabase.getMedia(targets); const serializedMedia = media.map((m) => ({...m, content: ArrayBufferUtil.arrayBufferToBase64(m.content)})); return serializedMedia; } - /** @type {import('offscreen').OffscreenApiHandler<'translatorPrepareOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'translatorPrepareOffscreen'>} */ _prepareTranslatorHandler({deinflectionReasons}) { this._translator.prepare(deinflectionReasons); } - /** @type {import('offscreen').OffscreenApiHandler<'findKanjiOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'findKanjiOffscreen'>} */ async _findKanjiHandler({text, options}) { /** @type {import('translation').FindKanjiOptions} */ const modifiedOptions = { @@ -134,7 +134,7 @@ export class Offscreen { return await this._translator.findKanji(text, modifiedOptions); } - /** @type {import('offscreen').OffscreenApiHandler<'findTermsOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'findTermsOffscreen'>} */ async _findTermsHandler({mode, text, options}) { const enabledDictionaryMap = new Map(options.enabledDictionaryMap); const excludeDictionaryDefinitions = ( @@ -161,17 +161,17 @@ export class Offscreen { return this._translator.findTerms(mode, text, modifiedOptions); } - /** @type {import('offscreen').OffscreenApiHandler<'getTermFrequenciesOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'getTermFrequenciesOffscreen'>} */ _getTermFrequenciesHandler({termReadingList, dictionaries}) { return this._translator.getTermFrequencies(termReadingList, dictionaries); } - /** @type {import('offscreen').OffscreenApiHandler<'clearDatabaseCachesOffscreen'>} */ + /** @type {import('offscreen').ApiHandler<'clearDatabaseCachesOffscreen'>} */ _clearDatabaseCachesHandler() { this._translator.clearDatabaseCaches(); } - /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ + /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ _onMessage({action, params}, _sender, callback) { return invokeApiMapHandler(this._apiMap, action, params, [], callback); } diff --git a/ext/js/comm/api.js b/ext/js/comm/api.js index c0cfae16..c2351538 100644 --- a/ext/js/comm/api.js +++ b/ext/js/comm/api.js @@ -373,7 +373,7 @@ export class API { * @returns {Promise>} */ _invoke(action, params) { - /** @type {import('api').MessageAny} */ + /** @type {import('api').ApiMessage} */ const data = {action, params}; return new Promise((resolve, reject) => { try { diff --git a/types/ext/api-map.d.ts b/types/ext/api-map.d.ts index 4a4eb87c..8c3215cf 100644 --- a/types/ext/api-map.d.ts +++ b/types/ext/api-map.d.ts @@ -109,7 +109,7 @@ export type ApiFunctionOrdered Promise>; /** Type alias for a union of all params types. */ -export type ApiParamsAny = ApiParams; +export type ApiParamsAny = {[name in ApiNames]: ApiParams}[ApiNames]; /** Type alias for a union of all return types. */ -export type ApiReturnAny = ApiReturn; +export type ApiReturnAny = {[name in ApiNames]: ApiReturn}[ApiNames]; diff --git a/types/ext/api.d.ts b/types/ext/api.d.ts index 2d23040c..ad3aa22c 100644 --- a/types/ext/api.d.ts +++ b/types/ext/api.d.ts @@ -399,9 +399,9 @@ export type ApiReturn = BaseApiReturn export type ApiParamsAny = BaseApiParamsAny; -export type MessageAny = Message; +export type ApiMessageAny = {[name in ApiNames]: ApiMessage}[ApiNames]; -type Message = { +type ApiMessage = { action: TName; params: ApiParams; }; diff --git a/types/ext/offscreen.d.ts b/types/ext/offscreen.d.ts index 511c32bd..9b1d844a 100644 --- a/types/ext/offscreen.d.ts +++ b/types/ext/offscreen.d.ts @@ -22,9 +22,16 @@ import type * as DictionaryImporter from './dictionary-importer'; import type * as Environment from './environment'; import type * as Translation from './translation'; import type * as Translator from './translator'; -import type {ApiMap, ApiMapInit, ApiHandler, ApiParams, ApiReturn, ApiNames} from './api-map'; +import type { + ApiMap as BaseApiMap, + ApiMapInit as BaseApiMapInit, + ApiHandler as BaseApiHandler, + ApiParams as BaseApiParams, + ApiReturn as BaseApiReturn, + ApiNames as BaseApiNames, +} from './api-map'; -type OffscreenApiSurface = { +type ApiSurface = { databasePrepareOffscreen: { params: void; return: void; @@ -93,13 +100,13 @@ type OffscreenApiSurface = { }; }; -export type Message = ( - OffscreenApiParams extends void ? - {action: TName} : - {action: TName, params: OffscreenApiParams} +export type ApiMessage = ( + ApiParams extends void ? + {action: TName, params?: never} : + {action: TName, params: ApiParams} ); -export type MessageType = ApiNames; +export type ApiNames = BaseApiNames; export type FindKanjiOptionsOffscreen = Omit & { enabledDictionaryMap: [ @@ -121,14 +128,14 @@ export type FindTermsTextReplacementOffscreen = Omit; +export type ApiMap = BaseApiMap; -export type OffscreenApiMapInit = ApiMapInit; +export type ApiMapInit = BaseApiMapInit; -export type OffscreenApiHandler = ApiHandler; +export type ApiHandler = BaseApiHandler; -export type OffscreenApiParams = ApiParams; +export type ApiParams = BaseApiParams; -export type OffscreenApiReturn = ApiReturn; +export type ApiReturn = BaseApiReturn; -export type MessageAny = Message; +export type ApiMessageAny = {[name in ApiNames]: ApiMessage}[ApiNames]; -- cgit v1.2.3