Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/mobile/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "./src/platform/installCrypto";
import "./src/platform/installAbortSignal";
import "./src/background/defineNavigationTask";
import { registerRootComponent } from "expo";
import { App } from "./src/App";
Expand Down
30 changes: 30 additions & 0 deletions apps/mobile/src/platform/installAbortSignal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { installAbortSignal } from "./installAbortSignal";

class BareSignal {
aborted = false;
reason?: unknown;
}
type Throwing = BareSignal & { throwIfAborted(): void };

describe("installAbortSignal", () => {
it("adds throwIfAborted to a signal class without it", () => {
installAbortSignal(BareSignal);
const signal = new BareSignal() as Throwing;
expect(() => signal.throwIfAborted()).not.toThrow();
signal.aborted = true;
expect(() => signal.throwIfAborted()).toThrow(expect.objectContaining({ name: "AbortError" }));
const reason = new Error("timeout");
signal.reason = reason;
expect(() => signal.throwIfAborted()).toThrow(reason);
});

it("keeps an existing implementation", () => {
const own = () => undefined;
class Native {
throwIfAborted = own;
}
Object.defineProperty(Native.prototype, "throwIfAborted", { value: own });
installAbortSignal(Native);
expect(Native.prototype.throwIfAborted).toBe(own);
});
});
28 changes: 28 additions & 0 deletions apps/mobile/src/platform/installAbortSignal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Gives React Native's `AbortSignal` the `throwIfAborted()` method it lacks.
*
* The shared API client calls `signal.throwIfAborted()` after reading a
* response, so without it every directions, reroute and transit refresh
* request from the shell fails with "undefined is not a function". A runtime
* that already has the method keeps it.
*/
export function installAbortSignal(
ctor: { prototype: object } | undefined = globalThis.AbortSignal,
): void {
if (!ctor) return;
const prototype = ctor.prototype as { throwIfAborted?: unknown };
if (typeof prototype.throwIfAborted === "function") return;
Object.defineProperty(prototype, "throwIfAborted", {
configurable: true,
writable: true,
value(this: { aborted: boolean; reason?: unknown }) {
if (!this.aborted) return;
if (this.reason !== undefined) throw this.reason;
const error = new Error("This operation was aborted");
error.name = "AbortError";
throw error;
},
});
}

installAbortSignal();
Loading