otesuki
v0.2.0
Published
Minimal task queue executed when CPU is idle
Downloads
3
Maintainers
Readme
otesuki(お手隙)
Minimal task queue executed when CPU is idle
Install
npm install otesuki
Usage
import { Queue } from "otesuki";
const executor = action => {
switch (action.type) {
case "preload":
return fetch(action.payload.url);
case "createSomeHeavyCache":
// Some heavy task
}
};
const queue = new Queue(executor, {
debug: true
});
queue.push({ type: "preload", payload: { url: "https://example.com" } });
queue.push({ type: "createSomeHeavyCache" });
API
new Queue
const queue = new Queue(executor, option);
| Argument | Required | Description | Default |
| ------------ | -------- | ---------------------------------------------- | ------- |
| executor | Yes | task executor function. Is must return Promise | |
| option | - | Option object (see below) | {}
|
| option.debug | - | Log verbosely | false
|
| option.retry | - | Count of retry | 3
|
Queue.push
queue.push({});
Queue.push can receive any objects.
It will passed to argument of executor
.
For TypeScript
otesuki supports TypeScript.
type PreloadAction = {
type: 'preload',
payload: {
url: string
}
}
type SomeHeavyTaskAction = {
type: 'someHeavyTask'
}
type Action = PreloadAction | SomeHeavyTaskAction
const executor = async (action: Action): Promise<void> => {
switch (action.type) {
case 'preload':
return fetch(...)
case 'someHeavyTask':
await ...
}
}
const queue: Queue<Action> = new Queue(executor)
queue.push()
Development
git clone [email protected]:xxx/otesuki.git # Your forked package
cd otesuki
npm i
npx lerna bootstrap
License
This package under MIT license.