Skip to content

Instantly share code, notes, and snippets.

View briandesousa's full-sized avatar

Brian De Sousa briandesousa

View GitHub Profile
@briandesousa
briandesousa / node-worker-thread-rxjs.js
Last active February 16, 2019 06:45
Demonstrating how to use Node worker_thread module with RxJS observable to stream data from a worker thread to it's parent thread.
const { workerData, parentPort } = require('worker_threads');
parentPort.postMessage(`starting heavy duty work from process ${process.pid} that will take ${workerData}s to complete`);
timeLimit = workerData;
timer = 0;
// simulate a long-running process with updates posted back on a regular interval
do {
setTimeout(
@briandesousa
briandesousa / node-parent-thread-rxjs.js
Last active February 16, 2019 06:45
Demonstrating how to use Node worker_thread module with RxJS observable to stream data from a worker thread to it's parent thread.
const Rxjs = require('rxjs');
const RxjsOperators = require('rxjs/operators');
const { Worker } = require('worker_threads');
console.log("\nNode multi-threading demo using worker_threads module in Node 11.7.0\n");
const COMPLETE_SIGNAL = 'COMPLETE';
function runTask(workerData, completedOnTime) {
return Rxjs.Observable.create(observer => {