@konforti/worker

<!-- <div align="center"> <img width="1000" src="examples/worker.png" alt="worker"> </div> -->

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
00Aug 17, 2018Jul 22, 2018Minified + gzip package size for @konforti/worker in KB

Readme

⚙️ Worker

✨ Features:

  • Move a function into a web worker.
  • Supports async function.
  • Accept any structured clone types as arguments.
  • Accept functions as arguments.

See Non blocking example

🔧 Installation

    npm i -S @konforti/worker
    yarn add @konforti/worker

✏️ Usage

import worker from '@konforti/worker';

🔦 Usage examples

// Function in worker

function fibo(n) {
    return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
}

const run = worker(fibo);
run(333).then(res => console.log(res));
// Async function in worker

const run = worker(async username => {
    const url = `https://api.github.com/users/${username}/repos`;
    const res = await fetch(url);
    const repos = await res.json();
    return repos.map(r => r.full_name);
});

run('konforti').then(res => console.log(res));
// Pass a callback function as argument

const fn = callback => `First we take Manhattan, ${callback()}`;
const run = worker(fn);
run(() => 'Then we take Berlin.').then(res => console.log(res));
// Inject imported function into worker scope

import mathModule from './math.js';

const calc = (sum, times) => {
    const a = sum(2, 3);
    const b = times(4, 5);
    return sum(a, b);
};

const run = worker(calc);
run(mathModule.sum, mathModule.times).then(res => console.log(res));

🔏 Limitations

The function and any argument passes cannot rely on its surrounding scope, since it is executed in an isolated context.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.