Created
April 26, 2017 12:21
-
-
Save mrtnbroder/fcf8086750642ccd6fd21f2b5bab4781 to your computer and use it in GitHub Desktop.
HowTo requestIdleCallback
This file contains hidden or 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
| import R from 'ramda' | |
| const runTask = R.curry((taskList, deadline) => { | |
| while ((deadline.timeRemaining() > 0 || deadline.didTimeout) && taskList.length) { | |
| const task = taskList.shift() | |
| task() | |
| } | |
| if (taskList.length) { | |
| requestIdleCallback(runTask(taskList), { timeout: 1000 }) | |
| } | |
| }) | |
| const initTask = () => { | |
| const taskList = [] | |
| return function enqueueTask(f) { | |
| taskList.push(f) | |
| requestIdleCallback(runTask(taskList), { timeout: 1000 }) | |
| } | |
| } | |
| // ....somewhere else in your app | |
| const app = () => { | |
| const taskQueue = initTask() | |
| const task = () => { | |
| // do stuff | |
| } | |
| taskQueue(task) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment