Skip to content

fundamentool


rpc/browser

Classes

RpcClientWorker

Defined in: src/rpc/browser/RpcWorker/RpcClientWorker.ts:34

RpcClient backed by a browser Web Worker (ordinary, shared, or synthetic). Automatically falls back from SharedWorkerWorkerSyntheticWorker based on availability.

Example

ts
const client = new RpcClientWorker('/worker.js');
const result = await client.call('compute', [data]);

Extends

Constructors

Constructor
ts
new RpcClientWorker(url, options?): RpcClientWorker;

Defined in: src/rpc/browser/RpcWorker/RpcClientWorker.ts:35

Parameters
ParameterType
urlstring | URL
options?TRpcClientWorkerOptions
Returns

RpcClientWorker

Overrides

RpcClient.constructor

Properties

PropertyModifierTypeInherited fromDefined in
DEFAULT_EVENTSstaticstring[]RpcClient.DEFAULT_EVENTSsrc/rpc/RpcClient.ts:33

Methods

call()
ts
call<R>(
   method, 
   args?, 
options?): Promise<R>;

Defined in: src/rpc/RpcClient.ts:68

Calls a remote method and returns a Promise of its result.

Type Parameters
Type ParameterDefault type
Rany
Parameters
ParameterTypeDescription
methodstringMethod name exported by RpcConnect.
args?any[]Arguments to pass to the method.
options?TRpcClientRequestOptionsOptional timeout / abort signal / event names.
Returns

Promise<R>

Inherited from

RpcClient.call

clear()
ts
clear(): void;

Defined in: src/EventEmitter/index.ts:53

Removes all listeners without destroying the emitter.

Returns

void

Inherited from

RpcClient.clear

destroy()
ts
destroy(): void;

Defined in: src/rpc/RpcClient.ts:63

Destroys the client and releases resources.

Returns

void

Inherited from

RpcClient.destroy

emit()
ts
protected emit(data): void;

Defined in: src/EventEmitter/index.ts:27

Parameters
ParameterType
dataany
Returns

void

Inherited from

RpcClient.emit

on()
ts
on(
   event, 
   args?, 
   options?): TRpcUnsubscribe;

Defined in: src/rpc/RpcClient.ts:72

Subscribes to a server-side event stream.

Parameters
ParameterTypeDescription
eventstringEvent name.
args?any[]Arguments forwarded to the server subscription handler.
options?TRpcClientRequestOptions-
Returns

TRpcUnsubscribe

An unsubscribe function.

Inherited from

RpcClient.on

once()
ts
once(...listeners): () => void;

Defined in: src/EventEmitter/index.ts:46

Parameters
ParameterType
...listenersIEventEmitterListener<any>[]
Returns

() => void

Inherited from

RpcClient.once

proxy()
ts
proxy(): Promise<any>;

Defined in: src/rpc/RpcClient.ts:83

Returns a proxy object where every property is an async method call.

Returns

Promise<any>

Inherited from

RpcClient.proxy

subscribe()
ts
subscribe(...listeners): () => void;

Defined in: src/EventEmitter/index.ts:42

Registers one or more listeners; returns a function that removes them all.

Parameters
ParameterType
...listenersIEventEmitterListener<any>[]
Returns

() => void

Inherited from

RpcClient.subscribe

taskCount()
ts
taskCount(): number;

Defined in: src/rpc/RpcClient.ts:59

Returns the number of currently pending calls.

Returns

number

Inherited from

RpcClient.taskCount


RpcClientWorkerPool

Defined in: src/rpc/browser/RpcWorker/RpcClientWorkerPool.ts:17

Pool of RpcClientWorker instances that distributes calls to the least-busy worker.

Example

ts
const pool = new RpcClientWorkerPool('/worker.js', { maxWorkers: 4 });
const result = await pool.call('compute', [data]);

Extends

Constructors

Constructor
ts
new RpcClientWorkerPool(url, options?): RpcClientWorkerPool;

Defined in: src/rpc/browser/RpcWorker/RpcClientWorkerPool.ts:18

Parameters
ParameterType
urlstring | URL
options?TRpcClientWorkerPoolOptions
Returns

RpcClientWorkerPool

Overrides

RpcClientPool.constructor

Methods

call()
ts
call<A, R>(...args): Promise<R>;

Defined in: src/rpc/RpcClientPool.ts:118

Routes the call to the least-busy worker.

Type Parameters
Type ParameterDefault type
A extends any[]any[]
Rany
Parameters
ParameterType
...args[string, A, TRpcClientRequestOptions]
Returns

Promise<R>

Inherited from

RpcClientPool.call

clear()
ts
clear(): void;

Defined in: src/EventEmitter/index.ts:53

Removes all listeners without destroying the emitter.

Returns

void

Inherited from

RpcClientPool.clear

destroy()
ts
destroy(): void;

Defined in: src/rpc/RpcClientPool.ts:110

Destroys all workers in the pool and the pool itself.

Returns

void

Inherited from

RpcClientPool.destroy

emit()
ts
protected emit(data): void;

Defined in: src/EventEmitter/index.ts:27

Parameters
ParameterType
dataany
Returns

void

Inherited from

RpcClientPool.emit

getWorker()
ts
getWorker(): IRpcClient;

Defined in: src/rpc/RpcClientPool.ts:64

Returns the least-busy worker, creating a new one if a slot is free. If all slots are busy, returns the worker with the fewest active tasks.

Returns

IRpcClient

The selected Client instance.

Inherited from

RpcClientPool.getWorker

getWorkers()
ts
getWorkers(count?): IRpcClient[];

Defined in: src/rpc/RpcClientPool.ts:40

Pre-creates exactly count workers (cycling through slots up to maxWorkers). Useful in tests to ensure workers exist before attaching servers.

Parameters
ParameterTypeDefault valueDescription
countnumber0Number of worker instances to pre-create.
Returns

IRpcClient[]

Array of the created/existing worker instances.

Inherited from

RpcClientPool.getWorkers

on()
ts
on(...args): TRpcUnsubscribe;

Defined in: src/rpc/RpcClientPool.ts:127

Routes the subscription to the least-busy worker.

Parameters
ParameterType
...args[string, any[], TRpcClientRequestOptions]
Returns

TRpcUnsubscribe

Inherited from

RpcClientPool.on

once()
ts
once(...listeners): () => void;

Defined in: src/EventEmitter/index.ts:46

Parameters
ParameterType
...listenersIEventEmitterListener<any>[]
Returns

() => void

Inherited from

RpcClientPool.once

proxy()
ts
proxy(): Promise<Record<string, any>>;

Defined in: src/rpc/RpcClientPool.ts:136

Returns a proxy from the least-busy worker.

Returns

Promise<Record<string, any>>

Inherited from

RpcClientPool.proxy

subscribe()
ts
subscribe(...listeners): () => void;

Defined in: src/EventEmitter/index.ts:42

Registers one or more listeners; returns a function that removes them all.

Parameters
ParameterType
...listenersIEventEmitterListener<any>[]
Returns

() => void

Inherited from

RpcClientPool.subscribe

taskCount()
ts
taskCount(): number;

Defined in: src/rpc/RpcClientPool.ts:141

Returns the total number of pending tasks across all workers.

Returns

number

Inherited from

RpcClientPool.taskCount


RpcConnectWorker

Defined in: src/rpc/browser/RpcWorker/RpcConnectWorker.ts:19

Server-side RPC handler that runs inside a browser Worker (ordinary, shared, or synthetic). Call RpcConnectWorker.run(exports) at the top of the worker script to wire up the server.

Example

ts
// inside worker.js
await RpcConnectWorker.run({ greet: (name) => `Hello, ${name}!` });

Extends

Constructors

Constructor
ts
new RpcConnectWorker(options): RpcConnectWorker;

Defined in: src/rpc/RpcConnect.ts:45

Parameters
ParameterType
options| TRpcConnectOptions | TRpcConnectOptionsInit
Returns

RpcConnectWorker

Inherited from

RpcConnect.constructor

Properties

PropertyModifierTypeDefault valueInherited fromDefined in
interruptstatic<A>(millis?, params?) => Promise<A>waitRpcConnect.interruptsrc/rpc/RpcConnect.ts:35

Methods

destroy()
ts
destroy(): void;

Defined in: src/rpc/RpcConnect.ts:57

Returns

void

Inherited from

RpcConnect.destroy

dispatch()
ts
dispatch(data): void;

Defined in: src/rpc/RpcConnect.ts:61

Parameters
ParameterType
dataany
Returns

void

Inherited from

RpcConnect.dispatch

dispatchEncoded()
ts
dispatchEncoded(encodedData): void;

Defined in: src/rpc/RpcConnect.ts:65

Parameters
ParameterType
encodedDataTRpcEncodedData
Returns

void

Inherited from

RpcConnect.dispatchEncoded

run()
ts
static run(exports): Promise<void>;

Defined in: src/rpc/browser/RpcWorker/RpcConnectWorker.ts:20

Parameters
ParameterType
exportsRecord<string, any>
Returns

Promise<void>


SyntheticWorker

Defined in: src/rpc/browser/SyntheticWorker/index.ts:7

A polyfill for the MessagePort interface.

Extends

Constructors

Constructor
ts
new SyntheticWorker(scriptURL): SyntheticWorker;

Defined in: src/rpc/browser/SyntheticWorker/index.ts:44

Parameters
ParameterType
scriptURLstring | URL
Returns

SyntheticWorker

Overrides

PortPolyfill.constructor

Methods

addEventListener()
ts
addEventListener(
   type, 
   callback, 
   options?): void;

Defined in: node_modules/typescript/lib/lib.dom.d.ts:11569

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

MDN Reference

Parameters
ParameterType
typestring
callbackEventListenerOrEventListenerObject
options?boolean | AddEventListenerOptions
Returns

void

Inherited from

PortPolyfill.addEventListener

dispatchEvent()
ts
dispatchEvent(event): boolean;

Defined in: node_modules/typescript/lib/lib.dom.d.ts:11575

The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order.

MDN Reference

Parameters
ParameterType
eventEvent
Returns

boolean

Inherited from

PortPolyfill.dispatchEvent

postMessage()
ts
postMessage(data): void;

Defined in: src/rpc/browser/SyntheticWorker/index.ts:63

Sends a message to the port.

Parameters
ParameterTypeDescription
dataanyThe data to send.
Returns

void

The data.

Example
ts
const port = new PortPolyfill();
port.postMessage('hello'); // => 'hello'
Overrides

PortPolyfill.postMessage

removeEventListener()
ts
removeEventListener(
   type, 
   callback, 
   options?): void;

Defined in: node_modules/typescript/lib/lib.dom.d.ts:11581

The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target.

MDN Reference

Parameters
ParameterType
typestring
callbackEventListenerOrEventListenerObject
options?boolean | EventListenerOptions
Returns

void

Inherited from

PortPolyfill.removeEventListener

start()
ts
start(): void;

Defined in: src/rpc/browser/SyntheticWorker/index.ts:72

Returns

void

connect()
ts
static connect(): Promise<SyntheticWorker>;

Defined in: src/rpc/browser/SyntheticWorker/index.ts:38

Returns

Promise<SyntheticWorker>

incrementIndex()
ts
static incrementIndex(): Promise<number>;

Defined in: src/rpc/browser/SyntheticWorker/index.ts:32

Returns

Promise<number>

Type Aliases

TRpcClientWorkerOptions

ts
type TRpcClientWorkerOptions = {
  type?: TRpcClientWorkerOptionsType;
  workerOptions?: WorkerOptions;
};

Defined in: src/rpc/browser/RpcWorker/RpcClientWorker.ts:13

Options for RpcClientWorker.

Properties

type?
ts
optional type?: TRpcClientWorkerOptionsType;

Defined in: src/rpc/browser/RpcWorker/RpcClientWorker.ts:20

Which worker API to use.

  • 'ordinary' — standard Worker (default)
  • 'shared'SharedWorker (falls back to WorkerSyntheticWorker)
  • 'synthetic' — in-thread SyntheticWorker (no serialization overhead)
workerOptions?
ts
optional workerOptions?: WorkerOptions;

Defined in: src/rpc/browser/RpcWorker/RpcClientWorker.ts:22

Options forwarded to the underlying Worker / SharedWorker constructor.


TRpcClientWorkerOptionsType

ts
type TRpcClientWorkerOptionsType = "ordinary" | "shared" | "synthetic";

Defined in: src/rpc/browser/RpcWorker/RpcClientWorker.ts:10

Worker type for RpcClientWorker. Falls back automatically if the chosen type is unavailable.


TRpcClientWorkerPoolOptions

ts
type TRpcClientWorkerPoolOptions = Omit<TRpcClientWorkerOptions, "type"> & {
  maxWorkers?: number;
};

Defined in: src/rpc/browser/RpcWorker/RpcClientWorkerPool.ts:5

Options for RpcClientWorkerPool.

Type Declaration

NameTypeDescriptionDefined in
maxWorkers?numberMaximum number of workers to spawn (default: 1).src/rpc/browser/RpcWorker/RpcClientWorkerPool.ts:7

TSyntheticWorkerListener

ts
type TSyntheticWorkerListener = (parent) => void;

Defined in: src/rpc/browser/SyntheticWorker/index.ts:5

Parameters

ParameterType
parentSyntheticWorker

Returns

void

Functions

onRpcMessageProvider()

ts
function onRpcMessageProvider(ctx): (listener) => TUnsubscribe;

Defined in: src/rpc/browser/RpcWorker/onRpcMessageProvider.ts:12

Returns an onMessage subscription factory for any EventTarget that emits MessageEvents.

Parameters

ParameterTypeDescription
ctxEventTargetThe event target (e.g., Worker, MessagePort, globalThis).

Returns

A function that registers a message listener and returns an unsubscribe callback.

(listener) => TUnsubscribe

Example

ts
const onMessage = onRpcMessageProvider(worker);
const unsub = onMessage((data) => console.log(data));

MIT License