@cutos/core 3.2.7 → 3.3.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 +1 -1
- package/dist/cutos.cjs.js +52 -24
- package/dist/cutos.esm.js +52 -24
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/cutos.cjs.js
CHANGED
|
@@ -398,7 +398,7 @@ const cutosAPI$2 = {
|
|
|
398
398
|
var cutosApi = cutosAPI$2;
|
|
399
399
|
|
|
400
400
|
var name = "@cutos/core";
|
|
401
|
-
var version = "3.
|
|
401
|
+
var version = "3.3.0";
|
|
402
402
|
var description = "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ";
|
|
403
403
|
var main = "dist/cutos.cjs.js";
|
|
404
404
|
var module$1 = "dist/cutos.esm.js";
|
|
@@ -456,6 +456,7 @@ const callbackMap = new Map();
|
|
|
456
456
|
const callbackInterval = 500; // 500ms
|
|
457
457
|
const callbackTimeout = 5000; // 5s
|
|
458
458
|
const listenerMap = new Map();
|
|
459
|
+
const ipcListenerMap = new Map();
|
|
459
460
|
const packageJson = require$$3;
|
|
460
461
|
|
|
461
462
|
const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
@@ -505,7 +506,7 @@ function saveCallback(id, item) {
|
|
|
505
506
|
let currentTimestamp = new Date().getTime();
|
|
506
507
|
callbackMap.forEach((callbackItem, id) => {
|
|
507
508
|
if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
|
|
508
|
-
|
|
509
|
+
console.warn("callback is timeout");
|
|
509
510
|
callbackMap.delete(id);
|
|
510
511
|
}
|
|
511
512
|
});
|
|
@@ -516,11 +517,14 @@ function doCallback(id, body, err) {
|
|
|
516
517
|
if (!callbackMap.has(id)) {
|
|
517
518
|
return false;
|
|
518
519
|
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
520
|
+
try {
|
|
521
|
+
if (err) {
|
|
522
|
+
callbackMap.get(id).callback(null, new CutosError('CUTOS CORE error', err));
|
|
523
|
+
} else {
|
|
524
|
+
callbackMap.get(id).callback(body);
|
|
525
|
+
}
|
|
526
|
+
} catch (e) {
|
|
527
|
+
console.warn(e.message);
|
|
524
528
|
}
|
|
525
529
|
|
|
526
530
|
callbackMap.delete(id);
|
|
@@ -885,43 +889,67 @@ class IPC {
|
|
|
885
889
|
* send message to channel
|
|
886
890
|
* @param channel
|
|
887
891
|
* @param args
|
|
888
|
-
* @param callback (result
|
|
892
|
+
* @param callback (result) response from receiver
|
|
889
893
|
* @returns {boolean}
|
|
890
894
|
*/
|
|
891
895
|
sendTo(channel, args, callback = null) {
|
|
892
896
|
let body = {};
|
|
893
897
|
body.channel = channel;
|
|
894
898
|
body.args = args;
|
|
895
|
-
|
|
899
|
+
let topic = cutosAPI$1.CORE_NORESP.IPC + (this.destAddress ? "" : "-endpoint");
|
|
900
|
+
return publishRequest(topic, callback, body, this.destAddress);
|
|
896
901
|
}
|
|
897
902
|
|
|
898
903
|
/**
|
|
899
|
-
*
|
|
900
|
-
* @param context
|
|
904
|
+
* internal function, used to response sender with the result
|
|
905
|
+
* @param context contain sender information
|
|
901
906
|
* @param result
|
|
902
|
-
* @param error
|
|
903
907
|
*/
|
|
904
|
-
|
|
908
|
+
_sendResponse(context, result) {
|
|
905
909
|
let response = {};
|
|
906
910
|
response.context = {};
|
|
907
911
|
response.context.destAddress = context.sourceAddress;
|
|
908
912
|
response.id = context.id;
|
|
909
913
|
response.result = result;
|
|
910
|
-
|
|
911
|
-
client.publish(
|
|
914
|
+
let topic = cutosAPI$1.CORE_NORESP.IPC + (this.destAddress ? "" : "-endpoint");
|
|
915
|
+
client.publish(topic, JSON.stringify(response));
|
|
912
916
|
}
|
|
913
917
|
|
|
914
918
|
/**
|
|
915
|
-
*
|
|
919
|
+
* listening channel
|
|
916
920
|
* @param channel
|
|
917
|
-
* @param listener (args,
|
|
921
|
+
* @param listener (args, callback(result)) args is the data from sender, callback is used to response sender with the result
|
|
918
922
|
*/
|
|
919
923
|
on(channel, listener) {
|
|
920
|
-
|
|
924
|
+
if (listener) {
|
|
925
|
+
if (!ipcListenerMap.has(channel)) {
|
|
926
|
+
ipcListenerMap.set(channel, new Map());
|
|
927
|
+
}
|
|
928
|
+
if (!ipcListenerMap.get(channel).has(listener)) {
|
|
929
|
+
let innerListener = (args, context) => {
|
|
930
|
+
listener(args, (result) => {
|
|
931
|
+
this._sendResponse(context, result);
|
|
932
|
+
});
|
|
933
|
+
};
|
|
934
|
+
ipcListenerMap.get(channel).set(listener, innerListener);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
921
937
|
}
|
|
922
938
|
|
|
923
|
-
|
|
924
|
-
|
|
939
|
+
/**
|
|
940
|
+
* unsubscribe from channel, if listener is null, all listeners will remove
|
|
941
|
+
* @param channel
|
|
942
|
+
* @param listener
|
|
943
|
+
*/
|
|
944
|
+
removeListener(channel, listener = null) {
|
|
945
|
+
if (!ipcListenerMap.has(channel)) {
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
if (!listener) {
|
|
949
|
+
ipcListenerMap.delete(channel);
|
|
950
|
+
} else {
|
|
951
|
+
ipcListenerMap.get(channel).delete(listener);
|
|
952
|
+
}
|
|
925
953
|
}
|
|
926
954
|
}
|
|
927
955
|
|
|
@@ -1237,12 +1265,12 @@ const CoreAPI$2 = {
|
|
|
1237
1265
|
if (topic === cutosAPI$1.CORE_NORESP.IPC + '-endpoint') { // ipc
|
|
1238
1266
|
// process response message
|
|
1239
1267
|
if (_message.id) {
|
|
1240
|
-
doCallback(_message.id, _message.result
|
|
1268
|
+
doCallback(_message.id, _message.result);
|
|
1241
1269
|
return
|
|
1242
1270
|
}
|
|
1243
|
-
let
|
|
1244
|
-
if (
|
|
1245
|
-
listener(_message.body.args, _message.context);
|
|
1271
|
+
let listenerMap = ipcListenerMap.get(_message.body.channel);
|
|
1272
|
+
if (listenerMap) {
|
|
1273
|
+
listenerMap.forEach(listener => listener(_message.body.args, _message.context));
|
|
1246
1274
|
}
|
|
1247
1275
|
} else if (topic === cutosAPI$1.CORE_APP.NOTIFICATION || topic === cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER
|
|
1248
1276
|
|| topic.startsWith(cutosAPI$1.DEVICE.CHANNEL) && !topic.endsWith('-callback')) {
|
package/dist/cutos.esm.js
CHANGED
|
@@ -690,7 +690,7 @@ const cutosAPI$2 = {
|
|
|
690
690
|
var cutosApi = cutosAPI$2;
|
|
691
691
|
|
|
692
692
|
var name = "@cutos/core";
|
|
693
|
-
var version = "3.
|
|
693
|
+
var version = "3.3.0";
|
|
694
694
|
var description = "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ";
|
|
695
695
|
var main = "dist/cutos.cjs.js";
|
|
696
696
|
var module = "dist/cutos.esm.js";
|
|
@@ -748,6 +748,7 @@ const callbackMap = new Map();
|
|
|
748
748
|
const callbackInterval = 500; // 500ms
|
|
749
749
|
const callbackTimeout = 5000; // 5s
|
|
750
750
|
const listenerMap = new Map();
|
|
751
|
+
const ipcListenerMap = new Map();
|
|
751
752
|
const packageJson = require$$3;
|
|
752
753
|
|
|
753
754
|
const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
@@ -797,7 +798,7 @@ function saveCallback(id, item) {
|
|
|
797
798
|
let currentTimestamp = new Date().getTime();
|
|
798
799
|
callbackMap.forEach((callbackItem, id) => {
|
|
799
800
|
if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
|
|
800
|
-
|
|
801
|
+
console.warn("callback is timeout");
|
|
801
802
|
callbackMap.delete(id);
|
|
802
803
|
}
|
|
803
804
|
});
|
|
@@ -808,11 +809,14 @@ function doCallback(id, body, err) {
|
|
|
808
809
|
if (!callbackMap.has(id)) {
|
|
809
810
|
return false;
|
|
810
811
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
812
|
+
try {
|
|
813
|
+
if (err) {
|
|
814
|
+
callbackMap.get(id).callback(null, new CutosError('CUTOS CORE error', err));
|
|
815
|
+
} else {
|
|
816
|
+
callbackMap.get(id).callback(body);
|
|
817
|
+
}
|
|
818
|
+
} catch (e) {
|
|
819
|
+
console.warn(e.message);
|
|
816
820
|
}
|
|
817
821
|
|
|
818
822
|
callbackMap.delete(id);
|
|
@@ -1177,43 +1181,67 @@ class IPC {
|
|
|
1177
1181
|
* send message to channel
|
|
1178
1182
|
* @param channel
|
|
1179
1183
|
* @param args
|
|
1180
|
-
* @param callback (result
|
|
1184
|
+
* @param callback (result) response from receiver
|
|
1181
1185
|
* @returns {boolean}
|
|
1182
1186
|
*/
|
|
1183
1187
|
sendTo(channel, args, callback = null) {
|
|
1184
1188
|
let body = {};
|
|
1185
1189
|
body.channel = channel;
|
|
1186
1190
|
body.args = args;
|
|
1187
|
-
|
|
1191
|
+
let topic = cutosAPI$1.CORE_NORESP.IPC + (this.destAddress ? "" : "-endpoint");
|
|
1192
|
+
return publishRequest(topic, callback, body, this.destAddress);
|
|
1188
1193
|
}
|
|
1189
1194
|
|
|
1190
1195
|
/**
|
|
1191
|
-
*
|
|
1192
|
-
* @param context
|
|
1196
|
+
* internal function, used to response sender with the result
|
|
1197
|
+
* @param context contain sender information
|
|
1193
1198
|
* @param result
|
|
1194
|
-
* @param error
|
|
1195
1199
|
*/
|
|
1196
|
-
|
|
1200
|
+
_sendResponse(context, result) {
|
|
1197
1201
|
let response = {};
|
|
1198
1202
|
response.context = {};
|
|
1199
1203
|
response.context.destAddress = context.sourceAddress;
|
|
1200
1204
|
response.id = context.id;
|
|
1201
1205
|
response.result = result;
|
|
1202
|
-
|
|
1203
|
-
client.publish(
|
|
1206
|
+
let topic = cutosAPI$1.CORE_NORESP.IPC + (this.destAddress ? "" : "-endpoint");
|
|
1207
|
+
client.publish(topic, JSON.stringify(response));
|
|
1204
1208
|
}
|
|
1205
1209
|
|
|
1206
1210
|
/**
|
|
1207
|
-
*
|
|
1211
|
+
* listening channel
|
|
1208
1212
|
* @param channel
|
|
1209
|
-
* @param listener (args,
|
|
1213
|
+
* @param listener (args, callback(result)) args is the data from sender, callback is used to response sender with the result
|
|
1210
1214
|
*/
|
|
1211
1215
|
on(channel, listener) {
|
|
1212
|
-
|
|
1216
|
+
if (listener) {
|
|
1217
|
+
if (!ipcListenerMap.has(channel)) {
|
|
1218
|
+
ipcListenerMap.set(channel, new Map());
|
|
1219
|
+
}
|
|
1220
|
+
if (!ipcListenerMap.get(channel).has(listener)) {
|
|
1221
|
+
let innerListener = (args, context) => {
|
|
1222
|
+
listener(args, (result) => {
|
|
1223
|
+
this._sendResponse(context, result);
|
|
1224
|
+
});
|
|
1225
|
+
};
|
|
1226
|
+
ipcListenerMap.get(channel).set(listener, innerListener);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1213
1229
|
}
|
|
1214
1230
|
|
|
1215
|
-
|
|
1216
|
-
|
|
1231
|
+
/**
|
|
1232
|
+
* unsubscribe from channel, if listener is null, all listeners will remove
|
|
1233
|
+
* @param channel
|
|
1234
|
+
* @param listener
|
|
1235
|
+
*/
|
|
1236
|
+
removeListener(channel, listener = null) {
|
|
1237
|
+
if (!ipcListenerMap.has(channel)) {
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
if (!listener) {
|
|
1241
|
+
ipcListenerMap.delete(channel);
|
|
1242
|
+
} else {
|
|
1243
|
+
ipcListenerMap.get(channel).delete(listener);
|
|
1244
|
+
}
|
|
1217
1245
|
}
|
|
1218
1246
|
}
|
|
1219
1247
|
|
|
@@ -1529,12 +1557,12 @@ const CoreAPI$2 = {
|
|
|
1529
1557
|
if (topic === cutosAPI$1.CORE_NORESP.IPC + '-endpoint') { // ipc
|
|
1530
1558
|
// process response message
|
|
1531
1559
|
if (_message.id) {
|
|
1532
|
-
doCallback(_message.id, _message.result
|
|
1560
|
+
doCallback(_message.id, _message.result);
|
|
1533
1561
|
return
|
|
1534
1562
|
}
|
|
1535
|
-
let
|
|
1536
|
-
if (
|
|
1537
|
-
listener(_message.body.args, _message.context);
|
|
1563
|
+
let listenerMap = ipcListenerMap.get(_message.body.channel);
|
|
1564
|
+
if (listenerMap) {
|
|
1565
|
+
listenerMap.forEach(listener => listener(_message.body.args, _message.context));
|
|
1538
1566
|
}
|
|
1539
1567
|
} else if (topic === cutosAPI$1.CORE_APP.NOTIFICATION || topic === cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER
|
|
1540
1568
|
|| topic.startsWith(cutosAPI$1.DEVICE.CHANNEL) && !topic.endsWith('-callback')) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cutos/core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ",
|
|
5
5
|
"main": "dist/cutos.cjs.js",
|
|
6
6
|
"module": "dist/cutos.esm.js",
|