@shinka-rpc/shared-worker

v0.0.2

Published

Symmetric RPC bus. [Documentation is here](https://shinka-rpc-js.readthedocs.io/latest/transports/shared-worker/)

Readme

@shinka-rpc/shared-worker

Symmetric RPC bus. Documentation is here

This package implements the transport implementation of @shinka-rpc/core for SharedWorker

Usage

client case

import { ClientBus, FactoryClient } from "@shinka-rpc/core";
import { SharedWorker2FactoryData } from "@shinka-rpc/shared-worker";
import serializer from "@shinka-rpc/serializer-json";  // for example

const factory: FactoryClient<ClientBus> = async (bus) =>
  SharedWorker2FactoryData(
    new SharedWorker(new URL("./worker.ts", import.meta.url)),
    bus,
  );

export const bus = new ClientBus({ factory, serializer });

bus.start();

API Reference:

SharedWorker2FactoryData:

  • Required instance: SharedWorker

  • Required bus: ClientBus

  • Optional binary: Boolean — enable binary-specific transfer optimization. Default: false

  • Refurning: FactoryData

server case / worker side

First of all: please read the docs about SharedWorker API. There is no magic.

// @ts-nocheck
declare let onconnect: (event: MessageEvent) => void;

import { ServerBus } from "@shinka-rpc/core";
import { SharedWorkerServer } from "@shinka-rpc/shared-worker";
import serializer from "@shinka-rpc/serializer-json";  // for example

export const server = new ServerBus({ serializer });

onconnect = SharedWorkerServer(server);

API Reference:

SharedWorkerServer:

  • Reqiored server: ServerBus

  • Optional binary: Boolean — enable binary-specific transfer optimization. Default: false

  • Refurning: (e: MessageEvent) => void