Skip to content

Commit

Permalink
Dispatch events on retries (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jan 19, 2024
1 parent 2861446 commit c919a54
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions source/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const events = new EventTarget();
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export * from "./receiver.js";
export * from "./sender.js";
export * from "./types.js";
export * from "./events.js";
export { getThisFrame, getTopLevelFrame } from "./thisTarget.js";
export { toggleLogging } from "./logging.js";

Expand Down
19 changes: 19 additions & 0 deletions source/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isObject, MessengerError, __webextMessenger } from "./shared.js";
import { log } from "./logging.js";
import { type SetReturnType } from "type-fest";
import { handlers } from "./handlers.js";
import { events } from "./events.js";

const _errorNonExistingTarget =
"Could not establish connection. Receiving end does not exist.";
Expand Down Expand Up @@ -116,6 +117,18 @@ async function manageMessage(
factor: 1.3,
maxRetryTime: 4000,
async onFailedAttempt(error) {
events.dispatchEvent(
new CustomEvent("failed-attempt", {
detail: {
type,
seq,
target,
error,
attemptCount: error.attemptNumber,
},
})
);

if (error.message === _errorTargetClosedEarly) {
throw new Error(errorTargetClosedEarly);
}
Expand Down Expand Up @@ -158,6 +171,12 @@ async function manageMessage(
);
}

events.dispatchEvent(
new CustomEvent("attempts-exhausted", {
detail: { type, seq, target, error },
})
);

throw error;
});

Expand Down

0 comments on commit c919a54

Please sign in to comment.