Skip to content

Commit 7e49a70

Browse files
authored
fix(auth): clear initial setTimeout in stopAutoRefresh (#1993)
1 parent d3d05f8 commit 7e49a70

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/core/auth-js/src/GoTrueClient.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export default class GoTrueClient {
246246
protected memoryStorage: { [key: string]: string } | null = null
247247
protected stateChangeEmitters: Map<string | symbol, Subscription> = new Map()
248248
protected autoRefreshTicker: ReturnType<typeof setInterval> | null = null
249+
protected autoRefreshTickTimeout: ReturnType<typeof setTimeout> | null = null
249250
protected visibilityChangedCallback: (() => Promise<any>) | null = null
250251
protected refreshingDeferred: Deferred<CallRefreshTokenResult> | null = null
251252
/**
@@ -2873,10 +2874,19 @@ export default class GoTrueClient {
28732874
// run the tick immediately, but in the next pass of the event loop so that
28742875
// #_initialize can be allowed to complete without recursively waiting on
28752876
// itself
2876-
setTimeout(async () => {
2877+
const timeout = setTimeout(async () => {
28772878
await this.initializePromise
28782879
await this._autoRefreshTokenTick()
28792880
}, 0)
2881+
this.autoRefreshTickTimeout = timeout
2882+
2883+
if (timeout && typeof timeout === 'object' && typeof timeout.unref === 'function') {
2884+
timeout.unref()
2885+
// @ts-expect-error TS has no context of Deno
2886+
} else if (typeof Deno !== 'undefined' && typeof Deno.unrefTimer === 'function') {
2887+
// @ts-expect-error TS has no context of Deno
2888+
Deno.unrefTimer(timeout)
2889+
}
28802890
}
28812891

28822892
/**
@@ -2892,6 +2902,13 @@ export default class GoTrueClient {
28922902
if (ticker) {
28932903
clearInterval(ticker)
28942904
}
2905+
2906+
const timeout = this.autoRefreshTickTimeout
2907+
this.autoRefreshTickTimeout = null
2908+
2909+
if (timeout) {
2910+
clearTimeout(timeout)
2911+
}
28952912
}
28962913

28972914
/**

0 commit comments

Comments
 (0)