microlink

0.3.0 • Public • Published

microlink

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)

Package Sidebar

Install

npm i microlink

Weekly Downloads

28

Version

0.3.0

License

CC0-1.0

Unpacked Size

62.4 kB

Total Files

25

Last publish

Collaborators

  • danieljdufour