@webqit/port-plus 0.1.8 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +990 -31
- package/dist/main.js +1 -1
- package/dist/main.js.map +3 -3
- package/package.json +1 -1
- package/src/MessagePortPlus.js +18 -23
- package/src/RelayPort.js +4 -4
- package/src/StarPort.js +34 -4
- package/test/basic.test.js +3 -0
package/package.json
CHANGED
package/src/MessagePortPlus.js
CHANGED
|
@@ -54,7 +54,7 @@ export function MessagePortPlusMixin(superClass) {
|
|
|
54
54
|
|
|
55
55
|
const proto = this.prototype;
|
|
56
56
|
|
|
57
|
-
for (const prop of portPlusMethods) {
|
|
57
|
+
for (const prop of portPlusMethods.concat('_autoStart')) {
|
|
58
58
|
const original = port[prop];
|
|
59
59
|
const plus = proto[prop];
|
|
60
60
|
|
|
@@ -283,12 +283,7 @@ export function MessagePortPlusMixin(superClass) {
|
|
|
283
283
|
|
|
284
284
|
set onmessage(v) {
|
|
285
285
|
// Auto-start?
|
|
286
|
-
|
|
287
|
-
const options = _options(this);
|
|
288
|
-
if (!portPlusMeta.get('internal_call')
|
|
289
|
-
&& options.autoStart) {
|
|
290
|
-
this.start();
|
|
291
|
-
}
|
|
286
|
+
this._autoStart();
|
|
292
287
|
|
|
293
288
|
if (typeof super.onmessage !== 'undefined') {
|
|
294
289
|
super.onmessage = v;
|
|
@@ -311,12 +306,7 @@ export function MessagePortPlusMixin(superClass) {
|
|
|
311
306
|
|
|
312
307
|
addEventListener(...args) {
|
|
313
308
|
// Auto-start?
|
|
314
|
-
|
|
315
|
-
const options = _options(this);
|
|
316
|
-
if (!portPlusMeta.get('internal_call')
|
|
317
|
-
&& options.autoStart) {
|
|
318
|
-
this.start();
|
|
319
|
-
}
|
|
309
|
+
this._autoStart();
|
|
320
310
|
|
|
321
311
|
// Add to registry
|
|
322
312
|
const garbageCollection = getGarbageCollection.call(this);
|
|
@@ -345,17 +335,12 @@ export function MessagePortPlusMixin(superClass) {
|
|
|
345
335
|
|
|
346
336
|
postMessage(message, transferOrOptions = {}) {
|
|
347
337
|
// Auto-start?
|
|
348
|
-
|
|
349
|
-
const options = _options(this);
|
|
350
|
-
if (!portPlusMeta.get('internal_call')
|
|
351
|
-
&& options.autoStart) {
|
|
352
|
-
this.start();
|
|
353
|
-
}
|
|
338
|
+
this._autoStart();
|
|
354
339
|
|
|
355
340
|
// Update readyState
|
|
356
341
|
const readyStateInternals = getReadyStateInternals.call(this);
|
|
357
342
|
readyStateInternals.messaging.state = true;
|
|
358
|
-
readyStateInternals.messaging.resolve();
|
|
343
|
+
readyStateInternals.messaging.resolve(this);
|
|
359
344
|
|
|
360
345
|
// Format payload if not yet in the ['.wq'] format
|
|
361
346
|
let _relayedFrom;
|
|
@@ -377,6 +362,7 @@ export function MessagePortPlusMixin(superClass) {
|
|
|
377
362
|
};
|
|
378
363
|
|
|
379
364
|
// If client-server mode, wait for open ready state
|
|
365
|
+
const options = _options(this);
|
|
380
366
|
if (options.postAwaitsOpen) readyStateInternals.open.promise.then(post);
|
|
381
367
|
else post();
|
|
382
368
|
}
|
|
@@ -519,6 +505,15 @@ export function MessagePortPlusMixin(superClass) {
|
|
|
519
505
|
return readyStateInternals[query].promise;
|
|
520
506
|
}
|
|
521
507
|
|
|
508
|
+
_autoStart() {
|
|
509
|
+
const portPlusMeta = _meta(this);
|
|
510
|
+
const options = _options(this);
|
|
511
|
+
if (!portPlusMeta.get('internal_call')
|
|
512
|
+
&& options.autoStart) {
|
|
513
|
+
this.start();
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
522
517
|
start() {
|
|
523
518
|
const readyStateInternals = getReadyStateInternals.call(this);
|
|
524
519
|
if (readyStateInternals.open.state) return;
|
|
@@ -528,7 +523,7 @@ export function MessagePortPlusMixin(superClass) {
|
|
|
528
523
|
const readyStateOpen = () => {
|
|
529
524
|
if (readyStateInternals.open.state) return;
|
|
530
525
|
readyStateInternals.open.state = true;
|
|
531
|
-
readyStateInternals.open.resolve();
|
|
526
|
+
readyStateInternals.open.resolve(this);
|
|
532
527
|
|
|
533
528
|
const openEvent = new MessageEventPlus(null, { type: 'open' });
|
|
534
529
|
this._dispatchEvent
|
|
@@ -607,7 +602,7 @@ export function MessagePortPlusMixin(superClass) {
|
|
|
607
602
|
? this._close(...args)
|
|
608
603
|
: super.close(...args);
|
|
609
604
|
|
|
610
|
-
readyStateInternals.close.resolve();
|
|
605
|
+
readyStateInternals.close.resolve(this);
|
|
611
606
|
|
|
612
607
|
const openEvent = new MessageEventPlus(null, { type: 'close' });
|
|
613
608
|
this._dispatchEvent
|
|
@@ -744,7 +739,7 @@ export function getReadyStateInternals() {
|
|
|
744
739
|
const portPlusMeta = _meta(this);
|
|
745
740
|
if (!portPlusMeta.has('readystate_registry')) {
|
|
746
741
|
const $ref = (o) => {
|
|
747
|
-
o.promise = new Promise((res, rej) => (o.resolve =
|
|
742
|
+
o.promise = new Promise((res, rej) => (o.resolve = res, o.reject = rej));
|
|
748
743
|
return o;
|
|
749
744
|
};
|
|
750
745
|
portPlusMeta.set('readystate_registry', {
|
package/src/RelayPort.js
CHANGED
|
@@ -6,8 +6,8 @@ export class RelayPort extends StarPort {
|
|
|
6
6
|
#channelSpec;
|
|
7
7
|
get channelSpec() { return this.#channelSpec; }
|
|
8
8
|
|
|
9
|
-
constructor(channel = null) {
|
|
10
|
-
super();
|
|
9
|
+
constructor(channel = null, options = {}) {
|
|
10
|
+
super(options);
|
|
11
11
|
if (typeof channel === 'string') {
|
|
12
12
|
channel = { from: channel, to: channel };
|
|
13
13
|
} else if (_isObject(channel)) {
|
|
@@ -20,14 +20,14 @@ export class RelayPort extends StarPort {
|
|
|
20
20
|
this.#channelSpec = channel;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
addPort(portPlus, { resolveMessage = null } = {}) {
|
|
23
|
+
addPort(portPlus, { resolveMessage = null, ...options } = {}) {
|
|
24
24
|
const $resolveMessage = (data, ...args) => {
|
|
25
25
|
if (resolveMessage) return resolveMessage(data, ...args);
|
|
26
26
|
return data;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
// Add port and forward its messages back to this relay instance
|
|
30
|
-
const unadd = super.addPort(portPlus,
|
|
30
|
+
const unadd = super.addPort(portPlus, options);
|
|
31
31
|
const unforward = portPlus.relay({ channel: this.#channelSpec, to: this, bidirectional: false/* IMPORTANT */, resolveMessage: $resolveMessage });
|
|
32
32
|
|
|
33
33
|
const messageType_ping = this.#channelSpec.from
|
package/src/StarPort.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
MessagePortPlus,
|
|
3
|
+
_meta,
|
|
4
|
+
_options,
|
|
5
|
+
getReadyStateInternals,
|
|
6
|
+
} from './MessagePortPlus.js';
|
|
2
7
|
|
|
3
8
|
export class StarPort extends MessagePortPlus {
|
|
4
9
|
|
|
@@ -8,8 +13,8 @@ export class StarPort extends MessagePortPlus {
|
|
|
8
13
|
|
|
9
14
|
[Symbol.iterator]() { return this.#ports[Symbol.iterator](); }
|
|
10
15
|
|
|
11
|
-
constructor({ autoClose = true } = {}) {
|
|
12
|
-
super({ autoStart
|
|
16
|
+
constructor({ autoStart = true, postAwaitsOpen = false, autoClose = true } = {}) {
|
|
17
|
+
super({ autoStart, postAwaitsOpen, autoClose });
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
addPort(portPlus, { enableBubbling = true } = {}) {
|
|
@@ -17,6 +22,13 @@ export class StarPort extends MessagePortPlus {
|
|
|
17
22
|
throw new TypeError('Port must be a WQMessagePort instance.');
|
|
18
23
|
}
|
|
19
24
|
|
|
25
|
+
const readyStateInternals = getReadyStateInternals.call(this);
|
|
26
|
+
|
|
27
|
+
if (readyStateInternals.close.state) {
|
|
28
|
+
const starPortName = this.constructor.name;
|
|
29
|
+
throw new Error(`Cannot add port to ${starPortName}. ${starPortName} is closed.`);
|
|
30
|
+
}
|
|
31
|
+
|
|
20
32
|
if (this.#ports.has(portPlus)) return;
|
|
21
33
|
this.#ports.add(portPlus); // @ORDER: 1
|
|
22
34
|
|
|
@@ -34,7 +46,7 @@ export class StarPort extends MessagePortPlus {
|
|
|
34
46
|
|
|
35
47
|
const cleanup = () => {
|
|
36
48
|
if (!this.#ports.has(portPlus)) return;
|
|
37
|
-
|
|
49
|
+
|
|
38
50
|
this.#ports.delete(portPlus);
|
|
39
51
|
|
|
40
52
|
if (enableBubbling
|
|
@@ -66,9 +78,27 @@ export class StarPort extends MessagePortPlus {
|
|
|
66
78
|
}
|
|
67
79
|
}
|
|
68
80
|
|
|
81
|
+
_autoStart() {} // Must be present to do nothing
|
|
82
|
+
|
|
83
|
+
start() {
|
|
84
|
+
const readyStateInternals = getReadyStateInternals.call(this);
|
|
85
|
+
|
|
86
|
+
if (readyStateInternals.open.state) return;
|
|
87
|
+
readyStateInternals.open.state = true;
|
|
88
|
+
|
|
89
|
+
readyStateInternals.open.resolve(this);
|
|
90
|
+
}
|
|
91
|
+
|
|
69
92
|
close(...args) {
|
|
93
|
+
const readyStateInternals = getReadyStateInternals.call(this);
|
|
94
|
+
|
|
95
|
+
if (readyStateInternals.close.state) return;
|
|
96
|
+
readyStateInternals.close.state = true;
|
|
97
|
+
|
|
70
98
|
for (const portPlus of this.#ports) {
|
|
71
99
|
portPlus.close?.(...args);
|
|
72
100
|
}
|
|
101
|
+
|
|
102
|
+
readyStateInternals.close.resolve(this);
|
|
73
103
|
}
|
|
74
104
|
}
|