pinafore/routes/_utils/runMediumPriorityTask.js
Nolan Lawson 23ccec45d0
fix notifications in a background tab (#402)
Part of the way to improving #390.

Before this fix, if you recieved a notification while Pinafore was in a background tab, nothing would happen, because most browsers (Edge, Firefox, Chrome) don't run rAF in background tabs. Furthermore, Chrome doesn't run rIC. In this PR we detect if we're in a background tab and then avoid rAF/rIC in that case.
2018-06-23 10:11:14 -07:00

17 lines
557 B
JavaScript

import { scheduleIdleTask } from './scheduleIdleTask'
import { store } from '../_store/store'
import { isMobile } from './isMobile'
// Run a task that doesn't need to be processed immediately, but should
// probably be delayed if we're on a mobile device. Also run it sooner
// if we're in a hidden tab, since browsers throttle or don't run setTimeout/rAF/etc.
export function runMediumPriorityTask (fn) {
if (store.get().pageVisibilityHidden) {
fn()
} else if (isMobile()) {
scheduleIdleTask(fn)
} else {
requestAnimationFrame(fn)
}
}