Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
concurrent-worker
Advanced tools
This library allowes you to create web workers inline, focussed on concurrency and control flow. It is Promise based, this allowes control flow to be regulated via Promise chains as well as async / await. You can also pass in and use functions and objects / primitives from the main thread via the context. All exceptions and promise rejections are propagated to the main thread and can be handled in a promise catch or try / catch block in an async function.
There are two worker creation methods, serial
and concurrent
. A serial
worker will create a single Worker
that all calls to it will be executed on. A concurrent
worker will create a new Worker
for each call to it, this allowes you to run multiple calls in parallel. The API's of both are identical.
npm install concurrent-worker --save
import { serial } from "concurrent-worker";
const worker = serial((x, y) => x + y);
worker.run([1, 2]).then(result => {
console.log(result); // 3
});
The second, optional, argument to the creation method is a configuration object, this has the following properties:
Key | Type | Description | Default |
---|---|---|---|
context | object | All functions, objects and primitives on this object are available in the Worker, they can be accessed via this.{key} | {} |
inTransferrable | Function | A function that returns an array of Transferrable objects from the input arguments | () => [] |
outTransferrable | Function | A function that returns an array of Transferrable objects from the result object. | () => [] |
rootUrl | string | Scripts loaded from a relative path need to be prepended with the sites root URL. | '' |
scripts | string[] | Array of script URL's, these scripts are loaded in the worker when it's instantiated. | [] |
import { concurrent } from "concurrent-worker";
const sum = (x, y) => x + y;
const concurrentWorker = concurrent(sum);
const runAsyncTasks = async () => {
// These three calls will run concurrently on 3 seperate workers
const processes = Promise.all([
concurrentWorker.run([1, 2]),
concurrentWorker.run([2, 3]),
concurrentWorker.run([3, 4])
]);
const results = await processes; // [3, 5, 7]
const summed = results.reduce((acc, val) => (acc += val), 0);
console.log(summed); // 15
};
import { serial } from "concurrent-worker";
const constNumber = 5;
function add(this: typeof ctx, x: number, y: number) {
return x + y + this.constNumber;
}
function run(this: typeof ctx, x: number, y: number) {
return this.add(x, y);
}
const context = { add, constNumber };
const worker = serial(run, { context });
import { serial } from "concurrent-worker";
const arrayAdd = (n: number, arr: Float32Array) => arr.map(x => x + n);
const worker = serial(arrayAdd, {
inTransferrable: ([n, arr]) => [arr.buffer],
outTransferrable: arr => [arr.buffer]
});
const arr = new Float32Array([1, 2, 3, 4]);
worker.run([5, arr]).then(console.log); // Float32Array([6, 7, 8, 9])
import { concurrent } from "concurrent-worker";
// Function using lodash map from cdn and sum function from self hosted script import.
const arrayAdd = (n: number, arr: Float32Array) => _.map(arr, x => sum(x, n));
const worker = concurrent(arrayAdd, {
inTransferrable: ([n, arr]) => [arr.buffer],
outTransferrable: arr => [arr.buffer],
rootUrl: "http://www.mydomain.com",
scripts: [
"/js/sum.js",
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"
]
});
const arr = new Float32Array([1, 2, 3, 4]);
worker.run([5, arr]).then(console.log); // Float32Array([6, 7, 8, 9])
If your wrapped function returns a Promise, it will be resolved in the worker before returning to the main thread.
import { serial } from "concurrent-worker";
const worker = serial((x, y) => Promise.resolve(x + y));
worker.run([1, 2]).then(result => {
console.log(result); // 3
});
FAQs
Multithreading for javascript
We found that concurrent-worker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.