Skip to content

Commit 81a9d0a

Browse files
committed
sync(common-v8): finalize RN polyfills review fixes
- Move Set shims to explicit idempotent install path\n- Export Polyfills entrypoint via package exports/index\n- Preserve first abort reason and avoid patching native reason runtimes\n- Remove no-op lint suppression
1 parent 98e23b6 commit 81a9d0a

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

packages/react-native/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"react-native": "./dist/src/index.js",
2525
"default": "./dist/src/index.js"
2626
},
27+
"./Polyfills": {
28+
"types": "./dist/src/Polyfills.d.ts",
29+
"react-native": "./dist/src/Polyfills.js",
30+
"default": "./dist/src/Polyfills.js"
31+
},
2732
"./expo-sqlite": {
2833
"types": "./dist/src/exports/expo-sqlite.d.ts",
2934
"react-native": "./dist/src/exports/expo-sqlite.js",
@@ -48,6 +53,9 @@
4853
".": [
4954
"./dist/src/index.d.ts"
5055
],
56+
"Polyfills": [
57+
"./dist/src/Polyfills.d.ts"
58+
],
5159
"expo-sqlite": [
5260
"./dist/src/exports/expo-sqlite.d.ts"
5361
],

packages/react-native/src/Polyfills.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ import isSupersetOf from "set.prototype.issupersetof";
77
import symmetricDifference from "set.prototype.symmetricdifference";
88
import union from "set.prototype.union";
99

10-
difference.shim();
11-
intersection.shim();
12-
isDisjointFrom.shim();
13-
isSubsetOf.shim();
14-
isSupersetOf.shim();
15-
symmetricDifference.shim();
16-
union.shim();
10+
let areSetPolyfillsInstalled = false;
11+
12+
const installSetPolyfills = (): void => {
13+
if (areSetPolyfillsInstalled) return;
14+
15+
difference.shim();
16+
intersection.shim();
17+
isDisjointFrom.shim();
18+
isSubsetOf.shim();
19+
isSupersetOf.shim();
20+
symmetricDifference.shim();
21+
union.shim();
22+
23+
areSetPolyfillsInstalled = true;
24+
};
1725

1826
/** Installs polyfills required by Evolu in React Native runtimes. */
1927
export const installPolyfills = (): void => {
2028
installCommonPolyfills();
29+
installSetPolyfills();
2130
installPromisePolyfills();
2231
installAbortControllerPolyfills();
2332
};
@@ -54,7 +63,6 @@ const installPromisePolyfills = () => {
5463
try {
5564
resolve(func(...args));
5665
} catch (error) {
57-
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
5866
reject(error);
5967
}
6068
});
@@ -114,23 +122,31 @@ const installAbortReasonPolyfill = (
114122
abortController: AbortControllerConstructor,
115123
abortSignal: AbortSignalConstructor,
116124
): void => {
117-
if (!("reason" in abortSignal.prototype)) {
118-
Object.defineProperty(abortSignal.prototype, "reason", {
119-
configurable: true,
120-
enumerable: false,
121-
get(this: AbortSignal): unknown {
122-
return abortReasonBySignal.get(this);
123-
},
124-
});
125-
}
125+
const hasNativeReason = "reason" in abortSignal.prototype;
126+
if (hasNativeReason) return;
127+
128+
Object.defineProperty(abortSignal.prototype, "reason", {
129+
configurable: true,
130+
enumerable: false,
131+
get(this: AbortSignal): unknown {
132+
return abortReasonBySignal.get(this);
133+
},
134+
});
126135

127136
const prototype = abortController.prototype as AbortControllerPrototype;
128137
if (prototype.__evoluAbortReasonPatched) return;
129138

130139
const nativeAbort = prototype.abort;
131140
prototype.abort = function (this: AbortController, reason?: unknown): void {
132-
const normalizedReason = reason === undefined ? createAbortError() : reason;
133-
abortReasonBySignal.set(this.signal, normalizedReason);
141+
const signal = this.signal;
142+
const normalizedReason =
143+
abortReasonBySignal.get(signal) ??
144+
(reason === undefined ? createAbortError() : reason);
145+
146+
if (!abortReasonBySignal.has(signal)) {
147+
abortReasonBySignal.set(signal, normalizedReason);
148+
}
149+
134150
nativeAbort.call(this, normalizedReason);
135151
};
136152

packages/react-native/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./components/EvoluIdenticon.js";
2+
export * from "./Polyfills.js";
23
export * from "./Task.js";
34
export * from "./Worker.js";

0 commit comments

Comments
 (0)