stagehand

  • Version 1.0.1
  • Published
  • 34.8 kB
  • 1 dependency
  • MIT license

Install

npm i stagehand
yarn add stagehand
pnpm add stagehand

Overview

A type-safe library for communicating between JS processes, workers, or other message-passing boundaries.

Index

Variables

variable STAGEHAND_INSTANCE

const STAGEHAND_INSTANCE: string;

    Functions

    function connect

    connect: <T>(endpoint: MessageEndpoint) => Promise<Remote<T>>;
    • Given a message endpoint (and typically an explicit type parameter indicating the interface of the remote implementation), returns a promise that will resolve when successfully connected to the implementation on the other side of that endpoint.

      The resulting object will have methods defined on it that correspond to those of the backing implementation, returning a promise for the eventual result of that method.

    function disconnect

    disconnect: <T>(remote: Remote<T>) => void;
    • Given a remote object (as returned from connect), disconnects from the source and closes the connection.

    function launch

    launch: <T>(
    endpoint: MessageEndpoint,
    implementation: Implementation<T>
    ) => Promise<Stagehand<T>>;
    • Given a message endpoint and a backing implementation object, listens for a connection on the given endpoint and responds to commands that come in over it. This function will typically be called in whatever secondary thread/process will be servicing commands from the primary.

    Interfaces

    interface MessageEndpoint

    interface MessageEndpoint {}
    • The minimal interface needed for stagehand communication. The adapters directory contains utilities for converting common types (e.g. Node's ChildProcess or the Web's MessagePort) into MessageEndpoints.

    method disconnect

    disconnect: () => void;

      method onMessage

      onMessage: (callback: (message: unknown) => void) => void;

        method sendMessage

        sendMessage: (message: unknown) => void;

          Type Aliases

          type Implementation

          type Implementation<T> = HandlerType<T>;
          • The necessarily implementation type of an intended remote interface T. All methods in Implementation<T> are allowed to return promises even if the corresponding method in T does not, but Implementation<T> also requires that all incoming callbacks be async, since they correspond to a remote type themselves.

          type Remote

          type Remote<T> = RemoteType<MethodsOnly<T>> & {
          [STAGEHAND_INSTANCE]: Stagehand<T>;
          };
          • The remote implementation of a type T. It only includes Ts methods, and the return values of those methods are all made async if they weren't already.

          Package Files (1)

          Dependencies (1)

          Dev Dependencies (13)

          Peer Dependencies (0)

          No peer dependencies.

          Badge

          To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

          You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/stagehand.

          • Markdown
            [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/stagehand)
          • HTML
            <a href="https://www.jsdocs.io/package/stagehand"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>