@zerodensity/realityhub-api 1.5.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 CHANGED
@@ -28,15 +28,62 @@ 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
  });
34
42
  ```
35
43
 
44
+ ### Listing the engines
45
+
46
+ ```js
47
+ brokerClient.api.hub.reality_world.listEngines()
48
+ .then((engines) => {
49
+ console.log(engines);
50
+ })
51
+ .catch((ex) => console.trace(ex));
52
+ ```
53
+
54
+ ### Listing the nodes running on an engine
55
+
56
+ ```js
57
+ brokerClient.api.hub.reality_world.getNodes(8 /* id of the engine */)
58
+ .then((nodes) => {
59
+ console.log(nodes);
60
+ })
61
+ .catch((ex) => console.trace(ex));
62
+ ```
63
+
36
64
  ### Calling a Reality Node's Function
37
65
 
66
+ You can use `callNodeFunction(params[, engineIds])` to call a node function. `engineIds` is an array
67
+ of engine IDs. If `engineIds` is not supplied then the function will be called on all the engines.
68
+
38
69
  ```js
39
- // Node_0's PLAY function is called
70
+ /**
71
+ * @typedef FunctionProperty
72
+ * @type {object}
73
+ * @property {string} NodePath
74
+ * @property {string} PropertyPath
75
+ * @property {object} payload
76
+ * @property {*} payload.value
77
+ */
78
+
79
+ /**
80
+ * Node_0's PLAY function is called
81
+ * @param {object} params
82
+ * @param {string} params.NodePath
83
+ * @param {string} params.PropertyPath
84
+ * @param {FunctionProperty[]} [params.functionProperties] - (optional)
85
+ * @param {number} [engineIds] - (optional)
86
+ */
40
87
  brokerClient.api.hub.reality_world.callNodeFunction({
41
88
  NodePath: 'Mixer Default',
42
89
  PropertyPath: 'Default//DoTransition/0',
@@ -54,7 +101,21 @@ brokerClient.api.hub.reality_world.callNodeFunction({
54
101
 
55
102
  ### Setting a Reality Node's Property Value
56
103
 
104
+ You can use `setNodeProperty(params[, engineIds])` to set a node's property value. `engineIds` is an
105
+ array of engine IDs. If `engineIds` not supplied then all of the engines will receive the set node
106
+ property command.
107
+
57
108
  ```js
109
+ /**
110
+ * @param {object} params
111
+ * @param {string} params.NodePath
112
+ * @param {string} params.PropertyPath
113
+ * @param {*} params.Value
114
+ * @param {number} [params.Delay=0] - (optional) Delay in seconds
115
+ * @param {number} [params.Duration=0] - (optional) Duration in seconds
116
+ * @param {Jump|Linear|EaseIn|EaseOut|EaseInOut} [params.InterpType=Jump] - (optional)
117
+ * @param {number} [engineIds] - (optional)
118
+ */
58
119
  brokerClient.api.hub.reality_world.setNodeProperty({
59
120
  NodePath: 'Mixer Default',
60
121
  PropertyPath: 'Overlay Options//OverlayOpacity/0',
@@ -67,14 +128,13 @@ brokerClient.api.hub.reality_world.setNodeProperty({
67
128
  ### Subscribing to Property Change Event
68
129
 
69
130
  ```js
70
- // nodepropertyupdate event has a special sytax. You have to supply an engine name in order to
71
- // subscribe to the event. The engine name and event name is delitimed by 2 colons.
131
+ // nodepropertyupdate event has a special sytax. You have to supply an engine id and the node's path in order to
132
+ // subscribe to its property change events. The engine id, event name and node path are delitimed by 2 colons.
72
133
 
73
134
  brokerClient.api.hub.reality_world.listEngines()
74
- .then((engines) => engines[0].name)
75
- .then((engineName) => {
76
- brokerClient.api.hub.reality_world.on(`nodepropertyupdate::${engineName}`, (eventData) => {
77
- if (eventData.NodePath !== 'Mixer Default') return;
135
+ .then((engines) => engines[0].id)
136
+ .then((engineId) => {
137
+ brokerClient.api.hub.reality_world.on(`nodepropertyupdate::${engineId}::Mixer_0`, (eventData) => {
78
138
  if (eventData.property.PropertyPath !== 'Overlay Options//OverlayOpacity/0') return;
79
139
 
80
140
  console.log('New value of overlay opacity is', eventData.property.Value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerodensity/realityhub-api",
3
- "version": "1.5.0",
3
+ "version": "1.7.0",
4
4
  "description": "RealityHub API Javascript Implementation",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -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
  }
@@ -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
  });
@@ -123,9 +126,11 @@ module.exports = class BrokerClient extends BrokerBase {
123
126
  url = { hostname: options.host, port: options.port };
124
127
  }
125
128
 
129
+ const scheme = url.protocol === 'https:' ? 'wss' : 'ws';
130
+
126
131
  const webSocketURL = url.port
127
- ? `ws://${url.hostname}:${url.port}${this.webSocketURL}`
128
- : `ws://${url.hostname}${this.webSocketURL}`;
132
+ ? `${scheme}://${url.hostname}:${url.port}${this.webSocketURL}`
133
+ : `${scheme}://${url.hostname}${this.webSocketURL}`;
129
134
 
130
135
  this.socket = new ws(webSocketURL);
131
136
  this.addSocketListeners();
@@ -208,6 +213,11 @@ module.exports = class BrokerClient extends BrokerBase {
208
213
  try {
209
214
  await this.handleMessage(event.data, this.socket);
210
215
  } catch (ex) {
216
+ if (this.errorEmittingEnabled) {
217
+ this.emit('error', ex);
218
+ return;
219
+ }
220
+
211
221
  console.trace(ex);
212
222
  }
213
223
  }
@@ -247,7 +257,11 @@ module.exports = class BrokerClient extends BrokerBase {
247
257
  try {
248
258
  entry.eventHandler(...message.data);
249
259
  } catch (ex) {
250
- this.logger.warn(ex);
260
+ if (this.errorEmittingEnabled) {
261
+ this.emit('error', ex);
262
+ } else {
263
+ this.logger.warn(ex);
264
+ }
251
265
  } finally {
252
266
  if (entry.once) {
253
267
  this.unsubscribeFromAPIEvent(subscribedEvent, entry.eventHandler);
@@ -397,6 +411,11 @@ module.exports = class BrokerClient extends BrokerBase {
397
411
  }
398
412
  }
399
413
  } catch (ex) {
414
+ if (this.errorEmittingEnabled) {
415
+ this.emit('error', ex);
416
+ return;
417
+ }
418
+
400
419
  if (ex.code === 'TIMEOUT') {
401
420
  console.warn('Message timed out.');
402
421
  console.log(message);
@@ -511,6 +530,11 @@ module.exports = class BrokerClient extends BrokerBase {
511
530
  }
512
531
  }
513
532
  } catch (ex) {
533
+ if (this.errorEmittingEnabled) {
534
+ this.emit('error', ex);
535
+ return;
536
+ }
537
+
514
538
  console.trace(ex.message);
515
539
  }
516
540
  }
@@ -525,7 +549,11 @@ module.exports = class BrokerClient extends BrokerBase {
525
549
  }
526
550
 
527
551
  if (!this.isDuplicate) {
528
- this.logger.trace(err);
552
+ if (this.errorEmittingEnabled) {
553
+ this.emit('error', err);
554
+ } else {
555
+ this.logger.trace(err);
556
+ }
529
557
  }
530
558
 
531
559
  if (!this.isDuplicate) {
@@ -594,6 +622,11 @@ module.exports = class BrokerClient extends BrokerBase {
594
622
 
595
623
  return Promise.all(promises);
596
624
  } catch (ex) {
625
+ if (this.errorEmittingEnabled) {
626
+ this.emit('error', ex);
627
+ return;
628
+ }
629
+
597
630
  console.trace(ex.message);
598
631
  }
599
632
  }
@@ -635,6 +668,11 @@ module.exports = class BrokerClient extends BrokerBase {
635
668
  super.destroy();
636
669
  }
637
670
  } catch (ex) {
671
+ if (this.errorEmittingEnabled) {
672
+ this.emit('error', ex);
673
+ return;
674
+ }
675
+
638
676
  console.trace(ex.message);
639
677
  }
640
678
  }
@@ -671,6 +709,12 @@ module.exports = class BrokerClient extends BrokerBase {
671
709
  return this.registerHandlersToRemote(remote);
672
710
  }
673
711
 
712
+ /**
713
+ * @typedef {function} ErrorCallback
714
+ * @param {Error} error
715
+ * @param {BrokerClient} brokerClient
716
+ /
717
+
674
718
  /**
675
719
  * Third-party modules can use this method to initialize a BrokerClient
676
720
  * and register themselves to RealityHub.
@@ -689,12 +733,19 @@ module.exports = class BrokerClient extends BrokerBase {
689
733
  * @param {{ host: string, port: number }} params.hub RealityHub connection parameters
690
734
  * @param {string} params.hub.host RealityHub hostname or IP address
691
735
  * @param {string} params.hub.port RealityHub port
736
+ * @param {ErrorCallback} [params.onError] Error handler callback, BrokerClient will catch the errors if not specified
692
737
  * @returns {Promise<BrokerClient, Error>} A BrokerClient instance.
693
738
  */
694
739
  static async initModule(params) {
695
- const { moduleName, serverURL, hub, webSocketURL = '/core', clientModuleName, menuTitle } = params;
740
+ const { moduleName, serverURL, hub, webSocketURL = '/core', clientModuleName, menuTitle, onError} = params;
696
741
  const hubClient = new BrokerClient({ moduleName, webSocketURL });
697
742
 
743
+ if (typeof onError === 'function') {
744
+ hubClient.on('error', (error) => {
745
+ onError(error, hubClient);
746
+ });
747
+ }
748
+
698
749
  hubClient.connect(hub);
699
750
  await hubClient.getConnectPromise();
700
751
  if (!serverURL) return hubClient;