Skip to content

Commit

Permalink
Improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
toasted-nutbread committed Dec 26, 2023
1 parent 2669af9 commit 6746e22
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
6 changes: 3 additions & 3 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,11 +845,11 @@ export class Frontend {
}
chrome.runtime.onMessage.removeListener(onMessage);
};
/** @type {import('extension').ChromeRuntimeOnMessageCallback} */
/** @type {import('extension').ChromeRuntimeOnMessageCallback<import('application').ApiMessageAny>} */
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();
Expand Down
10 changes: 4 additions & 6 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1819,14 +1819,14 @@ export class Backend {
return new Promise((resolve, reject) => {
/** @type {?import('core').Timeout} */
let timer = null;
/** @type {?import('extension').ChromeRuntimeOnMessageCallback} */
/** @type {?import('extension').ChromeRuntimeOnMessageCallback<import('application').ApiMessageAny>} */
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;
}
Expand Down Expand Up @@ -1915,9 +1915,8 @@ export class Backend {
}

/**
* @template {import('application').ApiNames} TName
* @param {number} tabId
* @param {import('application').ApiMessage<TName>} message
* @param {import('application').ApiMessageAny} message
* @param {chrome.tabs.MessageSendOptions} options
*/
_sendMessageTabIgnoreResponse(tabId, message, options) {
Expand All @@ -1926,8 +1925,7 @@ export class Backend {
}

/**
* @template {import('application').ApiNames} TName
* @param {import('application').ApiMessage<TName>} message
* @param {import('application').ApiMessageAny} message
*/
_sendMessageAllTabsIgnoreResponse(message) {
const callback = () => this._checkLastError(chrome.runtime.lastError);
Expand Down
8 changes: 4 additions & 4 deletions ext/js/comm/frame-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ export class FrameClient {
contentWindow.postMessage({action, params}, targetOrigin);
};

/** @type {import('extension').ChromeRuntimeOnMessageCallback<import('extension').ChromeRuntimeMessageWithFrameId>} */
/** @type {import('extension').ChromeRuntimeOnMessageCallback<import('application').ApiMessageAny>} */
const onMessage = (message) => {
onMessageInner(message);
return false;
};

/**
* @param {import('extension').ChromeRuntimeMessageWithFrameId} message
* @param {import('application').ApiMessageAny} message
*/
const onMessageInner = async (message) => {
try {
Expand All @@ -130,15 +130,15 @@ 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});
}
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') {
Expand Down
13 changes: 1 addition & 12 deletions types/ext/extension.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TMessage = ChromeRuntimeMessage> = (
export type ChromeRuntimeOnMessageCallback<TMessage = unknown> = (
message: TMessage,
sender: chrome.runtime.MessageSender,
sendResponse: ChromeRuntimeMessageSendResponseFunction,
Expand Down

0 comments on commit 6746e22

Please sign in to comment.