aboutsummaryrefslogtreecommitdiff
path: root/types
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-12-20 00:18:55 -0500
committerGitHub <noreply@github.com>2023-12-20 05:18:55 +0000
commit3c226215419ca815712e9568f7d871a96f5ff1cf (patch)
tree8bfe561a0d36589860b64c1758c2e29e92e7e3f4 /types
parente0e29dc1aa0965b3e0fb97de64a27c2b695e068b (diff)
Simplify message handlers (#396)
Diffstat (limited to 'types')
-rw-r--r--types/ext/backend.d.ts8
-rw-r--r--types/ext/core.d.ts26
-rw-r--r--types/ext/offscreen.d.ts6
3 files changed, 11 insertions, 29 deletions
diff --git a/types/ext/backend.d.ts b/types/ext/backend.d.ts
index 08cbf4b0..6cbe7938 100644
--- a/types/ext/backend.d.ts
+++ b/types/ext/backend.d.ts
@@ -18,14 +18,6 @@
import type * as Api from './api';
import type * as Core from './core';
-export type MessageHandlerDetails = {
- async: boolean;
- contentScript: boolean;
- handler: (params: Core.SerializableObject | undefined, sender: chrome.runtime.MessageSender) => unknown;
-};
-export type MessageHandlerMap = Map<string, MessageHandlerDetails>;
-export type MessageHandlerMapInit = [key: string, handlerDetails: MessageHandlerDetails][];
-
export type MessageHandlerWithProgressDetails = {
async: boolean;
contentScript: boolean;
diff --git a/types/ext/core.d.ts b/types/ext/core.d.ts
index d1a4ef8f..c71a8ec4 100644
--- a/types/ext/core.d.ts
+++ b/types/ext/core.d.ts
@@ -75,30 +75,16 @@ export type ResponseError = {
export type Response<T = unknown> = ResponseSuccess<T> | ResponseError;
export type MessageHandler = (params: SafeAny, ...extraArgs: SafeAny[]) => (
- SafeAny |
- Promise<SafeAny> |
- MessageHandlerAsyncResult
+ MessageHandlerResult |
+ Promise<MessageHandlerResult>
);
-export type MessageHandlerAsyncResult = {
- async: boolean;
- result: SafeAny | Promise<SafeAny>;
-};
+export type MessageHandlerResult = SafeAny;
-export type MessageHandlerDetails = {
- /**
- * Whether or not the handler is async or not. Values include `false`, `true`, or `'dynamic'`.
- * When the value is `'dynamic'`, the handler should return an object of the format `{async: boolean, result: any}`.
- */
- async: boolean | 'dynamic';
- /**
- * A handler function which is passed `params` and `...extraArgs` as arguments.
- */
- handler: MessageHandler;
-};
+export type MessageHandlerMap = Map<string, MessageHandler>;
-export type MessageHandlerMap = Map<string, MessageHandlerDetails>;
+export type MessageHandlerMapInit = MessageHandlerMapInitItem[];
-export type MessageHandlerArray = [key: string, handlerDetails: MessageHandlerDetails][];
+export type MessageHandlerMapInitItem = [key: string, handlerDetails: MessageHandler];
export type Timeout = number | NodeJS.Timeout;
diff --git a/types/ext/offscreen.d.ts b/types/ext/offscreen.d.ts
index 85bce1ff..c741ac99 100644
--- a/types/ext/offscreen.d.ts
+++ b/types/ext/offscreen.d.ts
@@ -110,4 +110,8 @@ export type MessageHandler<
details: MessageDetailsMap[TMessage],
) => (TIsAsync extends true ? Promise<MessageReturn<TMessage>> : MessageReturn<TMessage>);
-export type MessageHandlerMap<T = MessageType> = Map<T, Core.MessageHandlerDetails>;
+export type MessageHandlerMap = Map<MessageType, Core.MessageHandler>;
+
+export type MessageHandlerMapInit = MessageHandlerMapInitItem[];
+
+export type MessageHandlerMapInitItem = [messageType: MessageType, handler: Core.MessageHandler];