Split out backgroundtask in prep for attachmentProxy separation.
This commit is contained in:
commit
9194eeb709
25
README.md
Normal file
25
README.md
Normal file
@ -0,0 +1,25 @@
|
||||
# Background task
|
||||
|
||||
An simple function wrapper around
|
||||
[requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback)
|
||||
that queues tasks and runs them sequentially.
|
||||
|
||||
The main purpose is to be able to run maintainance functions that are not a good fit for a web
|
||||
worker context.
|
||||
|
||||
# Usage
|
||||
|
||||
Wrap your function.
|
||||
|
||||
```
|
||||
const task = backgroundTask(() => {
|
||||
// do stuff
|
||||
});
|
||||
```
|
||||
|
||||
Call `task` as you would any other function that has no return value. The return value of a
|
||||
background task is the id from `requestIdleCallback` that can be passed to `cancelIdleCallback` if
|
||||
you wish to cancel the task before it runs.
|
||||
|
||||
Calling a task multiple times will queue the passed parameters and execute the task sequentially the
|
||||
same number of times.
|
||||
15
package.json
Normal file
15
package.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "backgroundtask",
|
||||
"version": "1.0.0",
|
||||
"description": "A simple way to run a task on the main thread without disrupting the UX",
|
||||
"main": "src/index.js",
|
||||
"files": [
|
||||
"src"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "node ../../bin/runTests.js ./",
|
||||
"pre-commit": "npm run test"
|
||||
},
|
||||
"author": "Timothy Farrell <tim@thecookiejar.me> (https://github.com/explorigin)",
|
||||
"license": "Apache-2.0"
|
||||
}
|
||||
46
spec/backgroundTask.spec.js
Normal file
46
spec/backgroundTask.spec.js
Normal file
@ -0,0 +1,46 @@
|
||||
import { backgroundTask } from '../src/index.js';
|
||||
|
||||
describe('A background task', () => {
|
||||
it('initially does nothing.', done => {
|
||||
const task = backgroundTask(() =>
|
||||
done(new Error('Background tasks should not spontaneously execute.'))
|
||||
);
|
||||
expect().nothing();
|
||||
setTimeout(done, 50);
|
||||
});
|
||||
|
||||
it('runs a task (but not right away)', done => {
|
||||
let shouldRun = false;
|
||||
const task = backgroundTask(() => {
|
||||
expect(shouldRun).toBeTruthy();
|
||||
done();
|
||||
});
|
||||
|
||||
task();
|
||||
shouldRun = true;
|
||||
setTimeout(done, 50);
|
||||
});
|
||||
|
||||
it('passes params', done => {
|
||||
let testVal = Math.random();
|
||||
const task = backgroundTask(val => {
|
||||
expect(val).toEqual(testVal);
|
||||
done();
|
||||
});
|
||||
|
||||
task(testVal);
|
||||
setTimeout(done, 50);
|
||||
});
|
||||
|
||||
it('queues multiple calls to be run sequentially', done => {
|
||||
let testVal = ['a', 'd', 'q'];
|
||||
const task = backgroundTask((val, index) => {
|
||||
expect(val).toEqual(testVal[index]);
|
||||
if (index === testVal.length - 1) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
testVal.map(task);
|
||||
});
|
||||
});
|
||||
46
src/index.js
Normal file
46
src/index.js
Normal file
@ -0,0 +1,46 @@
|
||||
// requestIdleCallback sortof-polyfill
|
||||
if (!self.requestIdleCallback) {
|
||||
const IDLE_TIMEOUT = 10;
|
||||
self.requestIdleCallback = cb => {
|
||||
let start = Date.now();
|
||||
return setTimeout(
|
||||
() =>
|
||||
cb({
|
||||
timeRemaining: () => Math.max(0, IDLE_TIMEOUT - (Date.now() - start))
|
||||
}),
|
||||
1
|
||||
);
|
||||
};
|
||||
self.requestIdleCallback = clearTimeout;
|
||||
}
|
||||
|
||||
export function backgroundTask(fn, timeout = undefined) {
|
||||
let id = null;
|
||||
const params = [];
|
||||
|
||||
async function runTask({ didTimeout }) {
|
||||
if (didTimeout) {
|
||||
id = requestIdleCallback(runTask);
|
||||
return;
|
||||
}
|
||||
const start = Date.now();
|
||||
const p = params.shift();
|
||||
await fn(...p);
|
||||
if (params.length) {
|
||||
id = requestIdleCallback(runTask, { timeout });
|
||||
} else {
|
||||
id = null;
|
||||
}
|
||||
}
|
||||
|
||||
const wrapper = (...args) => {
|
||||
params.push(args);
|
||||
if (id !== null) {
|
||||
return false;
|
||||
}
|
||||
id = requestIdleCallback(runTask, { timeout });
|
||||
return true;
|
||||
};
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user