Code Monkey home page Code Monkey logo

minlink's Introduction

minlink

Minimum(~ 1kb) and isomorphic worker wrapper with comlink like rpc.

npm install minlink --save
# or
yarn add minlink

Why?

  • WebWorker(DedicateWorker) and node's Worker(threads) have similar api but not same one. This library wraps them to same rpc.
  • minlink is inspired by comlink but to keep simple and small core, minlink does not use ES2015 Proxy(or its polyfill). Instead of proxy, minlink provides typescript's type utils.

Requirements

  • Node 14+
  • Modern Browser + IE11(WIP: Not tested yet)

Browser WebWorker

Minlink takes WebWorker as expose/wrap. Bundle them with webpack or rollup.

// browser worker.js
import { expose } from "minlink/dist/browser.mjs";
const impl = {
  async foo(n; number) {
    return n + 1;
  },
};
expose(self, impl);

// browesr main.js
// import { wrap } from "minlink/dist/browser.legacy.js"; // for ie11. UMD build.
import { wrap } from "minlink/dist/browser.mjs";
const api = wrap(new Worker("./worker.js"));
const ret = await api.exec("foo", 1);
console.log(ret); // => 2
await api.terminate();

Node Worker

Minlink takes worker_threads/Worker as expose/wrap.

// main.mjs
import { wrap } from "comlink/dist/node.mjs";
import { Worker } from "worker_threads";
const worker = new Worker("./worker.mjs");
const api = wrap(worker);
const res = await api.exec("foo", 1);
console.log("response", res);

// worker.mjs
import { expose } from "minlink/dist/node.mjs";
import { parentPort } from "worker_threads";
expose(parentPort, {
  async foo(n) {
    return n + 1;
  },
});

Advanced: TypeScript utilities

// browser/worker.ts
import { expose } from "minlink/dist/browser.mjs";
const impl = {
  async foo(n; number) {
    return n + 1;
  },
};
export type RemoteImpl = typeof impl;
expose(self, impl);

// browesr/main.ts
import type { RemoteImpl } from "./worker.ts"; // Typescript 3.9+ Type only import
import { wrap } from "minlink/dist/browser.mjs";
const api = wrap<RemoteImpl>(new Worker("/worker.js")); // take RemoteImpl as `wrap(...)`'s type argument.
const ret = await api.exec("foo", 1); // pass
const ret = await api.exec("foo", "invalid arg"); // type error

Advanced: Transferrable

const buf = new Uint8Array([1]);
const ret = await api.exec(["foo", [buf]], buf); // pass

Advanced: Call client expose from worker.

TBD

Inspired by ...

ChangeLog

  • v2: transferrable uses ['foo', [buf]] from {cmd: 'foo', transferrable: [buf]}
  • v1: release

LICENSE

MIT

minlink's People

Contributors

mizchi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

minlink's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.