@zerodensity/realityhub-api 1.6.0 → 1.7.0
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 +8 -0
- package/package.json +1 -1
- package/src/lib/BrokerBase.js +16 -0
- package/src/lib/BrokerClient.js +53 -4
package/README.md
CHANGED
|
@@ -28,6 +28,14 @@ BrokerClient.initModule({
|
|
|
28
28
|
host: '127.0.0.1',
|
|
29
29
|
port: 3000,
|
|
30
30
|
},
|
|
31
|
+
// optional error handler
|
|
32
|
+
// brokerClient will log the error if not provided
|
|
33
|
+
onError: (error, brokerClient) => {
|
|
34
|
+
// you may destroy the instance (and retry calling initModule() later)
|
|
35
|
+
// brokerClient.destroy();
|
|
36
|
+
// it will try to recover otherwise
|
|
37
|
+
console.error(error.message);
|
|
38
|
+
}
|
|
31
39
|
}).then((brokerClient) => {
|
|
32
40
|
// brokerClient is connected to RealityHub and ready to use
|
|
33
41
|
});
|
package/package.json
CHANGED
package/src/lib/BrokerBase.js
CHANGED
|
@@ -34,6 +34,7 @@ module.exports = class BrokerBase extends EventEmitter {
|
|
|
34
34
|
this.messageTimeout = 2000;
|
|
35
35
|
|
|
36
36
|
this.overridenTimeout = NaN; // NaN = use the implementation
|
|
37
|
+
this.destroyed = false;
|
|
37
38
|
|
|
38
39
|
try {
|
|
39
40
|
if (typeof window != 'undefined' && typeof localStorage != 'undefined') {
|
|
@@ -73,6 +74,10 @@ module.exports = class BrokerBase extends EventEmitter {
|
|
|
73
74
|
this.initProxy();
|
|
74
75
|
}
|
|
75
76
|
|
|
77
|
+
get errorEmittingEnabled() {
|
|
78
|
+
return this.listeners('error').length > 0;
|
|
79
|
+
}
|
|
80
|
+
|
|
76
81
|
getMethodProxy(vendorName, moduleName, options) {
|
|
77
82
|
options = {
|
|
78
83
|
timeout: 2000,
|
|
@@ -430,6 +435,11 @@ module.exports = class BrokerBase extends EventEmitter {
|
|
|
430
435
|
try {
|
|
431
436
|
responseMessage = await onceMultiple(this, [`response::${message.id}`], this.overridenTimeout || message.timeout || this.messageTimeout);
|
|
432
437
|
} catch (ex) {
|
|
438
|
+
if (this.errorEmittingEnabled) {
|
|
439
|
+
this.emit('error', ex);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
|
|
433
443
|
const logger = this.logger || console;
|
|
434
444
|
logger.debug(`${this.moduleName} failed to send message ${message.type} to ${message.targetModuleName || ''}`);
|
|
435
445
|
return;
|
|
@@ -483,6 +493,11 @@ module.exports = class BrokerBase extends EventEmitter {
|
|
|
483
493
|
excludedClients: options.excludedClients || [],
|
|
484
494
|
})
|
|
485
495
|
.catch((err) => {
|
|
496
|
+
if (this.errorEmittingEnabled) {
|
|
497
|
+
this.emit('error', err);
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
|
|
486
501
|
console.error(`Couldn't emit ${fullyQualifiedName}`);
|
|
487
502
|
|
|
488
503
|
if (err.code !== 'TIMEOUT') {
|
|
@@ -493,5 +508,6 @@ module.exports = class BrokerBase extends EventEmitter {
|
|
|
493
508
|
|
|
494
509
|
destroy() {
|
|
495
510
|
this.removeAllListeners();
|
|
511
|
+
this.destroyed = true;
|
|
496
512
|
}
|
|
497
513
|
}
|
package/src/lib/BrokerClient.js
CHANGED
|
@@ -89,11 +89,14 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
89
89
|
* @returns {Promise}
|
|
90
90
|
*/
|
|
91
91
|
getConnectPromise() {
|
|
92
|
-
return new Promise((resolve) => {
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
93
|
const looper = setInterval(() => {
|
|
94
94
|
if (this.connected) {
|
|
95
95
|
clearInterval(looper);
|
|
96
96
|
resolve();
|
|
97
|
+
} else if (this.destroyed) {
|
|
98
|
+
clearInterval(looper);
|
|
99
|
+
reject(new Error('BrokerClient is destroyed'));
|
|
97
100
|
}
|
|
98
101
|
}, 0);
|
|
99
102
|
});
|
|
@@ -210,6 +213,11 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
210
213
|
try {
|
|
211
214
|
await this.handleMessage(event.data, this.socket);
|
|
212
215
|
} catch (ex) {
|
|
216
|
+
if (this.errorEmittingEnabled) {
|
|
217
|
+
this.emit('error', ex);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
213
221
|
console.trace(ex);
|
|
214
222
|
}
|
|
215
223
|
}
|
|
@@ -249,7 +257,11 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
249
257
|
try {
|
|
250
258
|
entry.eventHandler(...message.data);
|
|
251
259
|
} catch (ex) {
|
|
252
|
-
this.
|
|
260
|
+
if (this.errorEmittingEnabled) {
|
|
261
|
+
this.emit('error', ex);
|
|
262
|
+
} else {
|
|
263
|
+
this.logger.warn(ex);
|
|
264
|
+
}
|
|
253
265
|
} finally {
|
|
254
266
|
if (entry.once) {
|
|
255
267
|
this.unsubscribeFromAPIEvent(subscribedEvent, entry.eventHandler);
|
|
@@ -399,6 +411,11 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
399
411
|
}
|
|
400
412
|
}
|
|
401
413
|
} catch (ex) {
|
|
414
|
+
if (this.errorEmittingEnabled) {
|
|
415
|
+
this.emit('error', ex);
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
|
|
402
419
|
if (ex.code === 'TIMEOUT') {
|
|
403
420
|
console.warn('Message timed out.');
|
|
404
421
|
console.log(message);
|
|
@@ -513,6 +530,11 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
513
530
|
}
|
|
514
531
|
}
|
|
515
532
|
} catch (ex) {
|
|
533
|
+
if (this.errorEmittingEnabled) {
|
|
534
|
+
this.emit('error', ex);
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
|
|
516
538
|
console.trace(ex.message);
|
|
517
539
|
}
|
|
518
540
|
}
|
|
@@ -527,7 +549,11 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
527
549
|
}
|
|
528
550
|
|
|
529
551
|
if (!this.isDuplicate) {
|
|
530
|
-
this.
|
|
552
|
+
if (this.errorEmittingEnabled) {
|
|
553
|
+
this.emit('error', err);
|
|
554
|
+
} else {
|
|
555
|
+
this.logger.trace(err);
|
|
556
|
+
}
|
|
531
557
|
}
|
|
532
558
|
|
|
533
559
|
if (!this.isDuplicate) {
|
|
@@ -596,6 +622,11 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
596
622
|
|
|
597
623
|
return Promise.all(promises);
|
|
598
624
|
} catch (ex) {
|
|
625
|
+
if (this.errorEmittingEnabled) {
|
|
626
|
+
this.emit('error', ex);
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
|
|
599
630
|
console.trace(ex.message);
|
|
600
631
|
}
|
|
601
632
|
}
|
|
@@ -637,6 +668,11 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
637
668
|
super.destroy();
|
|
638
669
|
}
|
|
639
670
|
} catch (ex) {
|
|
671
|
+
if (this.errorEmittingEnabled) {
|
|
672
|
+
this.emit('error', ex);
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
|
|
640
676
|
console.trace(ex.message);
|
|
641
677
|
}
|
|
642
678
|
}
|
|
@@ -673,6 +709,12 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
673
709
|
return this.registerHandlersToRemote(remote);
|
|
674
710
|
}
|
|
675
711
|
|
|
712
|
+
/**
|
|
713
|
+
* @typedef {function} ErrorCallback
|
|
714
|
+
* @param {Error} error
|
|
715
|
+
* @param {BrokerClient} brokerClient
|
|
716
|
+
/
|
|
717
|
+
|
|
676
718
|
/**
|
|
677
719
|
* Third-party modules can use this method to initialize a BrokerClient
|
|
678
720
|
* and register themselves to RealityHub.
|
|
@@ -691,12 +733,19 @@ module.exports = class BrokerClient extends BrokerBase {
|
|
|
691
733
|
* @param {{ host: string, port: number }} params.hub RealityHub connection parameters
|
|
692
734
|
* @param {string} params.hub.host RealityHub hostname or IP address
|
|
693
735
|
* @param {string} params.hub.port RealityHub port
|
|
736
|
+
* @param {ErrorCallback} [params.onError] Error handler callback, BrokerClient will catch the errors if not specified
|
|
694
737
|
* @returns {Promise<BrokerClient, Error>} A BrokerClient instance.
|
|
695
738
|
*/
|
|
696
739
|
static async initModule(params) {
|
|
697
|
-
const { moduleName, serverURL, hub, webSocketURL = '/core', clientModuleName, menuTitle } = params;
|
|
740
|
+
const { moduleName, serverURL, hub, webSocketURL = '/core', clientModuleName, menuTitle, onError} = params;
|
|
698
741
|
const hubClient = new BrokerClient({ moduleName, webSocketURL });
|
|
699
742
|
|
|
743
|
+
if (typeof onError === 'function') {
|
|
744
|
+
hubClient.on('error', (error) => {
|
|
745
|
+
onError(error, hubClient);
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
|
|
700
749
|
hubClient.connect(hub);
|
|
701
750
|
await hubClient.getConnectPromise();
|
|
702
751
|
if (!serverURL) return hubClient;
|