Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
/ otesuki Public archive

Minimal task queue executed when CPU is idle

License

Notifications You must be signed in to change notification settings

Leko/otesuki

Repository files navigation

otesuki(お手隙)

npm license CircleCI codecov Greenkeeper badge

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.