workerpool icon indicating copy to clipboard operation
workerpool copied to clipboard

adds worker-side on(message) events

Open flipswitchingmonkey opened this issue 2 years ago • 2 comments

This PR is related to https://github.com/josdejong/workerpool/issues/370

A rather small change that adds a second parameter to workerpool.worker() function, which accepts an object where each key is the receiver's name of an event, plus a payload.

It also adds an emit() function to the Promise object which will send a postMessage() call to the worker instance.

These changes should be non-breaking.

Example: workers/myworker.js

workerpool.worker({
  fibonacci: fibonacci,
}, {
  callme: function (payload) {
    makeThingsHappen(payload);
  }
});

And to call the event:

const pool = new Pool(__dirname + "/workers/myworker.js");

// main difference here is that the handler, a Promise, is picked up from the exec() command first
const handler = pool.exec('fibonacci', [15]);

// now continue the chaining
handler
  .then(function (result) {
    assert.strictEqual(result, 610);
  })
  .catch(function (err) {
  })
  .then(function () {
    pool.terminate(); // terminate all workers when done
  })
  
// the Promise now has a function Promise.emit(name, paramerers) 
handler.emit('callme', {say: 'O hai!'});

flipswitchingmonkey avatar Jan 17 '23 18:01 flipswitchingmonkey

The functionality here is crucial for my team to use this package. This PR appears to have been abandoned, and now unsurprisingly conflicts greatly. @josdejong what would be your level of interest in re-approaching this?

blordpluto avatar May 29 '24 17:05 blordpluto

@blordpluto thanks for your input. I would love to see this feature finished. The PR was under review but not yet there. Would you be interested in creating a new PR implementing this feature?

josdejong avatar May 29 '24 20:05 josdejong