@devicechain/dashboards 1.6.0 → 1.7.0-rc.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/dist/hub.d.ts +5 -0
- package/dist/index.js +152 -14
- package/dist/index.js.map +2 -2
- package/dist/internal/measurement-doc.d.ts +2 -0
- package/package.json +3 -3
package/dist/hub.d.ts
CHANGED
|
@@ -108,6 +108,8 @@ export declare class DashboardHub implements WidgetDataSource, WidgetActions {
|
|
|
108
108
|
private readonly alarmReconcilers;
|
|
109
109
|
private readonly commandReconcilers;
|
|
110
110
|
private readonly streams;
|
|
111
|
+
private readonly groups;
|
|
112
|
+
private multiDeviceUnsupported;
|
|
111
113
|
private readonly alarmDisposers;
|
|
112
114
|
private readonly commandDisposers;
|
|
113
115
|
private readonly locationDisposers;
|
|
@@ -156,6 +158,9 @@ export declare class DashboardHub implements WidgetDataSource, WidgetActions {
|
|
|
156
158
|
private availabilityToken;
|
|
157
159
|
private resolveDevices;
|
|
158
160
|
private resolveBinding;
|
|
161
|
+
private attachDevices;
|
|
162
|
+
private fallbackGroup;
|
|
163
|
+
private openGroup;
|
|
159
164
|
private attach;
|
|
160
165
|
private ensureStream;
|
|
161
166
|
private fanout;
|
package/dist/index.js
CHANGED
|
@@ -2286,6 +2286,19 @@ var MEASUREMENT_STREAM = `
|
|
|
2286
2286
|
}
|
|
2287
2287
|
}
|
|
2288
2288
|
`;
|
|
2289
|
+
var MEASUREMENT_STREAM_MULTI = `
|
|
2290
|
+
subscription MeasurementStream($deviceTokens: [String!], $name: String) {
|
|
2291
|
+
measurementStream(deviceTokens: $deviceTokens, name: $name) {
|
|
2292
|
+
id
|
|
2293
|
+
deviceToken
|
|
2294
|
+
eventType
|
|
2295
|
+
occurredTime
|
|
2296
|
+
name
|
|
2297
|
+
value
|
|
2298
|
+
classifier
|
|
2299
|
+
}
|
|
2300
|
+
}
|
|
2301
|
+
`;
|
|
2289
2302
|
|
|
2290
2303
|
// src/hub.ts
|
|
2291
2304
|
var EVENT_AREA = "event-management";
|
|
@@ -2305,6 +2318,13 @@ var LOCATION_POLL_MS = 15e3;
|
|
|
2305
2318
|
var PRESENCE_POLL_MS = 15e3;
|
|
2306
2319
|
var PRESENCE_CHUNK = 1e3;
|
|
2307
2320
|
var STAMPED = /* @__PURE__ */ new Set(["customer", "area", "asset"]);
|
|
2321
|
+
function refusesDeviceTokens(err) {
|
|
2322
|
+
if (Array.isArray(err)) return err.some(refusesDeviceTokens);
|
|
2323
|
+
if (!err || typeof err !== "object") return false;
|
|
2324
|
+
const e = err;
|
|
2325
|
+
if (typeof e.message === "string" && /(?:unknown argument ["']deviceTokens["']|argument ["']deviceTokens["'][^\n]*not defined)/i.test(e.message)) return true;
|
|
2326
|
+
return refusesDeviceTokens(e.errors) || refusesDeviceTokens(e.graphQLErrors);
|
|
2327
|
+
}
|
|
2308
2328
|
var DashboardHub = class {
|
|
2309
2329
|
constructor(config) {
|
|
2310
2330
|
// Per alarm-subscription reconcile triggers — invoked after an ack/clear so the
|
|
@@ -2315,6 +2335,8 @@ var DashboardHub = class {
|
|
|
2315
2335
|
this.commandReconcilers = /* @__PURE__ */ new Set();
|
|
2316
2336
|
// One entry per distinct device token that has at least one subscriber.
|
|
2317
2337
|
this.streams = /* @__PURE__ */ new Map();
|
|
2338
|
+
this.groups = /* @__PURE__ */ new Set();
|
|
2339
|
+
this.multiDeviceUnsupported = false;
|
|
2318
2340
|
// Live alarm-subscription disposers. The alarm channel isn't ref-counted through
|
|
2319
2341
|
// `streams` (it holds a poll/debounce/trigger per subscription, not a shared device
|
|
2320
2342
|
// stream), so its disposers are tracked here for disposeAll() to reach — otherwise an
|
|
@@ -2379,9 +2401,7 @@ var DashboardHub = class {
|
|
|
2379
2401
|
};
|
|
2380
2402
|
this.resolveDevices(datasource).then((groups) => {
|
|
2381
2403
|
if (disposed) return;
|
|
2382
|
-
|
|
2383
|
-
detachers.push(this.attach(group.deviceToken, group.names, sink));
|
|
2384
|
-
}
|
|
2404
|
+
detachers.push(...this.attachDevices(groups, sink, datasource.kind === "anchor"));
|
|
2385
2405
|
}).catch((err) => {
|
|
2386
2406
|
if (!disposed) sink.error?.(err);
|
|
2387
2407
|
});
|
|
@@ -2397,15 +2417,15 @@ var DashboardHub = class {
|
|
|
2397
2417
|
this.resolveRoster(datasource).then(({ roster, names }) => {
|
|
2398
2418
|
if (disposed) return;
|
|
2399
2419
|
sink.roster(roster);
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2420
|
+
detachers.push(...this.attachDevices(
|
|
2421
|
+
roster.members.map((member) => ({ deviceToken: member.token, names })),
|
|
2422
|
+
{
|
|
2423
|
+
next: (sample) => sink.next(sample),
|
|
2424
|
+
error: (err) => sink.error?.(err),
|
|
2425
|
+
reconnected: () => sink.reconnected?.()
|
|
2426
|
+
},
|
|
2427
|
+
datasource.kind === "anchor"
|
|
2428
|
+
));
|
|
2409
2429
|
}).catch((err) => {
|
|
2410
2430
|
if (disposed) return;
|
|
2411
2431
|
if (isForbiddenError(err)) sink.forbidden?.();
|
|
@@ -2921,6 +2941,7 @@ var DashboardHub = class {
|
|
|
2921
2941
|
// subscription's poll + trigger. Iterate a copy of the disposer sets since each
|
|
2922
2942
|
// removes itself as it runs.
|
|
2923
2943
|
disposeAll() {
|
|
2944
|
+
for (const group of [...this.groups]) group.unsubscribe();
|
|
2924
2945
|
for (const stream of this.streams.values()) stream.unsubscribe();
|
|
2925
2946
|
this.streams.clear();
|
|
2926
2947
|
for (const stream of this.deadStreams) stream.unsubscribe();
|
|
@@ -3014,6 +3035,123 @@ var DashboardHub = class {
|
|
|
3014
3035
|
const tokens = await this.resolver.devicesForAnchor(binding.anchor);
|
|
3015
3036
|
return tokens.map((deviceToken) => ({ deviceToken, names }));
|
|
3016
3037
|
}
|
|
3038
|
+
// Attach before opening a group, including for a synchronous validation refusal.
|
|
3039
|
+
// Existing streams are shared, so an overlapping device is never subscribed twice.
|
|
3040
|
+
attachDevices(devices, sink, grouped) {
|
|
3041
|
+
if (!grouped || this.multiDeviceUnsupported) {
|
|
3042
|
+
return devices.map((d) => this.attach(d.deviceToken, d.names, sink));
|
|
3043
|
+
}
|
|
3044
|
+
const missing = [...new Set(devices.map((d) => d.deviceToken))].filter((t) => !this.streams.has(t));
|
|
3045
|
+
const pending = [];
|
|
3046
|
+
for (let i = 0; i < missing.length; i += 1e3) {
|
|
3047
|
+
const group = { members: /* @__PURE__ */ new Map(), unsubscribe: () => {
|
|
3048
|
+
} };
|
|
3049
|
+
for (const token of missing.slice(i, i + 1e3)) {
|
|
3050
|
+
const stream = {
|
|
3051
|
+
subscribers: /* @__PURE__ */ new Set(),
|
|
3052
|
+
unsubscribe: () => {
|
|
3053
|
+
group.members.delete(token);
|
|
3054
|
+
if (group.members.size === 0) group.unsubscribe();
|
|
3055
|
+
}
|
|
3056
|
+
};
|
|
3057
|
+
group.members.set(token, stream);
|
|
3058
|
+
this.streams.set(token, stream);
|
|
3059
|
+
}
|
|
3060
|
+
pending.push(group);
|
|
3061
|
+
}
|
|
3062
|
+
const detachers = devices.map((d) => this.attach(d.deviceToken, d.names, sink));
|
|
3063
|
+
for (const group of pending) {
|
|
3064
|
+
if (this.multiDeviceUnsupported) this.fallbackGroup(group);
|
|
3065
|
+
else this.openGroup(group);
|
|
3066
|
+
}
|
|
3067
|
+
return detachers;
|
|
3068
|
+
}
|
|
3069
|
+
fallbackGroup(group) {
|
|
3070
|
+
group.unsubscribe();
|
|
3071
|
+
for (const [token, stream] of group.members) {
|
|
3072
|
+
if (this.streams.get(token) === stream) this.streams.delete(token);
|
|
3073
|
+
if (stream.subscribers.size) this.ensureStream(token, stream);
|
|
3074
|
+
}
|
|
3075
|
+
group.members.clear();
|
|
3076
|
+
}
|
|
3077
|
+
openGroup(group) {
|
|
3078
|
+
this.groups.add(group);
|
|
3079
|
+
const handle = this.connection.register();
|
|
3080
|
+
const variables = { deviceTokens: [...group.members.keys()], name: null };
|
|
3081
|
+
let closeSocket;
|
|
3082
|
+
let operationOpen = true;
|
|
3083
|
+
let up = true;
|
|
3084
|
+
let ended = 0;
|
|
3085
|
+
let reopen;
|
|
3086
|
+
const closeOperation = () => {
|
|
3087
|
+
if (!operationOpen) return;
|
|
3088
|
+
operationOpen = false;
|
|
3089
|
+
clearTimeout(reopen);
|
|
3090
|
+
closeSocket?.();
|
|
3091
|
+
};
|
|
3092
|
+
group.unsubscribe = () => {
|
|
3093
|
+
closeOperation();
|
|
3094
|
+
handle.release();
|
|
3095
|
+
this.groups.delete(group);
|
|
3096
|
+
};
|
|
3097
|
+
const resumed = () => {
|
|
3098
|
+
if (!operationOpen) return;
|
|
3099
|
+
for (const stream of group.members.values()) {
|
|
3100
|
+
for (const subscriber of stream.subscribers) subscriber.sink.reconnected?.();
|
|
3101
|
+
}
|
|
3102
|
+
};
|
|
3103
|
+
const open = () => {
|
|
3104
|
+
closeSocket = subscribe(EVENT_AREA, MEASUREMENT_STREAM_MULTI, variables, adapter);
|
|
3105
|
+
if (!operationOpen) closeSocket();
|
|
3106
|
+
};
|
|
3107
|
+
const adapter = {
|
|
3108
|
+
errors: (errs) => {
|
|
3109
|
+
if (operationOpen && refusesDeviceTokens({ errors: errs })) {
|
|
3110
|
+
this.multiDeviceUnsupported = true;
|
|
3111
|
+
this.fallbackGroup(group);
|
|
3112
|
+
}
|
|
3113
|
+
},
|
|
3114
|
+
next: (data) => {
|
|
3115
|
+
if (!operationOpen) return;
|
|
3116
|
+
ended = 0;
|
|
3117
|
+
const sample = data.measurementStream;
|
|
3118
|
+
const stream = group.members.get(sample.deviceToken);
|
|
3119
|
+
if (stream && this.streams.get(sample.deviceToken) === stream) this.fanout(sample.deviceToken, sample);
|
|
3120
|
+
},
|
|
3121
|
+
connected: (wasRetry) => {
|
|
3122
|
+
up = true;
|
|
3123
|
+
handle.connected();
|
|
3124
|
+
if (wasRetry) resumed();
|
|
3125
|
+
},
|
|
3126
|
+
closed: () => {
|
|
3127
|
+
up = false;
|
|
3128
|
+
handle.closed();
|
|
3129
|
+
},
|
|
3130
|
+
complete: () => {
|
|
3131
|
+
if (!operationOpen || !up) return;
|
|
3132
|
+
closeSocket?.();
|
|
3133
|
+
reopen = setTimeout(() => {
|
|
3134
|
+
open();
|
|
3135
|
+
resumed();
|
|
3136
|
+
}, Math.min(1e3 * 2 ** ended++, 3e4));
|
|
3137
|
+
},
|
|
3138
|
+
error: (err) => {
|
|
3139
|
+
if (!operationOpen) return;
|
|
3140
|
+
if (refusesDeviceTokens(err)) {
|
|
3141
|
+
this.multiDeviceUnsupported = true;
|
|
3142
|
+
this.fallbackGroup(group);
|
|
3143
|
+
return;
|
|
3144
|
+
}
|
|
3145
|
+
closeOperation();
|
|
3146
|
+
for (const [token, stream] of group.members) {
|
|
3147
|
+
if (this.streams.get(token) === stream) this.streams.delete(token);
|
|
3148
|
+
if (stream.subscribers.size) this.deadStreams.add(stream);
|
|
3149
|
+
for (const subscriber of stream.subscribers) subscriber.sink.error?.(err);
|
|
3150
|
+
}
|
|
3151
|
+
}
|
|
3152
|
+
};
|
|
3153
|
+
open();
|
|
3154
|
+
}
|
|
3017
3155
|
// attach registers a subscriber on a device's stream (opening the upstream on
|
|
3018
3156
|
// the first subscriber) and returns a detacher that drops it and closes the
|
|
3019
3157
|
// upstream once the last subscriber leaves.
|
|
@@ -3030,10 +3168,10 @@ var DashboardHub = class {
|
|
|
3030
3168
|
}
|
|
3031
3169
|
};
|
|
3032
3170
|
}
|
|
3033
|
-
ensureStream(deviceToken) {
|
|
3171
|
+
ensureStream(deviceToken, retained) {
|
|
3034
3172
|
const existing = this.streams.get(deviceToken);
|
|
3035
3173
|
if (existing) return existing;
|
|
3036
|
-
const stream = { subscribers: /* @__PURE__ */ new Set(), unsubscribe: () => {
|
|
3174
|
+
const stream = retained ?? { subscribers: /* @__PURE__ */ new Set(), unsubscribe: () => {
|
|
3037
3175
|
} };
|
|
3038
3176
|
this.streams.set(deviceToken, stream);
|
|
3039
3177
|
const handle = this.connection.register();
|