1 | "use strict";
|
2 | var __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 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const __1 = require("..");
|
12 | function connectLocal(handler) {
|
13 | return __awaiter(this, void 0, void 0, function* () {
|
14 | let [one, two] = endpointPair();
|
15 | __1.launch(one, handler);
|
16 | return __1.connect(two);
|
17 | });
|
18 | }
|
19 | exports.connectLocal = connectLocal;
|
20 | function endpointPair() {
|
21 | let one = new InMemoryEndpoint();
|
22 | let two = new InMemoryEndpoint();
|
23 | one.connect(two);
|
24 | two.connect(one);
|
25 | return [one, two];
|
26 | }
|
27 | exports.endpointPair = endpointPair;
|
28 | class InMemoryEndpoint {
|
29 | constructor() {
|
30 | this.listeners = [];
|
31 | }
|
32 | connect(other) {
|
33 | this.other = other;
|
34 | }
|
35 | onMessage(callback) {
|
36 | this.listeners.push(callback);
|
37 | }
|
38 | sendMessage(message) {
|
39 | if (this.other) {
|
40 | this.other.dispatchMessage(message);
|
41 | }
|
42 | }
|
43 | disconnect() {
|
44 | this.other = undefined;
|
45 | this.listeners = [];
|
46 | }
|
47 | dispatchMessage(message) {
|
48 | for (let listener of this.listeners) {
|
49 | listener(message);
|
50 | }
|
51 | }
|
52 | }
|