JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4094
  • Score
    100M100P100Q120973F
  • License CC0-1.0

Comlink Alternative. Built with JSON-RPC.

Package Exports

  • microlink
  • microlink/cjs
  • microlink/cjs/expose.js
  • microlink/cjs/index.js
  • microlink/cjs/wrap.js
  • microlink/esm
  • microlink/esm/expose.js
  • microlink/esm/index.js
  • microlink/esm/wrap.js

Readme

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)