@homelib/xiaomi 0.0.0-wip.ba4eafa051eb
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/LICENSE +19 -0
- package/README.md +22 -0
- package/bld/library/backend/client.d.ts +68 -0
- package/bld/library/backend/client.js +477 -0
- package/bld/library/backend/client.js.map +1 -0
- package/bld/library/backend/constants.d.ts +7 -0
- package/bld/library/backend/constants.js +11 -0
- package/bld/library/backend/constants.js.map +1 -0
- package/bld/library/backend/index.d.ts +3 -0
- package/bld/library/backend/index.js +4 -0
- package/bld/library/backend/index.js.map +1 -0
- package/bld/library/backend/oauth.d.ts +24 -0
- package/bld/library/backend/oauth.js +121 -0
- package/bld/library/backend/oauth.js.map +1 -0
- package/bld/library/binding.d.ts +44 -0
- package/bld/library/binding.js +212 -0
- package/bld/library/binding.js.map +1 -0
- package/bld/library/cloud/client.d.ts +27 -0
- package/bld/library/cloud/client.js +109 -0
- package/bld/library/cloud/client.js.map +1 -0
- package/bld/library/cloud/constants.d.ts +9 -0
- package/bld/library/cloud/constants.js +11 -0
- package/bld/library/cloud/constants.js.map +1 -0
- package/bld/library/cloud/device.d.ts +84 -0
- package/bld/library/cloud/device.js +686 -0
- package/bld/library/cloud/device.js.map +1 -0
- package/bld/library/cloud/index.d.ts +5 -0
- package/bld/library/cloud/index.js +6 -0
- package/bld/library/cloud/index.js.map +1 -0
- package/bld/library/cloud/mqtt.d.ts +69 -0
- package/bld/library/cloud/mqtt.js +517 -0
- package/bld/library/cloud/mqtt.js.map +1 -0
- package/bld/library/cloud/transport.d.ts +8 -0
- package/bld/library/cloud/transport.js +30 -0
- package/bld/library/cloud/transport.js.map +1 -0
- package/bld/library/command.d.ts +6 -0
- package/bld/library/command.js +6 -0
- package/bld/library/command.js.map +1 -0
- package/bld/library/configuration.d.ts +62 -0
- package/bld/library/configuration.js +303 -0
- package/bld/library/configuration.js.map +1 -0
- package/bld/library/device.d.ts +68 -0
- package/bld/library/device.js +235 -0
- package/bld/library/device.js.map +1 -0
- package/bld/library/devices/air-conditioner.d.ts +48 -0
- package/bld/library/devices/air-conditioner.js +155 -0
- package/bld/library/devices/air-conditioner.js.map +1 -0
- package/bld/library/devices/command-effect.d.ts +35 -0
- package/bld/library/devices/command-effect.js +269 -0
- package/bld/library/devices/command-effect.js.map +1 -0
- package/bld/library/devices/dehumidifier.d.ts +41 -0
- package/bld/library/devices/dehumidifier.js +124 -0
- package/bld/library/devices/dehumidifier.js.map +1 -0
- package/bld/library/devices/fan.d.ts +37 -0
- package/bld/library/devices/fan.js +135 -0
- package/bld/library/devices/fan.js.map +1 -0
- package/bld/library/devices/index.d.ts +7 -0
- package/bld/library/devices/index.js +8 -0
- package/bld/library/devices/index.js.map +1 -0
- package/bld/library/devices/light.d.ts +22 -0
- package/bld/library/devices/light.js +112 -0
- package/bld/library/devices/light.js.map +1 -0
- package/bld/library/devices/placeholder.d.ts +3 -0
- package/bld/library/devices/placeholder.js +4 -0
- package/bld/library/devices/placeholder.js.map +1 -0
- package/bld/library/devices/value-range.d.ts +2 -0
- package/bld/library/devices/value-range.js +6 -0
- package/bld/library/devices/value-range.js.map +1 -0
- package/bld/library/endpoint-connection.d.ts +154 -0
- package/bld/library/endpoint-connection.js +510 -0
- package/bld/library/endpoint-connection.js.map +1 -0
- package/bld/library/index.d.ts +26 -0
- package/bld/library/index.js +31 -0
- package/bld/library/index.js.map +1 -0
- package/bld/library/local/certificate.d.ts +31 -0
- package/bld/library/local/certificate.js +298 -0
- package/bld/library/local/certificate.js.map +1 -0
- package/bld/library/local/controller.d.ts +70 -0
- package/bld/library/local/controller.js +497 -0
- package/bld/library/local/controller.js.map +1 -0
- package/bld/library/local/discovery.d.ts +29 -0
- package/bld/library/local/discovery.js +321 -0
- package/bld/library/local/discovery.js.map +1 -0
- package/bld/library/local/index.d.ts +6 -0
- package/bld/library/local/index.js +7 -0
- package/bld/library/local/index.js.map +1 -0
- package/bld/library/local/message.d.ts +11 -0
- package/bld/library/local/message.js +128 -0
- package/bld/library/local/message.js.map +1 -0
- package/bld/library/local/mqtt.d.ts +113 -0
- package/bld/library/local/mqtt.js +1044 -0
- package/bld/library/local/mqtt.js.map +1 -0
- package/bld/library/local/routed-message-client.d.ts +49 -0
- package/bld/library/local/routed-message-client.js +326 -0
- package/bld/library/local/routed-message-client.js.map +1 -0
- package/bld/library/miot/index.d.ts +4 -0
- package/bld/library/miot/index.js +5 -0
- package/bld/library/miot/index.js.map +1 -0
- package/bld/library/miot/matcher.d.ts +65 -0
- package/bld/library/miot/matcher.js +217 -0
- package/bld/library/miot/matcher.js.map +1 -0
- package/bld/library/miot/request.d.ts +16 -0
- package/bld/library/miot/request.js +27 -0
- package/bld/library/miot/request.js.map +1 -0
- package/bld/library/miot/result.d.ts +4 -0
- package/bld/library/miot/result.js +5 -0
- package/bld/library/miot/result.js.map +1 -0
- package/bld/library/miot/spec.d.ts +74 -0
- package/bld/library/miot/spec.js +78 -0
- package/bld/library/miot/spec.js.map +1 -0
- package/bld/library/provider.d.ts +71 -0
- package/bld/library/provider.js +600 -0
- package/bld/library/provider.js.map +1 -0
- package/bld/library/session-manager.d.ts +34 -0
- package/bld/library/session-manager.js +177 -0
- package/bld/library/session-manager.js.map +1 -0
- package/bld/library/session.d.ts +36 -0
- package/bld/library/session.js +370 -0
- package/bld/library/session.js.map +1 -0
- package/bld/library/storage.d.ts +2 -0
- package/bld/library/storage.js +66 -0
- package/bld/library/storage.js.map +1 -0
- package/bld/library/terminal/bindings.d.ts +3 -0
- package/bld/library/terminal/bindings.js +280 -0
- package/bld/library/terminal/bindings.js.map +1 -0
- package/bld/library/terminal/index.d.ts +2 -0
- package/bld/library/terminal/index.js +3 -0
- package/bld/library/terminal/index.js.map +1 -0
- package/bld/library/terminal/provider-details.d.ts +3 -0
- package/bld/library/terminal/provider-details.js +477 -0
- package/bld/library/terminal/provider-details.js.map +1 -0
- package/package.json +29 -0
- package/src/library/backend/client.ts +713 -0
- package/src/library/backend/constants.ts +17 -0
- package/src/library/backend/index.ts +3 -0
- package/src/library/backend/oauth.ts +170 -0
- package/src/library/binding.ts +387 -0
- package/src/library/cloud/client.ts +155 -0
- package/src/library/cloud/constants.ts +19 -0
- package/src/library/cloud/device.ts +938 -0
- package/src/library/cloud/index.ts +5 -0
- package/src/library/cloud/mqtt.ts +671 -0
- package/src/library/cloud/transport.ts +38 -0
- package/src/library/command.ts +7 -0
- package/src/library/configuration.ts +480 -0
- package/src/library/device.ts +548 -0
- package/src/library/devices/air-conditioner.ts +171 -0
- package/src/library/devices/command-effect.ts +425 -0
- package/src/library/devices/dehumidifier.ts +131 -0
- package/src/library/devices/fan.ts +140 -0
- package/src/library/devices/index.ts +7 -0
- package/src/library/devices/light.ts +119 -0
- package/src/library/devices/placeholder.ts +3 -0
- package/src/library/devices/value-range.ts +14 -0
- package/src/library/endpoint-connection.ts +841 -0
- package/src/library/index.ts +62 -0
- package/src/library/local/certificate.ts +401 -0
- package/src/library/local/controller.ts +692 -0
- package/src/library/local/discovery.ts +485 -0
- package/src/library/local/index.ts +6 -0
- package/src/library/local/message.ts +174 -0
- package/src/library/local/mqtt.ts +1493 -0
- package/src/library/local/routed-message-client.ts +377 -0
- package/src/library/miot/index.ts +4 -0
- package/src/library/miot/matcher.ts +461 -0
- package/src/library/miot/request.ts +30 -0
- package/src/library/miot/result.ts +10 -0
- package/src/library/miot/spec.ts +103 -0
- package/src/library/provider.ts +745 -0
- package/src/library/session-manager.ts +224 -0
- package/src/library/session.ts +528 -0
- package/src/library/storage.ts +83 -0
- package/src/library/terminal/bindings.tsx +612 -0
- package/src/library/terminal/index.ts +2 -0
- package/src/library/terminal/provider-details.tsx +871 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CloudDeviceMessageClient,
|
|
3
|
+
CloudMqttConnectionStateListener,
|
|
4
|
+
CloudMqttDeviceMessageHandler,
|
|
5
|
+
} from '../cloud/index.js';
|
|
6
|
+
import type {MiotProperty} from '../miot/index.js';
|
|
7
|
+
|
|
8
|
+
export class RoutedDeviceMessageClient implements CloudDeviceMessageClient {
|
|
9
|
+
private readonly subscriptionMap = new Map<string, RoutedSubscription>();
|
|
10
|
+
|
|
11
|
+
private readonly connectionStateListenerSet =
|
|
12
|
+
new Set<CloudMqttConnectionStateListener>();
|
|
13
|
+
|
|
14
|
+
private cloudConnected = false;
|
|
15
|
+
|
|
16
|
+
private connectedValue = false;
|
|
17
|
+
|
|
18
|
+
private disconnecting = false;
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private readonly cloud: CloudDeviceMessageClient,
|
|
22
|
+
private readonly local: RoutedLocalMessageSource,
|
|
23
|
+
) {
|
|
24
|
+
this.cloud.addConnectionStateListener(connected => {
|
|
25
|
+
const wasConnected = this.connectedValue;
|
|
26
|
+
const cloudChanged = this.cloudConnected !== connected;
|
|
27
|
+
this.cloudConnected = connected;
|
|
28
|
+
this.updateConnected();
|
|
29
|
+
void this.refreshSubscriptions().catch(console.error);
|
|
30
|
+
|
|
31
|
+
if (cloudChanged && wasConnected && this.connectedValue) {
|
|
32
|
+
this.notifyConnectionState(false);
|
|
33
|
+
this.notifyConnectionState(true);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
this.local.addRoutesChangedListener(() => {
|
|
37
|
+
this.updateConnected();
|
|
38
|
+
void this.refreshSubscriptions().catch(console.error);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
updateAccessToken(accessToken: string): void {
|
|
43
|
+
this.cloud.updateAccessToken(accessToken);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async connect(): Promise<void> {
|
|
47
|
+
if (this.disconnecting) {
|
|
48
|
+
throw new Error('MIoT message client is disconnecting.');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const localOperation = this.local.connect().then(() => {
|
|
52
|
+
if (!this.local.connected) {
|
|
53
|
+
throw new Error('No local MIoT message route is connected.');
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
await Promise.any([this.cloud.connect(), localOperation]);
|
|
58
|
+
|
|
59
|
+
this.updateConnected();
|
|
60
|
+
await this.refreshSubscriptions();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async disconnect(): Promise<void> {
|
|
64
|
+
if (this.disconnecting) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
this.disconnecting = true;
|
|
69
|
+
const errors: unknown[] = [];
|
|
70
|
+
const subscriptions = [...this.subscriptionMap.values()];
|
|
71
|
+
this.subscriptionMap.clear();
|
|
72
|
+
|
|
73
|
+
for (const subscription of subscriptions) {
|
|
74
|
+
try {
|
|
75
|
+
await subscription.dispose();
|
|
76
|
+
} catch (error) {
|
|
77
|
+
errors.push(error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const results = await Promise.allSettled([
|
|
82
|
+
this.local.disconnect(),
|
|
83
|
+
this.cloud.disconnect(),
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
for (const result of results) {
|
|
87
|
+
if (result.status === 'rejected') {
|
|
88
|
+
errors.push(result.reason);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.cloudConnected = false;
|
|
93
|
+
this.updateConnected();
|
|
94
|
+
this.disconnecting = false;
|
|
95
|
+
|
|
96
|
+
if (errors.length === 1) {
|
|
97
|
+
throw errors[0];
|
|
98
|
+
} else if (errors.length > 1) {
|
|
99
|
+
throw new AggregateError(errors, 'Failed to disconnect MIoT messages.');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async subscribeDevice(
|
|
104
|
+
did: string,
|
|
105
|
+
handler: CloudMqttDeviceMessageHandler,
|
|
106
|
+
): Promise<void> {
|
|
107
|
+
if (this.disconnecting) {
|
|
108
|
+
throw new Error('MIoT message client is disconnecting.');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const existing = this.subscriptionMap.get(did);
|
|
112
|
+
|
|
113
|
+
if (existing !== undefined) {
|
|
114
|
+
if (existing.handler !== handler) {
|
|
115
|
+
throw new Error(`MIoT device ${did} is already subscribed.`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const subscription = new RoutedSubscription(did, handler, this);
|
|
122
|
+
this.subscriptionMap.set(did, subscription);
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
await subscription.refresh();
|
|
126
|
+
} catch (error) {
|
|
127
|
+
this.subscriptionMap.delete(did);
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async unsubscribeDevice(did: string): Promise<void> {
|
|
133
|
+
const subscription = this.subscriptionMap.get(did);
|
|
134
|
+
|
|
135
|
+
if (subscription === undefined) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
this.subscriptionMap.delete(did);
|
|
140
|
+
await subscription.dispose();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
addConnectionStateListener(
|
|
144
|
+
listener: CloudMqttConnectionStateListener,
|
|
145
|
+
): () => void {
|
|
146
|
+
this.connectionStateListenerSet.add(listener);
|
|
147
|
+
|
|
148
|
+
return () => {
|
|
149
|
+
this.connectionStateListenerSet.delete(listener);
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
resolveSource(did: string): RoutedMessageSource {
|
|
154
|
+
const localSource = this.local.getMessageSource(did);
|
|
155
|
+
|
|
156
|
+
if (localSource !== undefined) {
|
|
157
|
+
return {
|
|
158
|
+
identity: localSource,
|
|
159
|
+
subscribe: async handler => {
|
|
160
|
+
const subscriptions: Array<{dispose(): Promise<void>}> = [];
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
subscriptions.push(
|
|
164
|
+
await localSource.subscribeProperties(did, update => {
|
|
165
|
+
handler({...update, type: 'property'});
|
|
166
|
+
}),
|
|
167
|
+
);
|
|
168
|
+
subscriptions.push(
|
|
169
|
+
await localSource.subscribeEvents(did, update => {
|
|
170
|
+
handler({...update, type: 'event'});
|
|
171
|
+
}),
|
|
172
|
+
);
|
|
173
|
+
} catch (error) {
|
|
174
|
+
await disposeSubscriptions(subscriptions).catch(console.error);
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return () => disposeSubscriptions(subscriptions);
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
identity: this.cloud,
|
|
185
|
+
subscribe: async handler => {
|
|
186
|
+
await this.cloud.subscribeDevice(did, handler);
|
|
187
|
+
return () => this.cloud.unsubscribeDevice(did);
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private async refreshSubscriptions(): Promise<void> {
|
|
193
|
+
const results = await Promise.allSettled(
|
|
194
|
+
[...this.subscriptionMap.values()].map(subscription =>
|
|
195
|
+
subscription.refresh(),
|
|
196
|
+
),
|
|
197
|
+
);
|
|
198
|
+
let changed = false;
|
|
199
|
+
|
|
200
|
+
for (const result of results) {
|
|
201
|
+
if (result.status === 'rejected') {
|
|
202
|
+
console.error(result.reason);
|
|
203
|
+
} else if (result.value) {
|
|
204
|
+
changed = true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (changed && this.connectedValue) {
|
|
209
|
+
this.notifyConnectionState(false);
|
|
210
|
+
this.notifyConnectionState(true);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private updateConnected(): void {
|
|
215
|
+
const connected = this.cloudConnected || this.local.connected;
|
|
216
|
+
|
|
217
|
+
if (this.connectedValue === connected) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
this.connectedValue = connected;
|
|
222
|
+
this.notifyConnectionState(connected);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private notifyConnectionState(connected: boolean): void {
|
|
226
|
+
for (const listener of this.connectionStateListenerSet) {
|
|
227
|
+
try {
|
|
228
|
+
listener(connected);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
console.error(error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
class RoutedSubscription {
|
|
237
|
+
private sourceSubscription: ActiveSourceSubscription | undefined;
|
|
238
|
+
|
|
239
|
+
private operation: Promise<void> = Promise.resolve();
|
|
240
|
+
|
|
241
|
+
private disposed = false;
|
|
242
|
+
|
|
243
|
+
constructor(
|
|
244
|
+
readonly did: string,
|
|
245
|
+
readonly handler: CloudMqttDeviceMessageHandler,
|
|
246
|
+
private readonly client: RoutedDeviceMessageClient,
|
|
247
|
+
) {}
|
|
248
|
+
|
|
249
|
+
refresh(): Promise<boolean> {
|
|
250
|
+
if (this.disposed) {
|
|
251
|
+
return Promise.resolve(false);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
let changed = false;
|
|
255
|
+
const operation = this.operation.then(async () => {
|
|
256
|
+
if (this.disposed) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const source = this.client.resolveSource(this.did);
|
|
261
|
+
const previous = this.sourceSubscription;
|
|
262
|
+
|
|
263
|
+
if (previous?.identity === source.identity) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const token = {};
|
|
268
|
+
let dispose: (() => Promise<void>) | undefined;
|
|
269
|
+
|
|
270
|
+
try {
|
|
271
|
+
dispose = await source.subscribe(message => {
|
|
272
|
+
if (this.sourceSubscription?.token === token) {
|
|
273
|
+
this.handler(message);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
this.sourceSubscription = {
|
|
277
|
+
identity: source.identity,
|
|
278
|
+
token,
|
|
279
|
+
dispose,
|
|
280
|
+
};
|
|
281
|
+
changed = true;
|
|
282
|
+
} catch (error) {
|
|
283
|
+
if (dispose !== undefined) {
|
|
284
|
+
await dispose().catch(console.error);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (this.sourceSubscription?.token === token) {
|
|
288
|
+
this.sourceSubscription = previous;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (previous !== undefined) {
|
|
295
|
+
await previous.dispose().catch(console.error);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
this.operation = operation.catch(() => undefined);
|
|
300
|
+
return operation.then(() => changed);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async dispose(): Promise<void> {
|
|
304
|
+
if (this.disposed) {
|
|
305
|
+
await this.operation;
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
this.disposed = true;
|
|
310
|
+
const operation = this.operation.then(async () => {
|
|
311
|
+
const subscription = this.sourceSubscription;
|
|
312
|
+
this.sourceSubscription = undefined;
|
|
313
|
+
await subscription?.dispose();
|
|
314
|
+
});
|
|
315
|
+
this.operation = operation.catch(() => undefined);
|
|
316
|
+
await operation;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export type RoutedLocalMessageSource = {
|
|
321
|
+
readonly connected: boolean;
|
|
322
|
+
connect(): Promise<void>;
|
|
323
|
+
disconnect(): Promise<void>;
|
|
324
|
+
addRoutesChangedListener(listener: () => void): () => void;
|
|
325
|
+
getMessageSource(did: string): LocalDeviceMessageSource | undefined;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
export type LocalDeviceMessageSource = {
|
|
329
|
+
subscribeProperties(
|
|
330
|
+
did: string,
|
|
331
|
+
listener: (update: MiotProperty & {readonly value: unknown}) => void,
|
|
332
|
+
): Promise<{dispose(): Promise<void>}>;
|
|
333
|
+
subscribeEvents(
|
|
334
|
+
did: string,
|
|
335
|
+
listener: (update: {
|
|
336
|
+
readonly did: string;
|
|
337
|
+
readonly siid: number;
|
|
338
|
+
readonly eiid: number;
|
|
339
|
+
readonly arguments: readonly unknown[];
|
|
340
|
+
}) => void,
|
|
341
|
+
): Promise<{dispose(): Promise<void>}>;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
type RoutedMessageSource = {
|
|
345
|
+
readonly identity: object;
|
|
346
|
+
subscribe(
|
|
347
|
+
handler: CloudMqttDeviceMessageHandler,
|
|
348
|
+
): Promise<() => Promise<void>>;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
type ActiveSourceSubscription = {
|
|
352
|
+
readonly identity: object;
|
|
353
|
+
readonly token: object;
|
|
354
|
+
readonly dispose: () => Promise<void>;
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
async function disposeSubscriptions(
|
|
358
|
+
subscriptions: readonly {dispose(): Promise<void>}[],
|
|
359
|
+
): Promise<void> {
|
|
360
|
+
const results = await Promise.allSettled(
|
|
361
|
+
subscriptions.map(subscription => subscription.dispose()),
|
|
362
|
+
);
|
|
363
|
+
const errors = results
|
|
364
|
+
.filter(
|
|
365
|
+
(result): result is PromiseRejectedResult => result.status === 'rejected',
|
|
366
|
+
)
|
|
367
|
+
.map(result => result.reason as unknown);
|
|
368
|
+
|
|
369
|
+
if (errors.length === 1) {
|
|
370
|
+
throw errors[0];
|
|
371
|
+
} else if (errors.length > 1) {
|
|
372
|
+
throw new AggregateError(
|
|
373
|
+
errors,
|
|
374
|
+
'Failed to unsubscribe local MIoT messages.',
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
}
|