@@ -7,17 +7,26 @@ import isSupersetOf from "set.prototype.issupersetof";
77import symmetricDifference from "set.prototype.symmetricdifference" ;
88import 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. */
1927export 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
0 commit comments