-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(clerk-js): Pause/resume session touch while offline (#5098)
- Loading branch information
1 parent
a4bf420
commit cab9408
Showing
6 changed files
with
76 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
--- | ||
|
||
Pause session touch and token refresh while browser is offline, and resume it when the device comes back online. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { isBrowserOnline } from '@clerk/shared/browser'; | ||
|
||
/** | ||
* While online callbacks passed to `.schedule` will execute immediately. | ||
* While offline callbacks passed to `.schedule` are de-duped and only the first one will be scheduled for execution when online. | ||
*/ | ||
export const createOfflineScheduler = () => { | ||
let scheduled = false; | ||
|
||
const schedule = (cb: () => void) => { | ||
if (scheduled) { | ||
return; | ||
} | ||
if (isBrowserOnline()) { | ||
cb(); | ||
return; | ||
} | ||
scheduled = true; | ||
const controller = new AbortController(); | ||
window.addEventListener( | ||
'online', | ||
() => { | ||
void cb(); | ||
scheduled = false; | ||
controller.abort(); | ||
}, | ||
{ | ||
signal: controller.signal, | ||
}, | ||
); | ||
}; | ||
|
||
return { | ||
schedule, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters