npm.io
0.3.0 • Published 2 years ago

microlink

Licence
CC0-1.0
Version
0.3.0
Deps
0
Size
61 kB
Vulns
0
Weekly
0

Comlink Alternative

features

  • easily pass functions to worker threads
  • built with JSON-RPC 2.0
  • zero run-time dependencies
  • await promise in another thread
  • batching

usage

inside worker.js
import { expose } from 'microlink';

expose({
  run: (func, args) => func(...args),
  halve: n => n / 2,
});
inside main.js
import { wrap } from 'microlink';
// or, import wrap from 'microlink/wrap';

const worker = new Worker("worker.js");
const obj = await wrap(worker);

await obj.halve(10);
5

const count_elements = selector => document.querySelectorAll(selector).length;
await obj.run(count_elements, 'div');

advanced usage

batching
import { expose } from "microlink";

expose(methods, {
  batch_size: 10, // how many requests per batch should be sent up to the main thread
  batch_wait: 100, // wait up to 100ms for batch to be filled before sent
})
debugging
// pass an options object to expose or wrap
const options = { debug_level: 10 };

expose(methods, options);
wrap(worker, options)

Keywords