-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ continue running scheduled tasks when page is hidden
- Loading branch information
Showing
3 changed files
with
67 additions
and
7 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
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,32 @@ | ||
import waitNextTask from './waitNextTask' | ||
import withResolvers from './withResolvers' | ||
|
||
const state = { | ||
scheduled: false, | ||
hiddenTask: withResolvers(), | ||
} | ||
|
||
export default async function waitHiddenTask(): Promise<void> { | ||
if (document.visibilityState === 'hidden') { | ||
await waitNextTask() | ||
|
||
// in theory, here the page could have been hidden again, | ||
// but we ignore this case on purpose | ||
} else { | ||
if (!state.scheduled) { | ||
state.scheduled = true | ||
state.hiddenTask = withResolvers() | ||
onVisibilityChange(() => { | ||
// events, are already in a new task, so we don't need to call `waitNextTask()` | ||
state.scheduled = false | ||
state.hiddenTask.resolve() | ||
}) | ||
} | ||
|
||
return state.hiddenTask.promise | ||
} | ||
} | ||
|
||
function onVisibilityChange(callback: () => void): void { | ||
document.addEventListener('visibilitychange', callback, { once: true }) | ||
} |
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,28 @@ | ||
import withResolvers from './withResolvers' | ||
|
||
const state = { | ||
scheduled: false, | ||
nextTask: withResolvers(), | ||
} | ||
|
||
// same as queueMicrotask() but for tasks | ||
// https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide#tasks_vs._microtasks | ||
// https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide/In_depth#tasks_vs._microtasks | ||
export default function waitNextTask(): Promise<void> { | ||
if (!state.scheduled) { | ||
state.scheduled = true | ||
state.nextTask = withResolvers() | ||
nextTask(() => { | ||
state.scheduled = false | ||
state.nextTask.resolve() | ||
}) | ||
} | ||
|
||
return state.nextTask.promise | ||
} | ||
|
||
function nextTask(callback: () => void): void { | ||
const channel = new MessageChannel() | ||
channel.port2.postMessage(undefined) | ||
channel.port1.onmessage = callback | ||
} |