@cutos/core 4.0.2 → 4.0.5
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 +33 -192
- package/dist/cutos.cjs.js +181 -272
- package/dist/cutos.esm.js +180 -272
- package/package.json +3 -2
- package/types/index.d.ts +78 -19
package/dist/cutos.cjs.js
CHANGED
|
@@ -32,7 +32,7 @@ function getAugmentedNamespace(n) {
|
|
|
32
32
|
|
|
33
33
|
var src = {};
|
|
34
34
|
|
|
35
|
-
var cutos
|
|
35
|
+
var cutos = {};
|
|
36
36
|
|
|
37
37
|
const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
|
|
38
38
|
|
|
@@ -398,13 +398,14 @@ const cutosAPI$2 = {
|
|
|
398
398
|
var cutosApi = cutosAPI$2;
|
|
399
399
|
|
|
400
400
|
var name = "@cutos/core";
|
|
401
|
-
var version = "4.0.
|
|
401
|
+
var version = "4.0.5";
|
|
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";
|
|
405
405
|
var types = "types/index.d.ts";
|
|
406
406
|
var scripts = {
|
|
407
|
-
"build:rollup": "rollup -c rollup.config.cjs"
|
|
407
|
+
"build:rollup": "rollup -c rollup.config.cjs",
|
|
408
|
+
test: "node test/device-provider-base.mjs"
|
|
408
409
|
};
|
|
409
410
|
var keywords = [
|
|
410
411
|
"CUTOS",
|
|
@@ -1117,6 +1118,38 @@ class Heartbeat {
|
|
|
1117
1118
|
}
|
|
1118
1119
|
}
|
|
1119
1120
|
|
|
1121
|
+
const deviceStatusListenerMap = new Map();
|
|
1122
|
+
let deviceHeartbeatListenerRegistered = false;
|
|
1123
|
+
|
|
1124
|
+
function addDeviceStatusListener(type, listener) {
|
|
1125
|
+
if (!deviceStatusListenerMap.has(type)) {
|
|
1126
|
+
deviceStatusListenerMap.set(type, new Set());
|
|
1127
|
+
}
|
|
1128
|
+
deviceStatusListenerMap.get(type).add(listener);
|
|
1129
|
+
|
|
1130
|
+
if (!deviceHeartbeatListenerRegistered) {
|
|
1131
|
+
listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, msg => {
|
|
1132
|
+
deviceStatusListenerMap.forEach((listeners, listenerType) => {
|
|
1133
|
+
if (listenerType === cutosAPI$1.DEVICE.DEFAULT || listenerType === msg.pinger) {
|
|
1134
|
+
listeners.forEach(statusListener => statusListener(msg));
|
|
1135
|
+
}
|
|
1136
|
+
});
|
|
1137
|
+
});
|
|
1138
|
+
deviceHeartbeatListenerRegistered = true;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
return () => {
|
|
1142
|
+
const listeners = deviceStatusListenerMap.get(type);
|
|
1143
|
+
if (!listeners) {
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
listeners.delete(listener);
|
|
1147
|
+
if (listeners.size === 0) {
|
|
1148
|
+
deviceStatusListenerMap.delete(type);
|
|
1149
|
+
}
|
|
1150
|
+
};
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1120
1153
|
class Device {
|
|
1121
1154
|
/**
|
|
1122
1155
|
* The constructor of Device
|
|
@@ -1129,27 +1162,6 @@ class Device {
|
|
|
1129
1162
|
this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
|
|
1130
1163
|
this.topicResponse = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-response';
|
|
1131
1164
|
this.topicCommand = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-command';
|
|
1132
|
-
this.data = {};
|
|
1133
|
-
|
|
1134
|
-
if (Device.objectMap === undefined) {
|
|
1135
|
-
Device.objectMap = new Map();
|
|
1136
|
-
}
|
|
1137
|
-
Device.objectMap.set(this.type, this);
|
|
1138
|
-
|
|
1139
|
-
if (Device.heartbeatListener === undefined) {
|
|
1140
|
-
Device.heartbeatListener = function (msg) {
|
|
1141
|
-
for (const [key, obj] of Device.objectMap) {
|
|
1142
|
-
if (obj.statusListener !== undefined) {
|
|
1143
|
-
if (obj.type === "default-device" || obj.type === msg.pinger) {
|
|
1144
|
-
obj.statusListener(msg);
|
|
1145
|
-
obj.msg = msg;
|
|
1146
|
-
}
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
//console.debug(JSON.stringify(msg));
|
|
1150
|
-
};
|
|
1151
|
-
listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
|
|
1152
|
-
}
|
|
1153
1165
|
// subscribe command callback
|
|
1154
1166
|
client.subscribe(this.topicCommand + '-callback');
|
|
1155
1167
|
}
|
|
@@ -1190,22 +1202,6 @@ class Device {
|
|
|
1190
1202
|
});
|
|
1191
1203
|
}
|
|
1192
1204
|
|
|
1193
|
-
/**
|
|
1194
|
-
* Get device status
|
|
1195
|
-
* @param {Function} listener
|
|
1196
|
-
*/
|
|
1197
|
-
onStatus(listener) {
|
|
1198
|
-
this.statusListener = listener;
|
|
1199
|
-
}
|
|
1200
|
-
|
|
1201
|
-
/**
|
|
1202
|
-
* Get device status
|
|
1203
|
-
* @param {Function} listener
|
|
1204
|
-
*/
|
|
1205
|
-
getCurrentStatus() {
|
|
1206
|
-
return this.msg;
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
1205
|
/**
|
|
1210
1206
|
*
|
|
1211
1207
|
* @param command - command description object {cmd: 'command', args: (''|{}) }
|
|
@@ -1223,40 +1219,6 @@ class Device {
|
|
|
1223
1219
|
}
|
|
1224
1220
|
return maybePromise(callback, cb => publishRequest(this.topicCommand, cb, command, undefined, opts))
|
|
1225
1221
|
}
|
|
1226
|
-
|
|
1227
|
-
/**
|
|
1228
|
-
* Receive data sent by the driver
|
|
1229
|
-
* @param listener - listener(data)
|
|
1230
|
-
*/
|
|
1231
|
-
onData(listener) {
|
|
1232
|
-
if (!listenerMap.has(this.topicData)) {
|
|
1233
|
-
client.subscribe(this.topicData); // receive data from device
|
|
1234
|
-
}
|
|
1235
|
-
|
|
1236
|
-
listenerMap.set(this.topicData, (data) => {
|
|
1237
|
-
this.data = data;
|
|
1238
|
-
listener(data);
|
|
1239
|
-
});
|
|
1240
|
-
return () => this.offData(listener);
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
|
-
offData(listener = null) {
|
|
1244
|
-
listenerMap.delete(this.topicData);
|
|
1245
|
-
}
|
|
1246
|
-
|
|
1247
|
-
getCurrentData() {
|
|
1248
|
-
return this.data;
|
|
1249
|
-
}
|
|
1250
|
-
|
|
1251
|
-
/**
|
|
1252
|
-
*
|
|
1253
|
-
* @param callback (result) => {}, the result is normal make up of {status: true|false, msg: (''| {})}
|
|
1254
|
-
*/
|
|
1255
|
-
readDeviceInfo(callback) {
|
|
1256
|
-
let msg = {};
|
|
1257
|
-
msg.cmd = "read-device-info";
|
|
1258
|
-
return this.sendCommand(msg, callback);
|
|
1259
|
-
}
|
|
1260
1222
|
}
|
|
1261
1223
|
|
|
1262
1224
|
class Driver {
|
|
@@ -1276,12 +1238,16 @@ class Driver {
|
|
|
1276
1238
|
this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
|
|
1277
1239
|
this.topicResponse = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-response';
|
|
1278
1240
|
this.topicCommand = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-command';
|
|
1241
|
+
this.beatTimer = null;
|
|
1279
1242
|
}
|
|
1280
1243
|
|
|
1281
1244
|
startBeat() {
|
|
1245
|
+
if (this.beatTimer) {
|
|
1246
|
+
return;
|
|
1247
|
+
}
|
|
1282
1248
|
this.heartbeat.register();
|
|
1283
1249
|
let _this = this;
|
|
1284
|
-
setInterval(function () {
|
|
1250
|
+
this.beatTimer = setInterval(function () {
|
|
1285
1251
|
if (_this.statusInfo.status !== undefined && _this.statusInfo.status !== _this.heartbeat.STATUS.REGISTER) {
|
|
1286
1252
|
_this.heartbeat.ping(_this.statusInfo.status, _this.statusInfo.info);
|
|
1287
1253
|
}
|
|
@@ -1374,7 +1340,11 @@ class Capability {
|
|
|
1374
1340
|
this.eventListeners.get(event).add(listener);
|
|
1375
1341
|
|
|
1376
1342
|
if (!this.dataListenerRegistered) {
|
|
1377
|
-
|
|
1343
|
+
if (!listenerMap.has(this.device.topicData)) {
|
|
1344
|
+
client.subscribe(this.device.topicData);
|
|
1345
|
+
}
|
|
1346
|
+
listenerMap.set(this.device.topicData, message => this._dispatchEvent(message));
|
|
1347
|
+
this.unsubscribeData = () => listenerMap.delete(this.device.topicData);
|
|
1378
1348
|
this.dataListenerRegistered = true;
|
|
1379
1349
|
}
|
|
1380
1350
|
|
|
@@ -1407,8 +1377,9 @@ class Capability {
|
|
|
1407
1377
|
}
|
|
1408
1378
|
|
|
1409
1379
|
_dispatchEvent(message) {
|
|
1410
|
-
const
|
|
1411
|
-
const
|
|
1380
|
+
const hasDataPayload = message && Object.prototype.hasOwnProperty.call(message, "data");
|
|
1381
|
+
const event = hasDataPayload ? (message.event || message.type || "data") : "data";
|
|
1382
|
+
const payload = hasDataPayload ? message.data : message;
|
|
1412
1383
|
const dispatch = listeners => listeners && listeners.forEach(listener => listener(payload, message));
|
|
1413
1384
|
|
|
1414
1385
|
dispatch(this.eventListeners.get(event));
|
|
@@ -1488,32 +1459,119 @@ class Provider {
|
|
|
1488
1459
|
}
|
|
1489
1460
|
}
|
|
1490
1461
|
|
|
1491
|
-
|
|
1492
|
-
|
|
1462
|
+
let DeviceCapabilityBase$1 = class DeviceCapabilityBase extends Capability {
|
|
1463
|
+
constructor(name, opts = {}) {
|
|
1464
|
+
super(name, {...opts, type: opts.type || name});
|
|
1465
|
+
}
|
|
1493
1466
|
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
capabilityMap.set(key, new Capability(name, opts));
|
|
1467
|
+
init(opts = null, callback) {
|
|
1468
|
+
if (typeof opts === "function") {
|
|
1469
|
+
callback = opts;
|
|
1470
|
+
opts = null;
|
|
1499
1471
|
}
|
|
1500
|
-
return
|
|
1501
|
-
}
|
|
1472
|
+
return this.device.init(opts, callback);
|
|
1473
|
+
}
|
|
1502
1474
|
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
providerMap.set(key, new Provider(name, opts));
|
|
1507
|
-
}
|
|
1508
|
-
return providerMap.get(key);
|
|
1509
|
-
},
|
|
1475
|
+
connect(params = {}, options = null, callback = null) {
|
|
1476
|
+
return this.call("connect", params, options, callback);
|
|
1477
|
+
}
|
|
1510
1478
|
|
|
1511
|
-
|
|
1512
|
-
return this.
|
|
1513
|
-
}
|
|
1479
|
+
disconnect(params = {}, options = null, callback = null) {
|
|
1480
|
+
return this.call("disconnect", params, options, callback);
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
readDeviceInfo(params = {}, options = null, callback = null) {
|
|
1484
|
+
return this.call("readDeviceInfo", params, options, callback);
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
onData(listener) {
|
|
1488
|
+
return this.on("data", listener);
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1491
|
+
onError(listener) {
|
|
1492
|
+
return this.on("error", listener);
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
onStatus(listener) {
|
|
1496
|
+
let active = true;
|
|
1497
|
+
const statusListener = (status, raw) => {
|
|
1498
|
+
if (active) {
|
|
1499
|
+
listener(status, raw);
|
|
1500
|
+
}
|
|
1501
|
+
};
|
|
1502
|
+
const unsubscribeStatus = this.on("status", statusListener);
|
|
1503
|
+
const unsubscribeHeartbeatStatus = addDeviceStatusListener(this.type, statusListener);
|
|
1504
|
+
return () => {
|
|
1505
|
+
active = false;
|
|
1506
|
+
unsubscribeStatus();
|
|
1507
|
+
unsubscribeHeartbeatStatus();
|
|
1508
|
+
};
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
};
|
|
1512
|
+
|
|
1513
|
+
let DeviceProviderBase$1 = class DeviceProviderBase extends Provider {
|
|
1514
|
+
constructor(name, opts = {}) {
|
|
1515
|
+
super(name, {...opts, type: opts.type || name});
|
|
1516
|
+
|
|
1517
|
+
this.method("connect", (params, context) => this.connect(params, context));
|
|
1518
|
+
this.method("disconnect", (params, context) => this.disconnect(params, context));
|
|
1519
|
+
this.method("readDeviceInfo", (params, context) => this.readDeviceInfo(params, context));
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
connect(params, context) {
|
|
1523
|
+
throw new CutosError$2(
|
|
1524
|
+
"DEVICE_CONNECT_NOT_IMPLEMENTED",
|
|
1525
|
+
"connect() is not implemented."
|
|
1526
|
+
);
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
disconnect(params, context) {
|
|
1530
|
+
throw new CutosError$2(
|
|
1531
|
+
"DEVICE_DISCONNECT_NOT_IMPLEMENTED",
|
|
1532
|
+
"disconnect() is not implemented."
|
|
1533
|
+
);
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
readDeviceInfo(params, context) {
|
|
1537
|
+
throw new CutosError$2(
|
|
1538
|
+
"DEVICE_INFO_NOT_IMPLEMENTED",
|
|
1539
|
+
"readDeviceInfo() is not implemented."
|
|
1540
|
+
);
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
startBeat() {
|
|
1544
|
+
this.driver.startBeat();
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
setBeatInterval(interval) {
|
|
1548
|
+
this.driver.beatInterval = interval;
|
|
1549
|
+
return this;
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
setStatus(status = "ready", info = undefined) {
|
|
1553
|
+
const payload = {
|
|
1554
|
+
status,
|
|
1555
|
+
info,
|
|
1556
|
+
updatedAt: Date.now()
|
|
1557
|
+
};
|
|
1558
|
+
const heartbeatStatus = status === "error" || status === "offline"
|
|
1559
|
+
? cutosAPI$1.HEARTBEAT_STATUS.ERROR
|
|
1560
|
+
: cutosAPI$1.HEARTBEAT_STATUS.ALIVE;
|
|
1514
1561
|
|
|
1515
|
-
|
|
1516
|
-
|
|
1562
|
+
this.driver.updateStatusInfo(heartbeatStatus, payload);
|
|
1563
|
+
this.emit("status", payload);
|
|
1564
|
+
return this;
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
emitData(data) {
|
|
1568
|
+
return this.emit("data", data);
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
emitError(error) {
|
|
1572
|
+
const payload = normalizeError(error);
|
|
1573
|
+
this.setStatus("error", payload);
|
|
1574
|
+
return this.emit("error", payload);
|
|
1517
1575
|
}
|
|
1518
1576
|
};
|
|
1519
1577
|
|
|
@@ -1725,37 +1783,23 @@ const CoreAPI$3 = {
|
|
|
1725
1783
|
return maybePromise(callback, cb => publishRequest(cutosAPI$1.CORE.SET_HTTP_PROXY, cb, {api, target}));
|
|
1726
1784
|
},
|
|
1727
1785
|
|
|
1728
|
-
capability(name, opts = {}) {
|
|
1729
|
-
return cutos$2.capability(name, opts);
|
|
1730
|
-
},
|
|
1731
|
-
|
|
1732
|
-
provider(name, opts = {}) {
|
|
1733
|
-
return cutos$2.provider(name, opts);
|
|
1734
|
-
},
|
|
1735
|
-
|
|
1736
|
-
device(name, opts = {}) {
|
|
1737
|
-
return cutos$2.device(name, opts);
|
|
1738
|
-
},
|
|
1739
|
-
|
|
1740
|
-
driver(name, opts = {}) {
|
|
1741
|
-
return cutos$2.driver(name, opts);
|
|
1742
|
-
}
|
|
1743
1786
|
};
|
|
1744
1787
|
|
|
1745
|
-
cutos
|
|
1746
|
-
cutos
|
|
1788
|
+
cutos.CoreAPI = CoreAPI$3;
|
|
1789
|
+
cutos.CoreDefine = {
|
|
1747
1790
|
DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
|
|
1748
1791
|
};
|
|
1749
|
-
cutos
|
|
1750
|
-
Device, Driver, Database, Capability, Provider, CutosError: CutosError$2
|
|
1792
|
+
cutos.CoreClass = {
|
|
1793
|
+
Device, Driver, Database, Capability, Provider, DeviceCapabilityBase: DeviceCapabilityBase$1, DeviceProviderBase: DeviceProviderBase$1, CutosError: CutosError$2
|
|
1751
1794
|
};
|
|
1752
1795
|
|
|
1753
|
-
cutos
|
|
1754
|
-
cutos
|
|
1755
|
-
cutos
|
|
1796
|
+
cutos.cutosAPI = cutosAPI$1;
|
|
1797
|
+
cutos.CutosError = CutosError$2;
|
|
1798
|
+
cutos.DeviceCapabilityBase = DeviceCapabilityBase$1;
|
|
1799
|
+
cutos.DeviceProviderBase = DeviceProviderBase$1;
|
|
1756
1800
|
|
|
1757
1801
|
// simulate cutos core for development
|
|
1758
|
-
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos
|
|
1802
|
+
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos;
|
|
1759
1803
|
|
|
1760
1804
|
const CoreSimulator$1 = {
|
|
1761
1805
|
start: function (driver, port = 1883) {
|
|
@@ -1787,7 +1831,7 @@ const CoreSimulator$1 = {
|
|
|
1787
1831
|
|
|
1788
1832
|
var coreSimulator = {CoreSimulator: CoreSimulator$1};
|
|
1789
1833
|
|
|
1790
|
-
const {CoreAPI: CoreAPI$1,
|
|
1834
|
+
const {CoreAPI: CoreAPI$1, CutosError: CutosError$1} = cutos;
|
|
1791
1835
|
|
|
1792
1836
|
const original = {
|
|
1793
1837
|
init: CoreAPI$1.init,
|
|
@@ -1801,13 +1845,7 @@ const original = {
|
|
|
1801
1845
|
shell: CoreAPI$1.shell,
|
|
1802
1846
|
getVolume: CoreAPI$1.getVolume,
|
|
1803
1847
|
setVolume: CoreAPI$1.setVolume,
|
|
1804
|
-
setHttpProxy: CoreAPI$1.setHttpProxy
|
|
1805
|
-
capability: CoreAPI$1.capability,
|
|
1806
|
-
provider: CoreAPI$1.provider,
|
|
1807
|
-
cutosCapability: cutos$1.capability,
|
|
1808
|
-
cutosProvider: cutos$1.provider,
|
|
1809
|
-
cutosDevice: cutos$1.device,
|
|
1810
|
-
cutosDriver: cutos$1.driver
|
|
1848
|
+
setHttpProxy: CoreAPI$1.setHttpProxy
|
|
1811
1849
|
};
|
|
1812
1850
|
|
|
1813
1851
|
const mockState = {
|
|
@@ -1858,130 +1896,6 @@ function registerCapability(name, definition = {}) {
|
|
|
1858
1896
|
return record;
|
|
1859
1897
|
}
|
|
1860
1898
|
|
|
1861
|
-
class MockCapability {
|
|
1862
|
-
constructor(name) {
|
|
1863
|
-
if (!name) {
|
|
1864
|
-
throw new Error('capability name cannot be empty.');
|
|
1865
|
-
}
|
|
1866
|
-
this.name = name;
|
|
1867
|
-
this.type = name;
|
|
1868
|
-
ensureCapabilityRecord(name);
|
|
1869
|
-
}
|
|
1870
|
-
|
|
1871
|
-
call(method, params = {}, options = null, callback = null) {
|
|
1872
|
-
if (typeof options === 'function') {
|
|
1873
|
-
callback = options;
|
|
1874
|
-
options = null;
|
|
1875
|
-
}
|
|
1876
|
-
|
|
1877
|
-
const record = ensureCapabilityRecord(this.name);
|
|
1878
|
-
const handler = record.methods.get(method);
|
|
1879
|
-
|
|
1880
|
-
if (!handler) {
|
|
1881
|
-
const error = new CutosError$1('METHOD_NOT_FOUND', `Mock capability method ${this.name}.${method} is not registered.`);
|
|
1882
|
-
return resolveCallback(callback, null, error);
|
|
1883
|
-
}
|
|
1884
|
-
|
|
1885
|
-
try {
|
|
1886
|
-
const result = handler(params, {capability: this, method, options});
|
|
1887
|
-
if (result && typeof result.then === 'function') {
|
|
1888
|
-
return typeof callback === 'function'
|
|
1889
|
-
? (result.then(value => callback(value)).catch(error => callback(null, error)), true)
|
|
1890
|
-
: result;
|
|
1891
|
-
}
|
|
1892
|
-
return resolveCallback(callback, result);
|
|
1893
|
-
} catch (error) {
|
|
1894
|
-
return resolveCallback(callback, null, error);
|
|
1895
|
-
}
|
|
1896
|
-
}
|
|
1897
|
-
|
|
1898
|
-
on(event, listener) {
|
|
1899
|
-
if (typeof listener !== 'function') {
|
|
1900
|
-
throw new Error('capability event listener must be a function.');
|
|
1901
|
-
}
|
|
1902
|
-
|
|
1903
|
-
const record = ensureCapabilityRecord(this.name);
|
|
1904
|
-
if (!record.listeners.has(event)) {
|
|
1905
|
-
record.listeners.set(event, new Set());
|
|
1906
|
-
}
|
|
1907
|
-
record.listeners.get(event).add(listener);
|
|
1908
|
-
return () => this.off(event, listener);
|
|
1909
|
-
}
|
|
1910
|
-
|
|
1911
|
-
off(event, listener = null) {
|
|
1912
|
-
const record = ensureCapabilityRecord(this.name);
|
|
1913
|
-
if (!record.listeners.has(event)) {
|
|
1914
|
-
return;
|
|
1915
|
-
}
|
|
1916
|
-
if (!listener) {
|
|
1917
|
-
record.listeners.delete(event);
|
|
1918
|
-
return;
|
|
1919
|
-
}
|
|
1920
|
-
|
|
1921
|
-
const listeners = record.listeners.get(event);
|
|
1922
|
-
listeners.delete(listener);
|
|
1923
|
-
if (listeners.size === 0) {
|
|
1924
|
-
record.listeners.delete(event);
|
|
1925
|
-
}
|
|
1926
|
-
}
|
|
1927
|
-
|
|
1928
|
-
dispose() {
|
|
1929
|
-
const record = ensureCapabilityRecord(this.name);
|
|
1930
|
-
record.listeners.clear();
|
|
1931
|
-
return true;
|
|
1932
|
-
}
|
|
1933
|
-
}
|
|
1934
|
-
|
|
1935
|
-
class MockProvider {
|
|
1936
|
-
constructor(name) {
|
|
1937
|
-
if (!name) {
|
|
1938
|
-
throw new Error('provider name cannot be empty.');
|
|
1939
|
-
}
|
|
1940
|
-
this.name = name;
|
|
1941
|
-
this.type = name;
|
|
1942
|
-
ensureCapabilityRecord(name);
|
|
1943
|
-
}
|
|
1944
|
-
|
|
1945
|
-
method(method, handler) {
|
|
1946
|
-
if (typeof handler !== 'function') {
|
|
1947
|
-
throw new Error('provider method handler must be a function.');
|
|
1948
|
-
}
|
|
1949
|
-
ensureCapabilityRecord(this.name).methods.set(method, handler);
|
|
1950
|
-
return this;
|
|
1951
|
-
}
|
|
1952
|
-
|
|
1953
|
-
emit(event, data) {
|
|
1954
|
-
const record = ensureCapabilityRecord(this.name);
|
|
1955
|
-
const listeners = record.listeners.get(event);
|
|
1956
|
-
const allListeners = record.listeners.get('*');
|
|
1957
|
-
const dispatch = listener => listener(data, {event, data});
|
|
1958
|
-
|
|
1959
|
-
listeners && listeners.forEach(dispatch);
|
|
1960
|
-
allListeners && allListeners.forEach(dispatch);
|
|
1961
|
-
return true;
|
|
1962
|
-
}
|
|
1963
|
-
|
|
1964
|
-
dispose() {
|
|
1965
|
-
const record = ensureCapabilityRecord(this.name);
|
|
1966
|
-
record.methods.clear();
|
|
1967
|
-
return true;
|
|
1968
|
-
}
|
|
1969
|
-
}
|
|
1970
|
-
|
|
1971
|
-
function getMockCapability(name) {
|
|
1972
|
-
if (!mockState.capabilities.has(name)) {
|
|
1973
|
-
registerCapability(name);
|
|
1974
|
-
}
|
|
1975
|
-
return new MockCapability(name);
|
|
1976
|
-
}
|
|
1977
|
-
|
|
1978
|
-
function getMockProvider(name) {
|
|
1979
|
-
if (!mockState.providers.has(name)) {
|
|
1980
|
-
mockState.providers.set(name, new MockProvider(name));
|
|
1981
|
-
}
|
|
1982
|
-
return mockState.providers.get(name);
|
|
1983
|
-
}
|
|
1984
|
-
|
|
1985
1899
|
function installCutosMock$1(options = {}) {
|
|
1986
1900
|
mockState.options = options;
|
|
1987
1901
|
mockState.connected = false;
|
|
@@ -2013,13 +1927,6 @@ function installCutosMock$1(options = {}) {
|
|
|
2013
1927
|
return resolveCallback(callback, vol);
|
|
2014
1928
|
};
|
|
2015
1929
|
CoreAPI$1.setHttpProxy = (api, target, callback) => resolveCallback(callback, {api, target});
|
|
2016
|
-
CoreAPI$1.capability = (name) => getMockCapability(name);
|
|
2017
|
-
CoreAPI$1.provider = (name) => getMockProvider(name);
|
|
2018
|
-
|
|
2019
|
-
cutos$1.capability = (name) => getMockCapability(name);
|
|
2020
|
-
cutos$1.provider = (name) => getMockProvider(name);
|
|
2021
|
-
cutos$1.device = (name) => getMockCapability(name);
|
|
2022
|
-
cutos$1.driver = (name) => getMockProvider(name);
|
|
2023
1930
|
|
|
2024
1931
|
mockState.installed = true;
|
|
2025
1932
|
return CutosMock$1;
|
|
@@ -2038,12 +1945,6 @@ function uninstallCutosMock$1() {
|
|
|
2038
1945
|
CoreAPI$1.getVolume = original.getVolume;
|
|
2039
1946
|
CoreAPI$1.setVolume = original.setVolume;
|
|
2040
1947
|
CoreAPI$1.setHttpProxy = original.setHttpProxy;
|
|
2041
|
-
CoreAPI$1.capability = original.capability;
|
|
2042
|
-
CoreAPI$1.provider = original.provider;
|
|
2043
|
-
cutos$1.capability = original.cutosCapability;
|
|
2044
|
-
cutos$1.provider = original.cutosProvider;
|
|
2045
|
-
cutos$1.device = original.cutosDevice;
|
|
2046
|
-
cutos$1.driver = original.cutosDriver;
|
|
2047
1948
|
|
|
2048
1949
|
mockState.connected = false;
|
|
2049
1950
|
mockState.installed = false;
|
|
@@ -2055,14 +1956,20 @@ const CutosMock$1 = {
|
|
|
2055
1956
|
install: installCutosMock$1,
|
|
2056
1957
|
uninstall: uninstallCutosMock$1,
|
|
2057
1958
|
registerCapability,
|
|
2058
|
-
capability: getMockCapability,
|
|
2059
|
-
provider: getMockProvider,
|
|
2060
1959
|
isInstalled: () => mockState.installed
|
|
2061
1960
|
};
|
|
2062
1961
|
|
|
2063
1962
|
var mock = {CutosMock: CutosMock$1, installCutosMock: installCutosMock$1, uninstallCutosMock: uninstallCutosMock$1};
|
|
2064
1963
|
|
|
2065
|
-
const {
|
|
1964
|
+
const {
|
|
1965
|
+
CoreAPI,
|
|
1966
|
+
CoreDefine,
|
|
1967
|
+
CoreClass,
|
|
1968
|
+
cutosAPI,
|
|
1969
|
+
CutosError,
|
|
1970
|
+
DeviceCapabilityBase,
|
|
1971
|
+
DeviceProviderBase
|
|
1972
|
+
} = cutos;
|
|
2066
1973
|
const {CoreSimulator} = coreSimulator;
|
|
2067
1974
|
const {CutosMock, installCutosMock, uninstallCutosMock} = mock;
|
|
2068
1975
|
|
|
@@ -2084,8 +1991,9 @@ var CoreAPI_1 = src.CoreAPI = CoreAPI;
|
|
|
2084
1991
|
var CoreDefine_1 = src.CoreDefine = CoreDefine;
|
|
2085
1992
|
var CoreClass_1 = src.CoreClass = CoreClass;
|
|
2086
1993
|
var cutosAPI_1 = src.cutosAPI = cutosAPI;
|
|
2087
|
-
var cutos_1 = src.cutos = cutos;
|
|
2088
1994
|
var CutosError_1 = src.CutosError = CutosError;
|
|
1995
|
+
var DeviceCapabilityBase_1 = src.DeviceCapabilityBase = DeviceCapabilityBase;
|
|
1996
|
+
var DeviceProviderBase_1 = src.DeviceProviderBase = DeviceProviderBase;
|
|
2089
1997
|
var CoreSimulator_1 = src.CoreSimulator = CoreSimulator;
|
|
2090
1998
|
var CutosMock_1 = src.CutosMock = CutosMock;
|
|
2091
1999
|
var installCutosMock_1 = src.installCutosMock = installCutosMock;
|
|
@@ -2097,7 +2005,8 @@ exports.CoreDefine = CoreDefine_1;
|
|
|
2097
2005
|
exports.CoreSimulator = CoreSimulator_1;
|
|
2098
2006
|
exports.CutosError = CutosError_1;
|
|
2099
2007
|
exports.CutosMock = CutosMock_1;
|
|
2100
|
-
exports.
|
|
2008
|
+
exports.DeviceCapabilityBase = DeviceCapabilityBase_1;
|
|
2009
|
+
exports.DeviceProviderBase = DeviceProviderBase_1;
|
|
2101
2010
|
exports.cutosAPI = cutosAPI_1;
|
|
2102
2011
|
exports.default = src;
|
|
2103
2012
|
exports.installCutosMock = installCutosMock_1;
|