chf
TypeScript icon, indicating that this package has built-in type declarations

3.0.0-rc1 • Public • Published

Chf

Build Status NPM Version

Chf is a minimal library for "chunkifying" long-running tasks with ability to be aborted. The main idea is to split such long-running task into small units of work joined into chunks with limited budget of execution time.

Installation

$ npm install chf

Note: Chf requires native Promise api so if your environment doesn't support them, you will have to install any suitable polyfill as well.

Usage

// with ES6 modules
import { createTask } from 'chf';
 
// with CommonJS modules
const { createTask } = require('chf');

Example

import { createTask } from 'chf';
 
const task = createTask({
    // Define unit of work
    unit: function unit(prevResult: number = 0) {
        const result = prevResult + 1;
        
        return {            
            next: result < 10? unit : null,
            result 
        };
    }, 
    
    // All units will be joined into chunks with execution budget limited to 20ms
    budget: 20
});
 
// Run task
const promise = task.run();
 
// Wait until task has completed
promise.then(
    result => {
        console.log(result);
    },
    err => {
        console.error(err);
    });
 
// Abort task at any time, next chunk of units won't be executed
setTimeout(task.abort, 50);

Package Sidebar

Install

npm i chf

Weekly Downloads

0

Version

3.0.0-rc1

License

MIT

Unpacked Size

9.69 kB

Total Files

13

Last publish

Collaborators

  • dfilatov