UNPKG

4.37 kBMarkdownView Raw
1# Stagehand [![Build Status](https://travis-ci.com/dfreeman/stagehand.svg?branch=master)](https://travis-ci.com/dfreeman/stagehand)
2
3Stagehand provides a type-safe means of coordinating communication across an evented message-passing boundary, such as a web worker or a Node.js child process.
4
5## Example
6
7The following is a quick example of how you might spin up a child process in Node to perform expensive computations:
8
9```ts
10// greeting-worker.js
11import { launch } from 'stagehand/lib/adapters/child-process';
12
13class GreetingWorker {
14 veryExpensiveGreeting(target) {
15 // The body of this method (and any others defined on the object passed to
16 // `launch`) might be arbitrarily expensive synchronous or async work.
17 return `Hello, ${target}!`;
18 }
19}
20
21launch(new GreetingWorker());
22```
23
24```ts
25// main.js
26import { fork } from 'child_process';
27import { connect } from 'stagehand/lib/adapters/child-process';
28
29let worker = await connect(fork('./greeting-worker'));
30let greeting = await worker.veryExpensiveGreeting('World');
31
32console.log(greeting); // 'Hello, World!'
33```
34
35## API
36
37The main `stagehand` module exports three functions: `launch`, `connect` and `disconnect`. These functions all center around the concept of a `MessageEndpoint`, which is the core interface Stagehand uses for communication.
38
39### `launch<T>(endpoint: MessageEndpoint, implementation: Implementation<T>)`
40
41This function is called to expose a backing implementation object across the given endpoint. Typically you'll call this from your child process/web worker/otherwise-somehow-secondary context in order to begin servicing commands from the parent.
42
43In general, all functions on the implementation object passed to `launch` will be exposed on the corresponding object on the `connect` side, but made async where appropriate. This means that all return values from methods on `implementation` will be promises, but also that functions _passed in_ to methods on the backing implementation will be asynchronous as well.
44
45If you're using TypeScript, you'll find this invariant enforced by the signature of `launch`, i.e.
46
47```ts
48// This is valid:
49launch(endpoint, {
50 doSomething(callback: () => Promise<number>) {
51 // Because `callback` is async, can communicate back to the parent when
52 // called in order to get the return value
53 }
54});
55
56// But this is not:
57launch(endpoint, {
58 doSomething(callback: () => number) {
59 // There's no way for `callback` to synchronously get a return value
60 // from the function originally passed on the parent side
61 }
62});
63```
64
65### `connect<T>(endpoint: MessageEndpoint): Promise<Remote<T>>`
66
67This function is called to connect to a backing implementation that was set up on the other end of the given message endpoint. It returns a promise that will ultimately resolve to a value that exposes the same methods as the backing implementation.
68
69When calling these methods, their results will always be promises that resolve or reject based on the return value or thrown execption when that method is called on the implementation side.
70
71### `disconnect<T>(remote: Remote<T>)`
72
73Disconnects the given stagehand worker, clearing any saved state on both ends and calling `disconnect` on both halves of the corresponding `MessageEndpoint`.
74
75### `MessageEndpoint`
76
77A `MessageEndpoint` is any object with the following three methods:
78 - `onMessage(callback: (message: unknown) => void): void`
79 - `sendMessage(message: unknown): void`
80 - `disconnect(): void`
81
82Several adapters for converting common communication objects to this type are included in this library:
83 - `stagehand/lib/adapters/child-process` creates endpoints for communicating to/from processes spawned with Node.js's [`fork`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options) (see the example above)
84 - `stagehand/lib/adapters/web-worker` creates endpoints for communicating to /from [web workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API)
85 - `stagehand/lib/adapters/in-memory` create endpoints from scratch that both exist within the same process (mostly useful for testing)
86
87These adapter modules also generally expose specialized versions of `launch` and `connect` that accept appropriate objects for the domain they deal with and internally convert them using the endpoint adapters also defined there.
88