API reference@evolu/commonWorker › MessagePort

Defined in: packages/common/src/Worker.ts:46

Typed, disposable MessagePort.

Note: There is no reliable way to detect when a port is closed or disconnected. Calling postMessage on a disposed port does not throw — it silently fails. To detect dead ports, use a heartbeat pattern where the other end periodically sends "alive" messages and stale ports are pruned after a timeout.

See

https://developer.mozilla.org/en-US/docs/Web/API/MessagePort

Extends

Extended by

Methods

[dispose]()

dispose: void;

Defined in: node_modules/@typescript/old/lib/lib.esnext.disposable.d.ts:34

Inherited from

Disposable.[dispose]

Properties

native

readonly native: NativeMessagePort<Input, Output>;

Defined in: packages/common/src/Worker.ts:86

The native underlying port for transferring via postMessage.

Prepare a port for transfer:

import {
  assertType,
  createMessageChannel,
  type NativeMessagePort,
} from "@evolu/common";

interface ConsoleMessage {
  readonly type: "ConsoleEntry";
  readonly text: string;
}

using consoleChannel = createMessageChannel<ConsoleMessage>();
const init = {
  type: "InitConsole",
  port: consoleChannel.port1.native,
};

assertType<NativeMessagePort<ConsoleMessage>, typeof init.port>();

onMessage

onMessage: ((message: Output) => void) | null;

Defined in: packages/common/src/Worker.ts:58

Handler for incoming messages. Messages are queued until this is assigned, matching native MessagePort behavior where setting onmessage implicitly calls start(). This enables safe async initialization — the sender can post messages immediately while the receiver sets up.


postMessage

readonly postMessage: (message: Input, transfer?: readonly Transferable[]) => void;

Defined in: packages/common/src/Worker.ts:47