0.2.0 • Published 6 years ago

otesuki v0.2.0

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

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);
ArgumentRequiredDescriptionDefault
executorYestask executor function. Is must return Promise
option-Option object (see below){}
option.debug-Log verboselyfalse
option.retry-Count of retry3

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.