webext-messenger 0.15.0-1 → 0.15.0-2

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,20 +26,26 @@ function manageConnection(type, options, sendMessage) {
26
26
  });
27
27
  }
28
28
  async function manageMessage(type, sendMessage) {
29
- const response = await pRetry(sendMessage, {
29
+ const response = await pRetry(async () => {
30
+ const response = await sendMessage();
31
+ if (!isMessengerResponse(response)) {
32
+ throw new MessengerError(`No handler registered for ${type} in the receiving end`);
33
+ }
34
+ return response;
35
+ }, {
30
36
  minTimeout: 100,
31
37
  factor: 1.3,
32
38
  maxRetryTime: 4000,
33
39
  onFailedAttempt(error) {
34
- if (!String(error.message).startsWith(errorNonExistingTarget)) {
40
+ if (error instanceof MessengerError ||
41
+ String(error.message).startsWith(errorNonExistingTarget)) {
42
+ debug(type, "will retry. Attempt", error.attemptNumber);
43
+ }
44
+ else {
35
45
  throw error;
36
46
  }
37
- debug(type, "will retry. Attempt", error.attemptNumber);
38
47
  },
39
48
  });
40
- if (!isMessengerResponse(response)) {
41
- throw new MessengerError(`No handler registered for ${type} in the receiving end`);
42
- }
43
49
  if ("error" in response) {
44
50
  debug(type, "↘️ replied with error", response.error);
45
51
  throw deserializeError(response.error);
@@ -1,4 +1,11 @@
1
+ import { JsonObject } from "type-fest";
1
2
  import { Method } from "./types.js";
3
+ declare type ErrorObject = {
4
+ name?: string;
5
+ stack?: string;
6
+ message?: string;
7
+ code?: string;
8
+ } & JsonObject;
2
9
  export declare const __webextMessenger = true;
3
10
  export declare function isObject(value: unknown): value is Record<string, unknown>;
4
11
  export declare class MessengerError extends Error {
@@ -7,3 +14,6 @@ export declare class MessengerError extends Error {
7
14
  export declare const handlers: Map<string, Method>;
8
15
  export declare const debug: (...args: any[]) => void;
9
16
  export declare const warn: (...args: any[]) => void;
17
+ export declare function isErrorObject(error: unknown): error is ErrorObject;
18
+ export declare function delay(milliseconds: number): Promise<void>;
19
+ export {};
@@ -17,3 +17,13 @@ export const handlers = new Map();
17
17
  // .bind preserves the call location in the console
18
18
  export const debug = console.debug.bind(console, "Messenger:");
19
19
  export const warn = console.warn.bind(console, "Messenger:");
20
+ export function isErrorObject(error) {
21
+ var _a;
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is a type guard function and it uses ?.
23
+ return typeof ((_a = error) === null || _a === void 0 ? void 0 : _a.message) === "string";
24
+ }
25
+ export async function delay(milliseconds) {
26
+ return new Promise((resolve) => {
27
+ setTimeout(resolve, milliseconds);
28
+ });
29
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.15.0-1",
3
+ "version": "0.15.0-2",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",