unetjs 4.0.1 → 4.0.2
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/dist/cjs/unet.cjs +54 -46
- package/dist/esm/unet.js +54 -46
- package/dist/unetjs.js +53 -45
- package/dist/unetjs.js.map +1 -1
- package/dist/unetjs.min.js +1 -1
- package/dist/unetjs.min.js.map +1 -1
- package/package.json +1 -1
package/dist/cjs/unet.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/* unet.js v4.0.
|
|
1
|
+
/* unet.js v4.0.2 2025-05-19T10:05:53.629Z */
|
|
2
2
|
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
|
-
/* fjage.js v2.
|
|
5
|
+
/* fjage.js v2.1.0 */
|
|
6
6
|
|
|
7
7
|
const isBrowser =
|
|
8
8
|
typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
@@ -694,6 +694,7 @@ class Message {
|
|
|
694
694
|
* @param {number} [opts.queueSize=128] - size of the queue of received messages that haven't been consumed yet
|
|
695
695
|
* @param {number} [opts.timeout=1000] - timeout for fjage level messages in ms
|
|
696
696
|
* @param {boolean} [opts.returnNullOnFailedResponse=true] - return null instead of throwing an error when a parameter is not found
|
|
697
|
+
* @param {boolean} [opts.cancelPendingOnDisconnect=false] - cancel pending requests on disconnect
|
|
697
698
|
*/
|
|
698
699
|
class Gateway {
|
|
699
700
|
|
|
@@ -712,9 +713,10 @@ class Gateway {
|
|
|
712
713
|
this._keepAlive = opts.keepAlive; // reconnect if connection gets closed/errored
|
|
713
714
|
this._queueSize = opts.queueSize; // size of queue
|
|
714
715
|
this._returnNullOnFailedResponse = opts.returnNullOnFailedResponse; // null or error
|
|
716
|
+
this._cancelPendingOnDisconnect = opts.cancelPendingOnDisconnect; // cancel pending requests on disconnect
|
|
715
717
|
this.pending = {}; // msgid to callback mapping for pending requests to server
|
|
716
718
|
this.subscriptions = {}; // hashset for all topics that are subscribed
|
|
717
|
-
this.
|
|
719
|
+
this.listeners = {}; // list of callbacks that want to listen to incoming messages
|
|
718
720
|
this.eventListeners = {}; // external listeners wanting to listen internal events
|
|
719
721
|
this.queue = []; // incoming message queue
|
|
720
722
|
this.connected = false; // connection status
|
|
@@ -731,19 +733,37 @@ class Gateway {
|
|
|
731
733
|
* @param {Object|Message|string} val - value to be sent to the listeners
|
|
732
734
|
*/
|
|
733
735
|
_sendEvent(type, val) {
|
|
734
|
-
if (Array.isArray(this.eventListeners[type]))
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
}
|
|
736
|
+
if (!Array.isArray(this.eventListeners[type])) return;
|
|
737
|
+
this.eventListeners[type].forEach(l => {
|
|
738
|
+
if (l && {}.toString.call(l) === '[object Function]'){
|
|
739
|
+
try {
|
|
740
|
+
l(val);
|
|
741
|
+
} catch (error) {
|
|
742
|
+
console.warn('Error in event listener : ' + error);
|
|
742
743
|
}
|
|
743
|
-
}
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* Sends the message to all registered receivers.
|
|
750
|
+
*
|
|
751
|
+
* @private
|
|
752
|
+
* @param {Message} msg
|
|
753
|
+
* @returns {boolean} - true if the message was consumed by any listener
|
|
754
|
+
*/
|
|
755
|
+
_sendReceivers(msg) {
|
|
756
|
+
for (var lid in this.listeners){
|
|
757
|
+
try {
|
|
758
|
+
if (this.listeners[lid] && this.listeners[lid](msg)) return true;
|
|
759
|
+
} catch (error) {
|
|
760
|
+
console.warn('Error in listener : ' + error);
|
|
761
|
+
}
|
|
744
762
|
}
|
|
763
|
+
return false;
|
|
745
764
|
}
|
|
746
765
|
|
|
766
|
+
|
|
747
767
|
/**
|
|
748
768
|
* @private
|
|
749
769
|
* @param {string} data - stringfied JSON data received from the master container to be processed
|
|
@@ -770,32 +790,10 @@ class Gateway {
|
|
|
770
790
|
if (!msg) return;
|
|
771
791
|
this._sendEvent('rxmsg', msg);
|
|
772
792
|
if ((msg.recipient == this.aid.toJSON() )|| this.subscriptions[msg.recipient]) {
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
if (this.eventListeners['message'][i](msg)) {
|
|
778
|
-
consumed = true;
|
|
779
|
-
break;
|
|
780
|
-
}
|
|
781
|
-
} catch (error) {
|
|
782
|
-
console.warn('Error in message listener : ' + error);
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
// iterate over internal callbacks, until one consumes the message
|
|
787
|
-
for (var key in this.listener){
|
|
788
|
-
// callback returns true if it has consumed the message
|
|
789
|
-
try {
|
|
790
|
-
if (this.listener[key](msg)) {
|
|
791
|
-
consumed = true;
|
|
792
|
-
break;
|
|
793
|
-
}
|
|
794
|
-
} catch (error) {
|
|
795
|
-
console.warn('Error in listener : ' + error);
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
if(!consumed) {
|
|
793
|
+
// send to any "message" listeners
|
|
794
|
+
this._sendEvent('message', msg);
|
|
795
|
+
// send message to receivers, if not consumed, add to queue
|
|
796
|
+
if(!this._sendReceivers(msg)) {
|
|
799
797
|
if (this.queue.length >= this._queueSize) this.queue.shift();
|
|
800
798
|
this.queue.push(msg);
|
|
801
799
|
}
|
|
@@ -849,7 +847,7 @@ class Gateway {
|
|
|
849
847
|
return new Promise(resolve => {
|
|
850
848
|
let timer = setTimeout(() => {
|
|
851
849
|
delete this.pending[rq.id];
|
|
852
|
-
if (this.debug) console.log('Receive Timeout : ' + rq);
|
|
850
|
+
if (this.debug) console.log('Receive Timeout : ' + JSON.stringify(rq));
|
|
853
851
|
resolve();
|
|
854
852
|
}, 8*this._timeout);
|
|
855
853
|
this.pending[rq.id] = rsp => {
|
|
@@ -859,7 +857,7 @@ class Gateway {
|
|
|
859
857
|
if (!this._msgTx.call(this,rq)) {
|
|
860
858
|
clearTimeout(timer);
|
|
861
859
|
delete this.pending[rq.id];
|
|
862
|
-
if (this.debug) console.log('Transmit Timeout : ' +
|
|
860
|
+
if (this.debug) console.log('Transmit Timeout : ' + JSON.stringify(rq));
|
|
863
861
|
resolve();
|
|
864
862
|
}
|
|
865
863
|
});
|
|
@@ -895,6 +893,11 @@ class Gateway {
|
|
|
895
893
|
this.flush();
|
|
896
894
|
this.connector.write('{"alive": true}');
|
|
897
895
|
this._update_watch();
|
|
896
|
+
} else {
|
|
897
|
+
if (this._cancelPendingOnDisconnect) {
|
|
898
|
+
this._sendReceivers(null);
|
|
899
|
+
this.flush();
|
|
900
|
+
}
|
|
898
901
|
}
|
|
899
902
|
this._sendEvent('conn', state);
|
|
900
903
|
});
|
|
@@ -1253,15 +1256,18 @@ class Gateway {
|
|
|
1253
1256
|
let timer;
|
|
1254
1257
|
if (timeout > 0){
|
|
1255
1258
|
timer = setTimeout(() => {
|
|
1256
|
-
this.
|
|
1259
|
+
this.listeners[lid] && delete this.listeners[lid];
|
|
1257
1260
|
if (this.debug) console.log('Receive Timeout : ' + filter);
|
|
1258
1261
|
resolve();
|
|
1259
1262
|
}, timeout);
|
|
1260
1263
|
}
|
|
1261
|
-
|
|
1262
|
-
|
|
1264
|
+
// listener for each pending receive
|
|
1265
|
+
this.listeners[lid] = msg => {
|
|
1266
|
+
// skip if the message does not match the filter
|
|
1267
|
+
if (msg && !this._matchMessage(filter, msg)) return false;
|
|
1263
1268
|
if(timer) clearTimeout(timer);
|
|
1264
|
-
|
|
1269
|
+
// if the message matches the filter or is null, delete listener clear timer and resolve
|
|
1270
|
+
this.listeners[lid] && delete this.listeners[lid];
|
|
1265
1271
|
resolve(msg);
|
|
1266
1272
|
return true;
|
|
1267
1273
|
};
|
|
@@ -1407,7 +1413,8 @@ if (isBrowser || isWebWorker){
|
|
|
1407
1413
|
'timeout': 1000,
|
|
1408
1414
|
'keepAlive' : true,
|
|
1409
1415
|
'queueSize': DEFAULT_QUEUE_SIZE,
|
|
1410
|
-
'returnNullOnFailedResponse': true
|
|
1416
|
+
'returnNullOnFailedResponse': true,
|
|
1417
|
+
'cancelPendingOnDisconnect': false
|
|
1411
1418
|
});
|
|
1412
1419
|
DEFAULT_URL = new URL('ws://localhost');
|
|
1413
1420
|
// Enable caching of Gateways
|
|
@@ -1422,7 +1429,8 @@ if (isBrowser || isWebWorker){
|
|
|
1422
1429
|
'timeout': 1000,
|
|
1423
1430
|
'keepAlive' : true,
|
|
1424
1431
|
'queueSize': DEFAULT_QUEUE_SIZE,
|
|
1425
|
-
'returnNullOnFailedResponse': true
|
|
1432
|
+
'returnNullOnFailedResponse': true,
|
|
1433
|
+
'cancelPendingOnDisconnect': false
|
|
1426
1434
|
});
|
|
1427
1435
|
DEFAULT_URL = new URL('tcp://localhost');
|
|
1428
1436
|
gObj.atob = a => Buffer.from(a, 'base64').toString('binary');
|
package/dist/esm/unet.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/* unet.js v4.0.
|
|
1
|
+
/* unet.js v4.0.2 2025-05-19T10:05:53.629Z */
|
|
2
2
|
|
|
3
|
-
/* fjage.js v2.
|
|
3
|
+
/* fjage.js v2.1.0 */
|
|
4
4
|
|
|
5
5
|
const isBrowser =
|
|
6
6
|
typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
@@ -692,6 +692,7 @@ class Message {
|
|
|
692
692
|
* @param {number} [opts.queueSize=128] - size of the queue of received messages that haven't been consumed yet
|
|
693
693
|
* @param {number} [opts.timeout=1000] - timeout for fjage level messages in ms
|
|
694
694
|
* @param {boolean} [opts.returnNullOnFailedResponse=true] - return null instead of throwing an error when a parameter is not found
|
|
695
|
+
* @param {boolean} [opts.cancelPendingOnDisconnect=false] - cancel pending requests on disconnect
|
|
695
696
|
*/
|
|
696
697
|
class Gateway {
|
|
697
698
|
|
|
@@ -710,9 +711,10 @@ class Gateway {
|
|
|
710
711
|
this._keepAlive = opts.keepAlive; // reconnect if connection gets closed/errored
|
|
711
712
|
this._queueSize = opts.queueSize; // size of queue
|
|
712
713
|
this._returnNullOnFailedResponse = opts.returnNullOnFailedResponse; // null or error
|
|
714
|
+
this._cancelPendingOnDisconnect = opts.cancelPendingOnDisconnect; // cancel pending requests on disconnect
|
|
713
715
|
this.pending = {}; // msgid to callback mapping for pending requests to server
|
|
714
716
|
this.subscriptions = {}; // hashset for all topics that are subscribed
|
|
715
|
-
this.
|
|
717
|
+
this.listeners = {}; // list of callbacks that want to listen to incoming messages
|
|
716
718
|
this.eventListeners = {}; // external listeners wanting to listen internal events
|
|
717
719
|
this.queue = []; // incoming message queue
|
|
718
720
|
this.connected = false; // connection status
|
|
@@ -729,19 +731,37 @@ class Gateway {
|
|
|
729
731
|
* @param {Object|Message|string} val - value to be sent to the listeners
|
|
730
732
|
*/
|
|
731
733
|
_sendEvent(type, val) {
|
|
732
|
-
if (Array.isArray(this.eventListeners[type]))
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
}
|
|
734
|
+
if (!Array.isArray(this.eventListeners[type])) return;
|
|
735
|
+
this.eventListeners[type].forEach(l => {
|
|
736
|
+
if (l && {}.toString.call(l) === '[object Function]'){
|
|
737
|
+
try {
|
|
738
|
+
l(val);
|
|
739
|
+
} catch (error) {
|
|
740
|
+
console.warn('Error in event listener : ' + error);
|
|
740
741
|
}
|
|
741
|
-
}
|
|
742
|
+
}
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Sends the message to all registered receivers.
|
|
748
|
+
*
|
|
749
|
+
* @private
|
|
750
|
+
* @param {Message} msg
|
|
751
|
+
* @returns {boolean} - true if the message was consumed by any listener
|
|
752
|
+
*/
|
|
753
|
+
_sendReceivers(msg) {
|
|
754
|
+
for (var lid in this.listeners){
|
|
755
|
+
try {
|
|
756
|
+
if (this.listeners[lid] && this.listeners[lid](msg)) return true;
|
|
757
|
+
} catch (error) {
|
|
758
|
+
console.warn('Error in listener : ' + error);
|
|
759
|
+
}
|
|
742
760
|
}
|
|
761
|
+
return false;
|
|
743
762
|
}
|
|
744
763
|
|
|
764
|
+
|
|
745
765
|
/**
|
|
746
766
|
* @private
|
|
747
767
|
* @param {string} data - stringfied JSON data received from the master container to be processed
|
|
@@ -768,32 +788,10 @@ class Gateway {
|
|
|
768
788
|
if (!msg) return;
|
|
769
789
|
this._sendEvent('rxmsg', msg);
|
|
770
790
|
if ((msg.recipient == this.aid.toJSON() )|| this.subscriptions[msg.recipient]) {
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
if (this.eventListeners['message'][i](msg)) {
|
|
776
|
-
consumed = true;
|
|
777
|
-
break;
|
|
778
|
-
}
|
|
779
|
-
} catch (error) {
|
|
780
|
-
console.warn('Error in message listener : ' + error);
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
// iterate over internal callbacks, until one consumes the message
|
|
785
|
-
for (var key in this.listener){
|
|
786
|
-
// callback returns true if it has consumed the message
|
|
787
|
-
try {
|
|
788
|
-
if (this.listener[key](msg)) {
|
|
789
|
-
consumed = true;
|
|
790
|
-
break;
|
|
791
|
-
}
|
|
792
|
-
} catch (error) {
|
|
793
|
-
console.warn('Error in listener : ' + error);
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
if(!consumed) {
|
|
791
|
+
// send to any "message" listeners
|
|
792
|
+
this._sendEvent('message', msg);
|
|
793
|
+
// send message to receivers, if not consumed, add to queue
|
|
794
|
+
if(!this._sendReceivers(msg)) {
|
|
797
795
|
if (this.queue.length >= this._queueSize) this.queue.shift();
|
|
798
796
|
this.queue.push(msg);
|
|
799
797
|
}
|
|
@@ -847,7 +845,7 @@ class Gateway {
|
|
|
847
845
|
return new Promise(resolve => {
|
|
848
846
|
let timer = setTimeout(() => {
|
|
849
847
|
delete this.pending[rq.id];
|
|
850
|
-
if (this.debug) console.log('Receive Timeout : ' + rq);
|
|
848
|
+
if (this.debug) console.log('Receive Timeout : ' + JSON.stringify(rq));
|
|
851
849
|
resolve();
|
|
852
850
|
}, 8*this._timeout);
|
|
853
851
|
this.pending[rq.id] = rsp => {
|
|
@@ -857,7 +855,7 @@ class Gateway {
|
|
|
857
855
|
if (!this._msgTx.call(this,rq)) {
|
|
858
856
|
clearTimeout(timer);
|
|
859
857
|
delete this.pending[rq.id];
|
|
860
|
-
if (this.debug) console.log('Transmit Timeout : ' +
|
|
858
|
+
if (this.debug) console.log('Transmit Timeout : ' + JSON.stringify(rq));
|
|
861
859
|
resolve();
|
|
862
860
|
}
|
|
863
861
|
});
|
|
@@ -893,6 +891,11 @@ class Gateway {
|
|
|
893
891
|
this.flush();
|
|
894
892
|
this.connector.write('{"alive": true}');
|
|
895
893
|
this._update_watch();
|
|
894
|
+
} else {
|
|
895
|
+
if (this._cancelPendingOnDisconnect) {
|
|
896
|
+
this._sendReceivers(null);
|
|
897
|
+
this.flush();
|
|
898
|
+
}
|
|
896
899
|
}
|
|
897
900
|
this._sendEvent('conn', state);
|
|
898
901
|
});
|
|
@@ -1251,15 +1254,18 @@ class Gateway {
|
|
|
1251
1254
|
let timer;
|
|
1252
1255
|
if (timeout > 0){
|
|
1253
1256
|
timer = setTimeout(() => {
|
|
1254
|
-
this.
|
|
1257
|
+
this.listeners[lid] && delete this.listeners[lid];
|
|
1255
1258
|
if (this.debug) console.log('Receive Timeout : ' + filter);
|
|
1256
1259
|
resolve();
|
|
1257
1260
|
}, timeout);
|
|
1258
1261
|
}
|
|
1259
|
-
|
|
1260
|
-
|
|
1262
|
+
// listener for each pending receive
|
|
1263
|
+
this.listeners[lid] = msg => {
|
|
1264
|
+
// skip if the message does not match the filter
|
|
1265
|
+
if (msg && !this._matchMessage(filter, msg)) return false;
|
|
1261
1266
|
if(timer) clearTimeout(timer);
|
|
1262
|
-
|
|
1267
|
+
// if the message matches the filter or is null, delete listener clear timer and resolve
|
|
1268
|
+
this.listeners[lid] && delete this.listeners[lid];
|
|
1263
1269
|
resolve(msg);
|
|
1264
1270
|
return true;
|
|
1265
1271
|
};
|
|
@@ -1405,7 +1411,8 @@ if (isBrowser || isWebWorker){
|
|
|
1405
1411
|
'timeout': 1000,
|
|
1406
1412
|
'keepAlive' : true,
|
|
1407
1413
|
'queueSize': DEFAULT_QUEUE_SIZE,
|
|
1408
|
-
'returnNullOnFailedResponse': true
|
|
1414
|
+
'returnNullOnFailedResponse': true,
|
|
1415
|
+
'cancelPendingOnDisconnect': false
|
|
1409
1416
|
});
|
|
1410
1417
|
DEFAULT_URL = new URL('ws://localhost');
|
|
1411
1418
|
// Enable caching of Gateways
|
|
@@ -1420,7 +1427,8 @@ if (isBrowser || isWebWorker){
|
|
|
1420
1427
|
'timeout': 1000,
|
|
1421
1428
|
'keepAlive' : true,
|
|
1422
1429
|
'queueSize': DEFAULT_QUEUE_SIZE,
|
|
1423
|
-
'returnNullOnFailedResponse': true
|
|
1430
|
+
'returnNullOnFailedResponse': true,
|
|
1431
|
+
'cancelPendingOnDisconnect': false
|
|
1424
1432
|
});
|
|
1425
1433
|
DEFAULT_URL = new URL('tcp://localhost');
|
|
1426
1434
|
gObj.atob = a => Buffer.from(a, 'base64').toString('binary');
|
package/dist/unetjs.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.unet = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
-
/* fjage.js v2.
|
|
7
|
+
/* fjage.js v2.1.0 */
|
|
8
8
|
|
|
9
9
|
const isBrowser =
|
|
10
10
|
typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
@@ -696,6 +696,7 @@
|
|
|
696
696
|
* @param {number} [opts.queueSize=128] - size of the queue of received messages that haven't been consumed yet
|
|
697
697
|
* @param {number} [opts.timeout=1000] - timeout for fjage level messages in ms
|
|
698
698
|
* @param {boolean} [opts.returnNullOnFailedResponse=true] - return null instead of throwing an error when a parameter is not found
|
|
699
|
+
* @param {boolean} [opts.cancelPendingOnDisconnect=false] - cancel pending requests on disconnect
|
|
699
700
|
*/
|
|
700
701
|
class Gateway {
|
|
701
702
|
|
|
@@ -714,9 +715,10 @@
|
|
|
714
715
|
this._keepAlive = opts.keepAlive; // reconnect if connection gets closed/errored
|
|
715
716
|
this._queueSize = opts.queueSize; // size of queue
|
|
716
717
|
this._returnNullOnFailedResponse = opts.returnNullOnFailedResponse; // null or error
|
|
718
|
+
this._cancelPendingOnDisconnect = opts.cancelPendingOnDisconnect; // cancel pending requests on disconnect
|
|
717
719
|
this.pending = {}; // msgid to callback mapping for pending requests to server
|
|
718
720
|
this.subscriptions = {}; // hashset for all topics that are subscribed
|
|
719
|
-
this.
|
|
721
|
+
this.listeners = {}; // list of callbacks that want to listen to incoming messages
|
|
720
722
|
this.eventListeners = {}; // external listeners wanting to listen internal events
|
|
721
723
|
this.queue = []; // incoming message queue
|
|
722
724
|
this.connected = false; // connection status
|
|
@@ -733,19 +735,37 @@
|
|
|
733
735
|
* @param {Object|Message|string} val - value to be sent to the listeners
|
|
734
736
|
*/
|
|
735
737
|
_sendEvent(type, val) {
|
|
736
|
-
if (Array.isArray(this.eventListeners[type]))
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
}
|
|
738
|
+
if (!Array.isArray(this.eventListeners[type])) return;
|
|
739
|
+
this.eventListeners[type].forEach(l => {
|
|
740
|
+
if (l && {}.toString.call(l) === '[object Function]'){
|
|
741
|
+
try {
|
|
742
|
+
l(val);
|
|
743
|
+
} catch (error) {
|
|
744
|
+
console.warn('Error in event listener : ' + error);
|
|
744
745
|
}
|
|
745
|
-
}
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Sends the message to all registered receivers.
|
|
752
|
+
*
|
|
753
|
+
* @private
|
|
754
|
+
* @param {Message} msg
|
|
755
|
+
* @returns {boolean} - true if the message was consumed by any listener
|
|
756
|
+
*/
|
|
757
|
+
_sendReceivers(msg) {
|
|
758
|
+
for (var lid in this.listeners){
|
|
759
|
+
try {
|
|
760
|
+
if (this.listeners[lid] && this.listeners[lid](msg)) return true;
|
|
761
|
+
} catch (error) {
|
|
762
|
+
console.warn('Error in listener : ' + error);
|
|
763
|
+
}
|
|
746
764
|
}
|
|
765
|
+
return false;
|
|
747
766
|
}
|
|
748
767
|
|
|
768
|
+
|
|
749
769
|
/**
|
|
750
770
|
* @private
|
|
751
771
|
* @param {string} data - stringfied JSON data received from the master container to be processed
|
|
@@ -772,32 +792,10 @@
|
|
|
772
792
|
if (!msg) return;
|
|
773
793
|
this._sendEvent('rxmsg', msg);
|
|
774
794
|
if ((msg.recipient == this.aid.toJSON() )|| this.subscriptions[msg.recipient]) {
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
if (this.eventListeners['message'][i](msg)) {
|
|
780
|
-
consumed = true;
|
|
781
|
-
break;
|
|
782
|
-
}
|
|
783
|
-
} catch (error) {
|
|
784
|
-
console.warn('Error in message listener : ' + error);
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
// iterate over internal callbacks, until one consumes the message
|
|
789
|
-
for (var key in this.listener){
|
|
790
|
-
// callback returns true if it has consumed the message
|
|
791
|
-
try {
|
|
792
|
-
if (this.listener[key](msg)) {
|
|
793
|
-
consumed = true;
|
|
794
|
-
break;
|
|
795
|
-
}
|
|
796
|
-
} catch (error) {
|
|
797
|
-
console.warn('Error in listener : ' + error);
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
if(!consumed) {
|
|
795
|
+
// send to any "message" listeners
|
|
796
|
+
this._sendEvent('message', msg);
|
|
797
|
+
// send message to receivers, if not consumed, add to queue
|
|
798
|
+
if(!this._sendReceivers(msg)) {
|
|
801
799
|
if (this.queue.length >= this._queueSize) this.queue.shift();
|
|
802
800
|
this.queue.push(msg);
|
|
803
801
|
}
|
|
@@ -851,7 +849,7 @@
|
|
|
851
849
|
return new Promise(resolve => {
|
|
852
850
|
let timer = setTimeout(() => {
|
|
853
851
|
delete this.pending[rq.id];
|
|
854
|
-
if (this.debug) console.log('Receive Timeout : ' + rq);
|
|
852
|
+
if (this.debug) console.log('Receive Timeout : ' + JSON.stringify(rq));
|
|
855
853
|
resolve();
|
|
856
854
|
}, 8*this._timeout);
|
|
857
855
|
this.pending[rq.id] = rsp => {
|
|
@@ -861,7 +859,7 @@
|
|
|
861
859
|
if (!this._msgTx.call(this,rq)) {
|
|
862
860
|
clearTimeout(timer);
|
|
863
861
|
delete this.pending[rq.id];
|
|
864
|
-
if (this.debug) console.log('Transmit Timeout : ' +
|
|
862
|
+
if (this.debug) console.log('Transmit Timeout : ' + JSON.stringify(rq));
|
|
865
863
|
resolve();
|
|
866
864
|
}
|
|
867
865
|
});
|
|
@@ -897,6 +895,11 @@
|
|
|
897
895
|
this.flush();
|
|
898
896
|
this.connector.write('{"alive": true}');
|
|
899
897
|
this._update_watch();
|
|
898
|
+
} else {
|
|
899
|
+
if (this._cancelPendingOnDisconnect) {
|
|
900
|
+
this._sendReceivers(null);
|
|
901
|
+
this.flush();
|
|
902
|
+
}
|
|
900
903
|
}
|
|
901
904
|
this._sendEvent('conn', state);
|
|
902
905
|
});
|
|
@@ -1255,15 +1258,18 @@
|
|
|
1255
1258
|
let timer;
|
|
1256
1259
|
if (timeout > 0){
|
|
1257
1260
|
timer = setTimeout(() => {
|
|
1258
|
-
this.
|
|
1261
|
+
this.listeners[lid] && delete this.listeners[lid];
|
|
1259
1262
|
if (this.debug) console.log('Receive Timeout : ' + filter);
|
|
1260
1263
|
resolve();
|
|
1261
1264
|
}, timeout);
|
|
1262
1265
|
}
|
|
1263
|
-
|
|
1264
|
-
|
|
1266
|
+
// listener for each pending receive
|
|
1267
|
+
this.listeners[lid] = msg => {
|
|
1268
|
+
// skip if the message does not match the filter
|
|
1269
|
+
if (msg && !this._matchMessage(filter, msg)) return false;
|
|
1265
1270
|
if(timer) clearTimeout(timer);
|
|
1266
|
-
|
|
1271
|
+
// if the message matches the filter or is null, delete listener clear timer and resolve
|
|
1272
|
+
this.listeners[lid] && delete this.listeners[lid];
|
|
1267
1273
|
resolve(msg);
|
|
1268
1274
|
return true;
|
|
1269
1275
|
};
|
|
@@ -1409,7 +1415,8 @@
|
|
|
1409
1415
|
'timeout': 1000,
|
|
1410
1416
|
'keepAlive' : true,
|
|
1411
1417
|
'queueSize': DEFAULT_QUEUE_SIZE,
|
|
1412
|
-
'returnNullOnFailedResponse': true
|
|
1418
|
+
'returnNullOnFailedResponse': true,
|
|
1419
|
+
'cancelPendingOnDisconnect': false
|
|
1413
1420
|
});
|
|
1414
1421
|
DEFAULT_URL = new URL('ws://localhost');
|
|
1415
1422
|
// Enable caching of Gateways
|
|
@@ -1424,7 +1431,8 @@
|
|
|
1424
1431
|
'timeout': 1000,
|
|
1425
1432
|
'keepAlive' : true,
|
|
1426
1433
|
'queueSize': DEFAULT_QUEUE_SIZE,
|
|
1427
|
-
'returnNullOnFailedResponse': true
|
|
1434
|
+
'returnNullOnFailedResponse': true,
|
|
1435
|
+
'cancelPendingOnDisconnect': false
|
|
1428
1436
|
});
|
|
1429
1437
|
DEFAULT_URL = new URL('tcp://localhost');
|
|
1430
1438
|
gObj.atob = a => Buffer.from(a, 'base64').toString('binary');
|