Add support for promises and fix tests to use karmatic
This commit is contained in:
parent
b400d28310
commit
06f8097609
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
coverage
|
||||
7324
package-lock.json
generated
Normal file
7324
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@ -1,15 +1,25 @@
|
||||
{
|
||||
"name": "backgroundtask",
|
||||
"version": "1.0.0",
|
||||
"description": "A simple way to run a task on the main thread without disrupting the UX",
|
||||
"description": "A simple way to run a task on the main thread without disrupting the UI",
|
||||
"main": "src/index.js",
|
||||
"files": [
|
||||
"src"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "node ../../bin/runTests.js ./",
|
||||
"pre-commit": "npm run test"
|
||||
"test": "karmatic"
|
||||
},
|
||||
"author": "Timothy Farrell <tim@thecookiejar.me> (https://github.com/explorigin)",
|
||||
"license": "Apache-2.0"
|
||||
"license": "Apache-2.0",
|
||||
"devDependencies": {
|
||||
"husky": "^3.0.1",
|
||||
"karmatic": "^1.3.1",
|
||||
"webpack": "^4.37.0"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "npm run test"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,46 +0,0 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
37
src/index.js
37
src/index.js
@ -11,35 +11,40 @@ if (!self.requestIdleCallback) {
|
||||
1
|
||||
);
|
||||
};
|
||||
self.requestIdleCallback = clearTimeout;
|
||||
self.cancelIdleCallback = clearTimeout;
|
||||
}
|
||||
|
||||
export function backgroundTask(fn, timeout = undefined) {
|
||||
export function backgroundTask(fn) {
|
||||
let id = null;
|
||||
const params = [];
|
||||
|
||||
async function runTask({ didTimeout }) {
|
||||
if (didTimeout) {
|
||||
id = requestIdleCallback(runTask);
|
||||
return;
|
||||
}
|
||||
async function runTask() {
|
||||
const start = Date.now();
|
||||
const p = params.shift();
|
||||
await fn(...p);
|
||||
const [p, resolve, reject] = params.shift();
|
||||
let result;
|
||||
let conclusion;
|
||||
try {
|
||||
result = await fn(...p);
|
||||
conclusion = resolve;
|
||||
} catch (e) {
|
||||
result = e;
|
||||
conclusion = reject;
|
||||
}
|
||||
if (params.length) {
|
||||
id = requestIdleCallback(runTask, { timeout });
|
||||
id = requestIdleCallback(runTask);
|
||||
} else {
|
||||
id = null;
|
||||
}
|
||||
conclusion(result);
|
||||
}
|
||||
|
||||
const wrapper = (...args) => {
|
||||
params.push(args);
|
||||
if (id !== null) {
|
||||
return false;
|
||||
}
|
||||
id = requestIdleCallback(runTask, { timeout });
|
||||
return true;
|
||||
return new Promise((resolve, reject) => {
|
||||
params.push([args, resolve, reject]);
|
||||
if (id === null) {
|
||||
id = requestIdleCallback(runTask);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return wrapper;
|
||||
|
||||
90
src/index.test.js
Normal file
90
src/index.test.js
Normal file
@ -0,0 +1,90 @@
|
||||
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(new Error('Background tasks did not run within 50msec of inactivity.'))
|
||||
}, 50);
|
||||
});
|
||||
|
||||
it('receives passed params', done => {
|
||||
let testVal = Math.random();
|
||||
const task = backgroundTask(val => {
|
||||
expect(val).toEqual(testVal);
|
||||
done();
|
||||
});
|
||||
|
||||
task(testVal);
|
||||
setTimeout(() => {
|
||||
done(new Error('Background tasks did not run within 50msec of inactivity.'))
|
||||
}, 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);
|
||||
});
|
||||
|
||||
it('returns a promise for invocation', done => {
|
||||
let testVal = ['a', 'd', 'q'];
|
||||
const task = backgroundTask((val, index) => {
|
||||
expect(val).toEqual(testVal[index]);
|
||||
return val + index;
|
||||
});
|
||||
|
||||
Promise.all(testVal.map(task)).then(results => {
|
||||
expect(results).toEqual(['a0', 'd1', 'q2']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns properly if the task is an async function', done => {
|
||||
let testVal = ['a', 'd', 'q'];
|
||||
const task = backgroundTask((val, index) => {
|
||||
expect(val).toEqual(testVal[index]);
|
||||
return new Promise(resolve => {
|
||||
resolve(val + index);
|
||||
})
|
||||
});
|
||||
|
||||
Promise.all(testVal.map(task)).then(results => {
|
||||
expect(results).toEqual(['a0', 'd1', 'q2']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects if the task throws an exception', done => {
|
||||
const task = backgroundTask((val, index) => {
|
||||
throw new Error('message');
|
||||
});
|
||||
|
||||
task().then(results => {
|
||||
done(new Error('Should fail'));
|
||||
}).catch((e) => {
|
||||
expect(e.message).toEqual('message');
|
||||
}).then(done);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user