pinafore/src/routes/_utils/throttleTimer.js
Nolan Lawson b2d7fad435
perf: only update draggable x/y state at end of drag (#1379)
* perf: only update draggable x/y state at end of drag

This is more intelligent and more performant than using requestIdleCallback willy-nilly. We can just update the store when the user is actually done dragging the button.

* remove console.log

* consistent syntax
2019-08-07 20:38:01 -07:00

21 lines
524 B
JavaScript

// Sometimes we want to queue multiple requestAnimationFrames but only run the last one.
// It's tedious to do this using cancelAnimationFrame, so this is a utility to throttle
// a timer such that it only runs the last callback when it fires.
export const throttleTimer = timer => {
let queuedCallback
const flush = () => {
const callback = queuedCallback
queuedCallback = null
callback()
}
return callback => {
if (!queuedCallback) {
timer(flush)
}
queuedCallback = callback
}
}