otesuki

0.2.0 • Public • Published

otesuki(お手隙)

npm license CircleCI codecov

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 git@github.com:xxx/otesuki.git # Your forked package
cd otesuki
npm i
npx lerna bootstrap

License

This package under MIT license.

Package Sidebar

Install

npm i otesuki

Weekly Downloads

1

Version

0.2.0

License

MIT

Unpacked Size

12.3 kB

Total Files

9

Last publish

Collaborators

  • lekoleko