Skip to content

Commit 739166d

Browse files
committed
fix: make SDK work in browsers
1 parent 53f2767 commit 739166d

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ async function retry<T>(
7575
return await fn();
7676
} catch (e) {
7777
if (attempt < options.retries && options.isRetryable(e)) {
78-
await retryDelay(options.delayMs);
7978
options.logger.warn(
8079
`${options.name}: attempt ${attempt + 1} failed: ${e}. Retrying in ~${
8180
options.delayMs
8281
}ms...`
8382
);
83+
await retryDelay(options.delayMs);
8484
continue;
8585
}
8686
throw e;
@@ -272,7 +272,7 @@ class ReplaneInMemoryStorage implements ReplaneStorage {
272272
}
273273

274274
close() {
275-
// No resources to clean up
275+
this.closeController.abort();
276276
}
277277
}
278278

@@ -480,6 +480,7 @@ function _createReplaneClient(
480480
clearInterval(intervalId);
481481
watchers.delete(watcher);
482482
unsubscribeFromEvents();
483+
updater.stop();
483484
},
484485
};
485486

@@ -522,7 +523,11 @@ function combineOptions(
522523
return {
523524
apiKey: overrides.apiKey ?? defaults.apiKey,
524525
baseUrl: (overrides.baseUrl ?? defaults.baseUrl).replace(/\/+$/, ""),
525-
fetchFn: overrides.fetchFn ?? defaults.fetchFn ?? globalThis.fetch,
526+
fetchFn:
527+
overrides.fetchFn ??
528+
defaults.fetchFn ??
529+
// some browsers require binding the fetch function to window
530+
globalThis.fetch.bind(globalThis),
526531
timeoutMs: overrides.timeoutMs ?? defaults.timeoutMs ?? 2000,
527532
logger: overrides.logger ?? defaults.logger ?? console,
528533
retries: overrides.retries ?? defaults.retries ?? 2,
@@ -607,10 +612,10 @@ async function* fetchSse(params: {
607612
}
608613
}
609614
} finally {
610-
abortController.abort();
611615
try {
612616
await reader.cancel();
613617
} catch {}
618+
abortController.abort();
614619
}
615620
} finally {
616621
cleanUpSignals();
@@ -675,7 +680,7 @@ function combineAbortSignals(signals: Array<AbortSignal | undefined | null>) {
675680
};
676681

677682
for (const s of signals) {
678-
s?.addEventListener("abort", onAbort);
683+
s?.addEventListener("abort", onAbort, { once: true });
679684
}
680685

681686
if (signals.some((s) => s?.aborted)) {

0 commit comments

Comments
 (0)