Skip to content

Commit

Permalink
Allow skipping retries (only via main messenger function) (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jan 19, 2024
1 parent 1319bac commit 1ebce7b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions source/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ function makeMessage(
// Do not turn this into an `async` function; Notifications must turn `void`
function manageConnection(
type: string,
{ seq, isNotification }: Options,
{ seq, isNotification, retry }: Options,
target: AnyTarget,
sendMessage: (attempt: number) => Promise<unknown>
): Promise<unknown> | void {
if (!isNotification) {
return manageMessage(type, target, seq!, sendMessage);
return manageMessage(type, target, seq!, retry ?? true, sendMessage);
}

void sendMessage(1).catch((error: unknown) => {
Expand All @@ -77,8 +77,10 @@ async function manageMessage(
type: string,
target: AnyTarget,
seq: number,
retry: boolean,
sendMessage: (attempt: number) => Promise<unknown>
): Promise<unknown> {
// TODO: Split this up a bit because it's too long. Probably drop p-retry

Check warning on line 83 in source/sender.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Split this up a bit because it's...'
const response = await pRetry(
async (attemptCount) => {
const response = await sendMessage(attemptCount);
Expand Down Expand Up @@ -119,6 +121,8 @@ async function manageMessage(
{
minTimeout: 100,
factor: 1.3,
// Do not set this to undefined or Infinity, it doesn't work the same way
...(retry ? {} : { retries: 0 }),
maxRetryTime: 4000,
async onFailedAttempt(error) {
events.dispatchEvent(
Expand Down
22 changes: 22 additions & 0 deletions source/test/contentscript/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
errorTabDoesntExist,
errorTargetClosedEarly,
getMethod,
messenger,
} from "../../sender.js";
import {
expectRejection,
Expand Down Expand Up @@ -323,6 +324,27 @@ function additionalTests() {
await closeTab(tabId);
});

test("does not retry if specifically asked not to", async (t) => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/No-static-content-scripts"
);

const request = messenger("getPageTitle", { retry: false }, { tabId });
const durationPromise = trackSettleTime(request);

await expectRejection(
t,
request,
new MessengerError(
`The target ${JSON.stringify({ tabId })} for getPageTitle was not found`
)
);

expectDuration(t, await durationPromise, 0);

await closeTab(tabId);
});

test("retries until it times out even if webext-messenger was loaded (but nothing was registered)", async (t) => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/webext-messenger-was-imported-but-not-executed"
Expand Down
1 change: 1 addition & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export interface Options {
*/
isNotification?: boolean;
trace?: Sender[];
retry?: boolean;

/** Automatically generated internally */
seq?: number;
Expand Down

0 comments on commit 1ebce7b

Please sign in to comment.