All Projects → kwolfy → iworker

kwolfy / iworker

Licence: MIT license
Promise-based wrapper for worker_threads

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to iworker

event-worker
A simpler way of dealing with Web Workers
Stars: ✭ 18 (+0%)
Mutual labels:  worker, promise, threads
ncpu
multi-threaded library that node.js run function worker
Stars: ✭ 16 (-11.11%)
Mutual labels:  worker, threads
Toolkit
Collection of useful patterns
Stars: ✭ 137 (+661.11%)
Mutual labels:  worker, promise
meteor-cluster
worker pool for meteor using node js native `cluster` module
Stars: ✭ 18 (+0%)
Mutual labels:  worker, worker-threads
Post Me
📩 Use web Workers and other Windows through a simple Promise API
Stars: ✭ 398 (+2111.11%)
Mutual labels:  worker, promise
wtsqs
Simplified Node AWS SQS Worker Wrapper
Stars: ✭ 18 (+0%)
Mutual labels:  worker, promise
rmfr
Node.js implementation of rm -fr – recursive removal of files and directories
Stars: ✭ 23 (+27.78%)
Mutual labels:  promise
isolated-runtime
Run untrusted Javascript code in a multi-tenant, isolated environment
Stars: ✭ 21 (+16.67%)
Mutual labels:  threads
PiedPiper
A small set of classes and functions to make easy use of Futures, Promises and async computation in general. All written in Swift for iOS 10+, WatchOS 3, tvOS and Mac OS X apps.
Stars: ✭ 44 (+144.44%)
Mutual labels:  promise
PromisedFuture
A Swift based Future/Promises framework to help writing asynchronous code in an elegant way
Stars: ✭ 75 (+316.67%)
Mutual labels:  promise
hangman-game
A responsive hangman game built with vanilla javascript, html, and css. Animated with GSAP and canvas animations.
Stars: ✭ 22 (+22.22%)
Mutual labels:  promise
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (+116.67%)
Mutual labels:  worker
best-queue
Queue in runtime based promise
Stars: ✭ 26 (+44.44%)
Mutual labels:  promise
repromised
🤝 Declarative promise resolver as a render props component
Stars: ✭ 20 (+11.11%)
Mutual labels:  promise
rsmq-promise
Promise interface for RSMQ
Stars: ✭ 28 (+55.56%)
Mutual labels:  worker
promise-abortable
Promise lib for aborting in chain.
Stars: ✭ 19 (+5.56%)
Mutual labels:  promise
lightflow
A tiny Promise-inspired control flow library for browser and Node.js.
Stars: ✭ 29 (+61.11%)
Mutual labels:  promise
nodeJS examples
Server, routing, db examples using NodeJS v6
Stars: ✭ 34 (+88.89%)
Mutual labels:  promise
eslint-config-welly
😎 ⚙️ ESLint configuration for React projects that I do. Feel free to use this!
Stars: ✭ 21 (+16.67%)
Mutual labels:  promise
SACK
System Abstraction Component Kit
Stars: ✭ 18 (+0%)
Mutual labels:  threads

IWorker Build Status

Promise-based wrapper for worker_threads

Install

npm install iworker

Using

const { WorkerGroup } = require('iworker');
const wg = new WorkerGroup();

Object schema based

Just put object schema with methods and magical iworker do others

const worker = wg.newThread({
  async foo(bar, baz) {
    return 'foo' + bar + baz;
  }
});


await worker.foo('bar', 'baz'); // foobarbaz

Factory based

You can define methods from factory-function

const worker = wg.newThread((worker) => {
  const crypto = require('crypto');
  
  worker.def('randomBytes', async (size) => {
    return crypto.randomBytes(size);
  });
});


await worker.randomBytes(32); // Buffer(32) 

Emit events during execution

When you want to send some events during the execution of the function

const worker = wg.newThread({
  async foo() {
    this.sendEvent('started');
    this.sendEvent('progress', 99);
    return 'bar';
  }
});


const res = await worker.foo()
  .on('started', () => {
    // do some job
  })
  .on('progress', (per) => {
    // do some job
  });
  
// res - "baz"

Send transferable objects

Instead of copying data, you can move them using Worker Transferable Objects

const worker = wg.newThread({
  async foo(buf) { return buf.toString('hex')}
});

const buf = Buffer.from('Hello', 'utf8');
const hex = buf.toString('hex');

const res = await worker.foo(buf).withTransferList([buf.buffer]);
assert.strictEqual(res, hex);
assert.strictEqual(buf.length, 0);

Return transferable objects from worker

const worker = wg.newThread({
  async foo() { 
    const buf = Buffer.from('hello', 'utf8');
    this.setTransferList([buf.buffer]);
    return buf;
  }
});

const buf = await worker.foo();

Pure event-emitter

You can use iworker as pure event-emitter

const worker = wg.newThread((w) => {
  w.on('event', (...args) => w.emit('eventBack', 'arg1', 'arg2'));
});

worker.on('eventBack', (arg1, arg2) => {
  // some code
});

worker.emit('event', 'arg');
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].