From ac396af399e75631d09bd51dabfef6ba9850dd86 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 26 Dec 2023 16:31:28 -0500 Subject: [PATCH] Improve types --- ext/js/app/frontend.js | 6 +++--- ext/js/background/backend.js | 10 ++++------ ext/js/comm/frame-client.js | 8 ++++---- types/ext/extension.d.ts | 13 +------------ 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js index b03a0629be..b093ec33fe 100644 --- a/ext/js/app/frontend.js +++ b/ext/js/app/frontend.js @@ -844,11 +844,11 @@ export class Frontend { } chrome.runtime.onMessage.removeListener(onMessage); }; - /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ + /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ const onMessage = (message, _sender, sendResponse) => { try { - const {action, params} = message; - if (action === 'frontendReady' && /** @type {import('application').ApiParams<'frontendReady'>} */ (params).frameId === frameId) { + const {action} = message; + if (action === 'frontendReady' && message.params.frameId === frameId) { cleanup(); resolve(); sendResponse(); diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index a29a54a3f8..4820e02922 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -1807,14 +1807,14 @@ export class Backend { return new Promise((resolve, reject) => { /** @type {?import('core').Timeout} */ let timer = null; - /** @type {?import('extension').ChromeRuntimeOnMessageCallback} */ + /** @type {?import('extension').ChromeRuntimeOnMessageCallback} */ let onMessage = (message, sender) => { if ( !sender.tab || sender.tab.id !== tabId || sender.frameId !== frameId || !(typeof message === 'object' && message !== null) || - /** @type {import('application').ApiMessageAny} */ (message).action !== 'applicationReady' + message.action !== 'applicationReady' ) { return; } @@ -1903,9 +1903,8 @@ export class Backend { } /** - * @template {import('application').ApiNames} TName * @param {number} tabId - * @param {import('application').ApiMessage} message + * @param {import('application').ApiMessageAny} message * @param {chrome.tabs.MessageSendOptions} options */ _sendMessageTabIgnoreResponse(tabId, message, options) { @@ -1914,8 +1913,7 @@ export class Backend { } /** - * @template {import('application').ApiNames} TName - * @param {import('application').ApiMessage} message + * @param {import('application').ApiMessageAny} message */ _sendMessageAllTabsIgnoreResponse(message) { const callback = () => this._checkLastError(chrome.runtime.lastError); diff --git a/ext/js/comm/frame-client.js b/ext/js/comm/frame-client.js index 5e997622fc..cb591ca995 100644 --- a/ext/js/comm/frame-client.js +++ b/ext/js/comm/frame-client.js @@ -110,14 +110,14 @@ export class FrameClient { contentWindow.postMessage({action, params}, targetOrigin); }; - /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ + /** @type {import('extension').ChromeRuntimeOnMessageCallback} */ const onMessage = (message) => { onMessageInner(message); return false; }; /** - * @param {import('extension').ChromeRuntimeMessageWithFrameId} message + * @param {import('application').ApiMessageAny} message */ const onMessageInner = async (message) => { try { @@ -130,7 +130,7 @@ export class FrameClient { switch (action) { case 'frameEndpointReady': { - const {secret} = /** @type {import('frame-client').FrameEndpointReadyDetails} */ (params); + const {secret} = params; const token = generateId(16); tokenMap.set(secret, token); postMessage('frameEndpointConnect', {secret, token, hostFrameId}); @@ -138,7 +138,7 @@ export class FrameClient { break; case 'frameEndpointConnected': { - const {secret, token} = /** @type {import('frame-client').FrameEndpointConnectedDetails} */ (params); + const {secret, token} = params; const frameId = message.frameId; const token2 = tokenMap.get(secret); if (typeof token2 !== 'undefined' && token === token2 && typeof frameId === 'number') { diff --git a/types/ext/extension.d.ts b/types/ext/extension.d.ts index 9c399640cd..5a24456669 100644 --- a/types/ext/extension.d.ts +++ b/types/ext/extension.d.ts @@ -56,18 +56,7 @@ export type ContentOrigin = { frameId?: number; }; -// TODO : Refactor -export type ChromeRuntimeMessage = { - action: string; - params?: Core.SerializableObject; -}; - -// TODO : Refactor -export type ChromeRuntimeMessageWithFrameId = ChromeRuntimeMessage & { - frameId?: number; -}; - -export type ChromeRuntimeOnMessageCallback = ( +export type ChromeRuntimeOnMessageCallback = ( message: TMessage, sender: chrome.runtime.MessageSender, sendResponse: ChromeRuntimeMessageSendResponseFunction,