@cutos/core 3.2.8 → 3.3.1

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
@@ -410,7 +410,7 @@ ipc.sendTo(channel, args, callback = null);
410
410
 
411
411
  * channel: channel name
412
412
  * args: information to send
413
- * callback: callback function
413
+ * callback: function callback(result)
414
414
 
415
415
  ### ipc.on
416
416
 
@@ -421,19 +421,8 @@ ipc.on(channel, listener);
421
421
  ```
422
422
 
423
423
  * channel: channel name
424
- * listener: listener function (args, context)
424
+ * listener: function listener(args, respond(result)). args is the data from sender, respond is used to send data(result) back to sender's callback
425
425
 
426
- ### ipc.sendResponse
427
-
428
- The receiver replies to the message
429
-
430
- ```js
431
- ipc.sendResponse(context, result, error = null);
432
- ```
433
-
434
- * context: context information
435
- * result: reply message content
436
- * error: error message
437
426
 
438
427
  ##### Local device communication example:
439
428
 
@@ -443,12 +432,16 @@ import {CoreAPI} from '@cutos/core';
443
432
  CoreAPI.init();
444
433
 
445
434
  let ipc = CoreAPI.getIPC();
446
- //Listen to IPC information
447
- ipc.on("channel-1", function (args, context) {
435
+ // listen
436
+ ipc.on("channel-1", function (args, respond) {
448
437
  console.log(args);
438
+ respond('received')
439
+ });
440
+
441
+ // send
442
+ ipc.sendTo("channel-1", {event: "event-1", msg: "msg-1"}, (result) => {
443
+ console.log(result)
449
444
  });
450
- //Send IPC information
451
- ipc.sendTo("channel-1", {event: "event-1", msg: "msg-1"});
452
445
  ```
453
446
 
454
447
  - Return result example:
@@ -468,13 +461,11 @@ IP:192.168.1.136
468
461
  import {CoreAPI} from '@cutos/core';
469
462
 
470
463
  CoreAPI.init();
471
- //Destination IP:192.168.1.149
464
+ // Destination IP:192.168.1.149
472
465
  let ipc = CoreAPI.getIPC("192.168.1.149");
473
- //Send IPC information
474
- ipc.sendTo("channel-2", {event: "event-2", msg: "msg-2"}, (result, error) => {
475
- if (!error) {
466
+ // Send IPC information
467
+ ipc.sendTo("channel-2", {event: "event-2", msg: "msg-2"}, (result) => {
476
468
  console.log(result)
477
- }
478
469
  });
479
470
  ```
480
471
  - Return result example:
@@ -489,14 +480,12 @@ IP:192.168.1.149
489
480
  import {CoreAPI} from '@cutos/core';
490
481
 
491
482
  CoreAPI.init();
492
- //Target IP:192.168.1.136
493
- let ipc = CoreAPI.getIPC("192.168.1.136");
494
- //Listen for IPC information
495
- let ipcListener = function (args, context) {
496
- console.log(args);
497
- ipc.sendResponse(context, "Received");
498
- }
499
- ipc.on("channel-2", ipcListener);
483
+
484
+ let ipc = CoreAPI.getIPC();
485
+ ipc.on("channel-2", (args, respond) => {
486
+ console.log(args);
487
+ respond('Received')
488
+ })
500
489
  ```
501
490
 
502
491
  - Return result example:
@@ -973,9 +962,9 @@ Receive device commands
973
962
  driver.onCommand(listener)
974
963
  ```
975
964
 
976
- - listener: listen callback function (command, [callback]), call callback after processing command
965
+ - listener: function listener(command, respond), call respond after processing command
977
966
  - command: command, example: {cmd: "connect", args: ""}
978
- - callback: callback function (result)
967
+ - respond: used to send data back to Device.sendCommand's callback function
979
968
 
980
969
  ## SDK
981
970
 
@@ -1000,7 +989,7 @@ device.init([opts], callback)
1000
989
 
1001
990
  * opts: optional parameters, passed to the driver loading process, refer
1002
991
  to [Driver Template](#download-project-template)
1003
- * callback: callback function
992
+ * callback: function(result, error)
1004
993
 
1005
994
  ##### Example:
1006
995
 
@@ -1023,7 +1012,7 @@ device.sendCommand(command, callback)
1023
1012
  ```
1024
1013
 
1025
1014
  - command: command, example: command = {cmd: "connect", args: ""}
1026
- - callback: callback function, you can pass a listening callback function to listen to the device status
1015
+ - callback: function callback(result), used to receive the response from driver
1027
1016
 
1028
1017
  ##### Example:
1029
1018
 
@@ -1039,8 +1028,7 @@ Receive data sent by the driver
1039
1028
  device.onData(listener)
1040
1029
  ```
1041
1030
 
1042
- - listener: listener callback function (data)
1043
- - data: data
1031
+ - listener: function listener(data)
1044
1032
 
1045
1033
  ## Device driver relationship
1046
1034
 
@@ -1220,37 +1208,37 @@ class DriverTemplate extends CoreClass.Driver {
1220
1208
  this.startBeat();
1221
1209
  this.updateStatusInfo(CoreDefine.HEARTBEAT_STATUS.ALIVE, "alive");
1222
1210
 
1223
- this.onCommand(({cmd, args}, callback) => {
1211
+ this.onCommand(({cmd, args}, respond) => {
1224
1212
  // Listen to the messages sent by the device SDK, process the different commands in the message body,
1225
1213
  // and respond back to the device SDK through the callback function.
1226
1214
  switch (cmd) {
1227
1215
  case CMD.CONNECT:
1228
- this.connect(args, callback);
1216
+ this.connect(args, respond);
1229
1217
  break;
1230
1218
  case CMD.CUSTOM_CMD:
1231
1219
  this.customCmd(cmd, args);
1232
1220
  break;
1233
1221
  default:
1234
- this.unsupported(cmd, args, callback);
1222
+ this.unsupported(cmd, args, respond);
1235
1223
  break;
1236
1224
  }
1237
1225
  });
1238
1226
  }
1239
1227
 
1240
- unsupported(cmd, args, callback) {
1228
+ unsupported(cmd, args, respond) {
1241
1229
  let result = {};
1242
1230
  result.msg = "cmd: " + cmd + " is unsupported.";
1243
1231
  result.status = false;
1244
1232
  console.log(result.msg);
1245
- callback(result);
1233
+ respond(result);
1246
1234
  }
1247
1235
 
1248
- connect(args, callback) {
1236
+ connect(args, respond) {
1249
1237
  console.log("connect: received.");
1250
1238
  let result = {};
1251
1239
  result.msg = "return success.";
1252
1240
  result.status = true;
1253
- callback(result);
1241
+ respond(result);
1254
1242
  }
1255
1243
 
1256
1244
  // no response
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.2.8";
401
+ var version = "3.3.1";
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";
@@ -506,7 +506,7 @@ function saveCallback(id, item) {
506
506
  let currentTimestamp = new Date().getTime();
507
507
  callbackMap.forEach((callbackItem, id) => {
508
508
  if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
509
- callbackItem.callback(null, new CutosError('CUTOS CORE error', 'callback is timeout'));
509
+ console.warn("callback is timeout");
510
510
  callbackMap.delete(id);
511
511
  }
512
512
  });
@@ -517,11 +517,14 @@ function doCallback(id, body, err) {
517
517
  if (!callbackMap.has(id)) {
518
518
  return false;
519
519
  }
520
-
521
- if (err) {
522
- callbackMap.get(id).callback(null, new CutosError('CUTOS CORE error', err));
523
- } else {
524
- callbackMap.get(id).callback(body);
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);
525
528
  }
526
529
 
527
530
  callbackMap.delete(id);
@@ -886,7 +889,7 @@ class IPC {
886
889
  * send message to channel
887
890
  * @param channel
888
891
  * @param args
889
- * @param callback (result, error):void
892
+ * @param callback (result) response from receiver
890
893
  * @returns {boolean}
891
894
  */
892
895
  sendTo(channel, args, callback = null) {
@@ -898,34 +901,46 @@ class IPC {
898
901
  }
899
902
 
900
903
  /**
901
- * 发送需要callback的消息的响应
902
- * @param context listener收到的context
904
+ * internal function, used to response sender with the result
905
+ * @param context contain sender information
903
906
  * @param result
904
- * @param error
905
907
  */
906
- sendResponse(context, result, error = null) {
908
+ _sendResponse(context, result) {
907
909
  let response = {};
908
910
  response.context = {};
909
911
  response.context.destAddress = context.sourceAddress;
910
912
  response.id = context.id;
911
913
  response.result = result;
912
- response.err = error;
913
914
  let topic = cutosAPI$1.CORE_NORESP.IPC + (this.destAddress ? "" : "-endpoint");
914
915
  client.publish(topic, JSON.stringify(response));
915
916
  }
916
917
 
917
918
  /**
918
- * Listen IPC form channel
919
+ * listening channel
919
920
  * @param channel
920
- * @param listener (args, context) the context use when sendResponse
921
+ * @param listener (args, respond(result)) args is the data from sender, respond is used to send data back to sender's callback
921
922
  */
922
923
  on(channel, listener) {
923
- if (!ipcListenerMap.has(channel)) {
924
- ipcListenerMap.set(channel, []);
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
+ }
925
936
  }
926
- ipcListenerMap.get(channel).push(listener);
927
937
  }
928
938
 
939
+ /**
940
+ * unsubscribe from channel, if listener is null, all listeners will remove
941
+ * @param channel
942
+ * @param listener
943
+ */
929
944
  removeListener(channel, listener = null) {
930
945
  if (!ipcListenerMap.has(channel)) {
931
946
  return;
@@ -933,7 +948,7 @@ class IPC {
933
948
  if (!listener) {
934
949
  ipcListenerMap.delete(channel);
935
950
  } else {
936
- ipcListenerMap.has(channel).indexOf(listener) > -1 && ipcListenerMap.get(channel).splice(ipcListenerMap.get(channel).indexOf(listener), 1);
951
+ ipcListenerMap.get(channel).delete(listener);
937
952
  }
938
953
  }
939
954
  }
@@ -1174,7 +1189,7 @@ class Driver {
1174
1189
 
1175
1190
  /**
1176
1191
  * Receive device commands
1177
- * @param listener(command, callback(result))
1192
+ * @param listener(command, respond(result))
1178
1193
  */
1179
1194
  onCommand(listener) {
1180
1195
  if (listener) {
@@ -1250,14 +1265,12 @@ const CoreAPI$2 = {
1250
1265
  if (topic === cutosAPI$1.CORE_NORESP.IPC + '-endpoint') { // ipc
1251
1266
  // process response message
1252
1267
  if (_message.id) {
1253
- doCallback(_message.id, _message.result, _message.err);
1268
+ doCallback(_message.id, _message.result);
1254
1269
  return
1255
1270
  }
1256
- let listenerArray = ipcListenerMap.get(_message.body.channel);
1257
- if (listenerArray) {
1258
- for (const listener of listenerArray) {
1259
- listener(_message.body.args, _message.context);
1260
- }
1271
+ let listenerMap = ipcListenerMap.get(_message.body.channel);
1272
+ if (listenerMap) {
1273
+ listenerMap.forEach(listener => listener(_message.body.args, _message.context));
1261
1274
  }
1262
1275
  } else if (topic === cutosAPI$1.CORE_APP.NOTIFICATION || topic === cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER
1263
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.2.8";
693
+ var version = "3.3.1";
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";
@@ -798,7 +798,7 @@ function saveCallback(id, item) {
798
798
  let currentTimestamp = new Date().getTime();
799
799
  callbackMap.forEach((callbackItem, id) => {
800
800
  if (currentTimestamp - callbackItem.timestamp > callbackTimeout) {
801
- callbackItem.callback(null, new CutosError('CUTOS CORE error', 'callback is timeout'));
801
+ console.warn("callback is timeout");
802
802
  callbackMap.delete(id);
803
803
  }
804
804
  });
@@ -809,11 +809,14 @@ function doCallback(id, body, err) {
809
809
  if (!callbackMap.has(id)) {
810
810
  return false;
811
811
  }
812
-
813
- if (err) {
814
- callbackMap.get(id).callback(null, new CutosError('CUTOS CORE error', err));
815
- } else {
816
- callbackMap.get(id).callback(body);
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);
817
820
  }
818
821
 
819
822
  callbackMap.delete(id);
@@ -1178,7 +1181,7 @@ class IPC {
1178
1181
  * send message to channel
1179
1182
  * @param channel
1180
1183
  * @param args
1181
- * @param callback (result, error):void
1184
+ * @param callback (result) response from receiver
1182
1185
  * @returns {boolean}
1183
1186
  */
1184
1187
  sendTo(channel, args, callback = null) {
@@ -1190,34 +1193,46 @@ class IPC {
1190
1193
  }
1191
1194
 
1192
1195
  /**
1193
- * 发送需要callback的消息的响应
1194
- * @param context listener收到的context
1196
+ * internal function, used to response sender with the result
1197
+ * @param context contain sender information
1195
1198
  * @param result
1196
- * @param error
1197
1199
  */
1198
- sendResponse(context, result, error = null) {
1200
+ _sendResponse(context, result) {
1199
1201
  let response = {};
1200
1202
  response.context = {};
1201
1203
  response.context.destAddress = context.sourceAddress;
1202
1204
  response.id = context.id;
1203
1205
  response.result = result;
1204
- response.err = error;
1205
1206
  let topic = cutosAPI$1.CORE_NORESP.IPC + (this.destAddress ? "" : "-endpoint");
1206
1207
  client.publish(topic, JSON.stringify(response));
1207
1208
  }
1208
1209
 
1209
1210
  /**
1210
- * Listen IPC form channel
1211
+ * listening channel
1211
1212
  * @param channel
1212
- * @param listener (args, context) the context use when sendResponse
1213
+ * @param listener (args, respond(result)) args is the data from sender, respond is used to send data back to sender's callback
1213
1214
  */
1214
1215
  on(channel, listener) {
1215
- if (!ipcListenerMap.has(channel)) {
1216
- ipcListenerMap.set(channel, []);
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
+ }
1217
1228
  }
1218
- ipcListenerMap.get(channel).push(listener);
1219
1229
  }
1220
1230
 
1231
+ /**
1232
+ * unsubscribe from channel, if listener is null, all listeners will remove
1233
+ * @param channel
1234
+ * @param listener
1235
+ */
1221
1236
  removeListener(channel, listener = null) {
1222
1237
  if (!ipcListenerMap.has(channel)) {
1223
1238
  return;
@@ -1225,7 +1240,7 @@ class IPC {
1225
1240
  if (!listener) {
1226
1241
  ipcListenerMap.delete(channel);
1227
1242
  } else {
1228
- ipcListenerMap.has(channel).indexOf(listener) > -1 && ipcListenerMap.get(channel).splice(ipcListenerMap.get(channel).indexOf(listener), 1);
1243
+ ipcListenerMap.get(channel).delete(listener);
1229
1244
  }
1230
1245
  }
1231
1246
  }
@@ -1466,7 +1481,7 @@ class Driver {
1466
1481
 
1467
1482
  /**
1468
1483
  * Receive device commands
1469
- * @param listener(command, callback(result))
1484
+ * @param listener(command, respond(result))
1470
1485
  */
1471
1486
  onCommand(listener) {
1472
1487
  if (listener) {
@@ -1542,14 +1557,12 @@ const CoreAPI$2 = {
1542
1557
  if (topic === cutosAPI$1.CORE_NORESP.IPC + '-endpoint') { // ipc
1543
1558
  // process response message
1544
1559
  if (_message.id) {
1545
- doCallback(_message.id, _message.result, _message.err);
1560
+ doCallback(_message.id, _message.result);
1546
1561
  return
1547
1562
  }
1548
- let listenerArray = ipcListenerMap.get(_message.body.channel);
1549
- if (listenerArray) {
1550
- for (const listener of listenerArray) {
1551
- listener(_message.body.args, _message.context);
1552
- }
1563
+ let listenerMap = ipcListenerMap.get(_message.body.channel);
1564
+ if (listenerMap) {
1565
+ listenerMap.forEach(listener => listener(_message.body.args, _message.context));
1553
1566
  }
1554
1567
  } else if (topic === cutosAPI$1.CORE_APP.NOTIFICATION || topic === cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER
1555
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.2.8",
3
+ "version": "3.3.1",
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",