UNPKG

2.87 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __importDefault = (this && this.__importDefault) || function (mod) {
11 return (mod && mod.__esModule) ? mod : { "default": mod };
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14const stagehand_1 = __importDefault(require("./stagehand"));
15const STAGEHAND_INSTANCE = '--stagehand-instance';
16/**
17 * Given a message endpoint and a backing implementation object, listens for a connection on the
18 * given endpoint and responds to commands that come in over it. This function will typically be
19 * called in whatever secondary thread/process will be servicing commands from the primary.
20 */
21function launch(endpoint, implementation) {
22 return __awaiter(this, void 0, void 0, function* () {
23 let stagehand = new stagehand_1.default(implementation);
24 stagehand.listen(endpoint);
25 return stagehand;
26 });
27}
28exports.launch = launch;
29/**
30 * Given a message endpoint (and typically an explicit type parameter indicating the interface of
31 * the remote implementation), returns a promise that will resolve when successfully connected to
32 * the implementation on the other side of that endpoint.
33 *
34 * The resulting object will have methods defined on it that correspond to those of the backing
35 * implementation, returning a promise for the eventual result of that method.
36 */
37function connect(endpoint) {
38 return __awaiter(this, void 0, void 0, function* () {
39 let stagehand = new stagehand_1.default();
40 let { name, methods } = yield stagehand.connect(endpoint);
41 class StagehandRemote {
42 }
43 for (let method of methods) {
44 Object.defineProperty(StagehandRemote.prototype, method, {
45 value: (...args) => stagehand.call(method, args)
46 });
47 }
48 Object.defineProperty(StagehandRemote.prototype, STAGEHAND_INSTANCE, { value: stagehand });
49 Object.defineProperty(StagehandRemote, 'name', { value: `StagehandRemote<${name}>` });
50 return new StagehandRemote();
51 });
52}
53exports.connect = connect;
54/**
55 * Given a remote object (as returned from `connect`), disconnects from the source and closes the connection.
56 */
57function disconnect(remote) {
58 remote[STAGEHAND_INSTANCE].disconnect();
59}
60exports.disconnect = disconnect;