Appearance
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 SharedWorker → Worker → SyntheticWorker 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
| Parameter | Type |
|---|---|
url | string | URL |
options? | TRpcClientWorkerOptions |
Returns
Overrides
Properties
| Property | Modifier | Type | Inherited from | Defined in |
|---|---|---|---|---|
DEFAULT_EVENTS | static | string[] | RpcClient.DEFAULT_EVENTS | src/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 Parameter | Default type |
|---|---|
R | any |
Parameters
| Parameter | Type | Description |
|---|---|---|
method | string | Method name exported by RpcConnect. |
args? | any[] | Arguments to pass to the method. |
options? | TRpcClientRequestOptions | Optional timeout / abort signal / event names. |
Returns
Promise<R>
Inherited from
clear()
ts
clear(): void;Defined in: src/EventEmitter/index.ts:53
Removes all listeners without destroying the emitter.
Returns
void
Inherited from
destroy()
ts
destroy(): void;Defined in: src/rpc/RpcClient.ts:63
Destroys the client and releases resources.
Returns
void
Inherited from
emit()
ts
protected emit(data): void;Defined in: src/EventEmitter/index.ts:27
Parameters
| Parameter | Type |
|---|---|
data | any |
Returns
void
Inherited from
on()
ts
on(
event,
args?,
options?): TRpcUnsubscribe;Defined in: src/rpc/RpcClient.ts:72
Subscribes to a server-side event stream.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Event name. |
args? | any[] | Arguments forwarded to the server subscription handler. |
options? | TRpcClientRequestOptions | - |
Returns
An unsubscribe function.
Inherited from
once()
ts
once(...listeners): () => void;Defined in: src/EventEmitter/index.ts:46
Parameters
| Parameter | Type |
|---|---|
...listeners | IEventEmitterListener<any>[] |
Returns
() => void
Inherited from
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
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
| Parameter | Type |
|---|---|
...listeners | IEventEmitterListener<any>[] |
Returns
() => void
Inherited from
taskCount()
ts
taskCount(): number;Defined in: src/rpc/RpcClient.ts:59
Returns the number of currently pending calls.
Returns
number
Inherited from
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
| Parameter | Type |
|---|---|
url | string | URL |
options? | TRpcClientWorkerPoolOptions |
Returns
Overrides
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 Parameter | Default type |
|---|---|
A extends any[] | any[] |
R | any |
Parameters
| Parameter | Type |
|---|---|
...args | [string, A, TRpcClientRequestOptions] |
Returns
Promise<R>
Inherited from
clear()
ts
clear(): void;Defined in: src/EventEmitter/index.ts:53
Removes all listeners without destroying the emitter.
Returns
void
Inherited from
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
emit()
ts
protected emit(data): void;Defined in: src/EventEmitter/index.ts:27
Parameters
| Parameter | Type |
|---|---|
data | any |
Returns
void
Inherited from
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
The selected Client instance.
Inherited from
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
count | number | 0 | Number of worker instances to pre-create. |
Returns
Array of the created/existing worker instances.
Inherited from
on()
ts
on(...args): TRpcUnsubscribe;Defined in: src/rpc/RpcClientPool.ts:127
Routes the subscription to the least-busy worker.
Parameters
| Parameter | Type |
|---|---|
...args | [string, any[], TRpcClientRequestOptions] |
Returns
Inherited from
once()
ts
once(...listeners): () => void;Defined in: src/EventEmitter/index.ts:46
Parameters
| Parameter | Type |
|---|---|
...listeners | IEventEmitterListener<any>[] |
Returns
() => void
Inherited from
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
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
| Parameter | Type |
|---|---|
...listeners | IEventEmitterListener<any>[] |
Returns
() => void
Inherited from
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
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
| Parameter | Type |
|---|---|
options | | TRpcConnectOptions | TRpcConnectOptionsInit |
Returns
Inherited from
Properties
| Property | Modifier | Type | Default value | Inherited from | Defined in |
|---|---|---|---|---|---|
interrupt | static | <A>(millis?, params?) => Promise<A> | wait | RpcConnect.interrupt | src/rpc/RpcConnect.ts:35 |
Methods
destroy()
ts
destroy(): void;Defined in: src/rpc/RpcConnect.ts:57
Returns
void
Inherited from
dispatch()
ts
dispatch(data): void;Defined in: src/rpc/RpcConnect.ts:61
Parameters
| Parameter | Type |
|---|---|
data | any |
Returns
void
Inherited from
dispatchEncoded()
ts
dispatchEncoded(encodedData): void;Defined in: src/rpc/RpcConnect.ts:65
Parameters
| Parameter | Type |
|---|---|
encodedData | TRpcEncodedData |
Returns
void
Inherited from
run()
ts
static run(exports): Promise<void>;Defined in: src/rpc/browser/RpcWorker/RpcConnectWorker.ts:20
Parameters
| Parameter | Type |
|---|---|
exports | Record<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
| Parameter | Type |
|---|---|
scriptURL | string | URL |
Returns
Overrides
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.
Parameters
| Parameter | Type |
|---|---|
type | string |
callback | EventListenerOrEventListenerObject |
options? | boolean | AddEventListenerOptions |
Returns
void
Inherited from
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.
Parameters
| Parameter | Type |
|---|---|
event | Event |
Returns
boolean
Inherited from
postMessage()
ts
postMessage(data): void;Defined in: src/rpc/browser/SyntheticWorker/index.ts:63
Sends a message to the port.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The data to send. |
Returns
void
The data.
Example
ts
const port = new PortPolyfill();
port.postMessage('hello'); // => 'hello'Overrides
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.
Parameters
| Parameter | Type |
|---|---|
type | string |
callback | EventListenerOrEventListenerObject |
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'— standardWorker(default)'shared'—SharedWorker(falls back toWorker→SyntheticWorker)'synthetic'— in-threadSyntheticWorker(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
| Name | Type | Description | Defined in |
|---|---|---|---|
maxWorkers? | number | Maximum 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
| Parameter | Type |
|---|---|
parent | SyntheticWorker |
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
| Parameter | Type | Description |
|---|---|---|
ctx | EventTarget | The 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));