@devicechain/dashboards 1.5.0 → 1.6.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/README.md CHANGED
@@ -33,7 +33,7 @@ import { DashboardHub, resolveWidgetDatasource } from '@devicechain/dashboards';
33
33
 
34
34
  const hub = new DashboardHub({
35
35
  resolver, // how a selector becomes device tokens
36
- authorities: user.scopes, // what this viewer may do; drives hub.can(...)
36
+ authorities: user.authorities, // what this viewer may do; drives hub.can(...)
37
37
  });
38
38
 
39
39
  // The hub holds no slot bindings: resolve a widget's `slot` selector through them
@@ -54,13 +54,13 @@ Ten widgets pointed at the same device open **one** upstream subscription, not
54
54
  ten. Each widget declares the measurement names it cares about (none means all),
55
55
  and the hub fans a single stream out to the subscribers that asked for each name.
56
56
 
57
- Each channel has its own method, and each subscription returns its disposer:
57
+ Each subscription returns a disposer. Bucket reads return a promise:
58
58
 
59
59
  | method | what it carries |
60
60
  | --- | --- |
61
61
  | `subscribeWidget` | measurement samples for a datasource selector |
62
62
  | `subscribeMembers` | a fleet widget's members, then each member's samples tagged with its device |
63
- | `bucketMeasurements` | the time buckets (count, minimum, maximum, sum) a stat widget folds into one figure |
63
+ | `bucketMeasurements(datasource, window)` | a `Promise<MeasurementBucket[]>` of count, minimum, maximum and sum; not a subscription |
64
64
  | `subscribeAlarms` | live alarm rows, plus `acknowledgeAlarm` / `clearAlarm` |
65
65
  | `subscribeCommands` | command rows and their delivery status, plus `sendCommand` |
66
66
  | `subscribeLocations` | position series for map widgets |
@@ -74,6 +74,29 @@ the widgets that need them saying so instead of showing a number.
74
74
  `disposeAll()` tears down every open channel — a host calls it when the board
75
75
  closes, which is what keeps a poll and a socket from outliving the page.
76
76
 
77
+ `authorities` is an optional `string[]` (use `decodeToken(token)?.authorities` from
78
+ `@devicechain/client`). Missing authorities grant no actions; `'*'` grants all.
79
+ `can(authority)` checks that list. `subscribeWidget(datasource, sink)` receives
80
+ individual `MeasurementSample` values. Alarm and command subscriptions take
81
+ `{ datasource?, pageSize, ...filters }` and emit whole snapshots through `sink.next`;
82
+ location subscriptions take `{ datasource? }` and emit either `kind: 'positions'`
83
+ or `kind: 'forbidden'`. An absent alarm datasource means tenant-wide scope; an
84
+ `unbound` selector means no devices.
85
+
86
+ `sendCommand(deviceToken, commandKey, payload?)` returns a `Promise<CommandDispatch>`:
87
+ `{ status: 'sent', token }` or `{ status: 'rejected', code, reason }`.
88
+ The payload is JSON text. A server refusal resolves to `rejected`; a transport
89
+ failure rejects the promise. `acknowledgeAlarm(token)` and `clearAlarm(token)`
90
+ return `Promise<void>`.
91
+
92
+ `subscribePresence(datasource, sink)` supplies device connectivity. Custom
93
+ `WidgetDataSource` implementations may omit presence, member feeds, bucket reads,
94
+ seed reads and declared-metric lookups; widgets feature-detect these capabilities.
95
+ `declaredMetrics(deviceType)` and `declaredMetricsForDevice(deviceToken)` return
96
+ measurement names or null when unknown; null must not mean that a metric is absent.
97
+ Use `historyWindowFor(spanSeconds, endMs)` to construct the `HistoryWindow` used
98
+ by `bucketMeasurements` and `fetchWidgetHistory(widget, window, options?)`.
99
+
77
100
  ## The definition contract
78
101
 
79
102
  A dashboard is JSON. The server stores it opaquely; this package is what gives it
@@ -155,6 +178,23 @@ handed straight to the hub is an error.
155
178
  plausible measurements, so a board can be previewed, screenshotted or
156
179
  demonstrated with no instance behind it.
157
180
 
181
+ ## Recorded playback and editor state
182
+
183
+ `parseBoardRecording(input)` validates a recording or throws `RecordingFormatError`.
184
+ Use `createRecordedClock(recording)` and `new RecordedDataSource(recording, clock)`
185
+ to replay it through `WidgetDataSource`; `createRecordedResolver(recording)` and
186
+ `createRecordedLister(recording)` supply recorded entity lookup. Drive the clock
187
+ with `play()`, `pause()`, `seek(timeMs)` and `setRate(rate)` (`PLAYBACK_RATES` lists
188
+ supported rates). On teardown, call `source.disposeAll()` and `clock.pause()`.
189
+ A renderer using this source should set `seedHistory={false}` so it does not query
190
+ live history. Unsupported recorded channels report `NotInRecordingError`.
191
+
192
+ For an editor, `createEditHistory`, `commitEdit`, `undoEdit` and `redoEdit` manage
193
+ immutable definition history. `copyWidget` / `serializeWidgetClipboard` and
194
+ `parseWidgetClipboard` / `pasteWidgets` provide the portable clipboard format;
195
+ `duplicateWidget` copies within a board. Keep the installed widget catalog with
196
+ parsing and validation so extension widgets retain their identity.
197
+
158
198
  ## Command status
159
199
 
160
200
  `isTerminalCommandStatus` and `isCancellableCommandStatus` are the two questions
@@ -6,6 +6,7 @@ export declare function basePlacement(widget: WidgetInstance): Placement;
6
6
  export declare function resolveEverywhere(def: DashboardDefinition, first: readonly string[], aside?: boolean): DashboardDefinition;
7
7
  export interface EditOptions {
8
8
  nearest?: boolean;
9
+ breakpoint?: string;
9
10
  }
10
11
  export declare function setWidgetPlacement(def: DashboardDefinition, id: string, placement: Placement, options?: EditOptions): DashboardDefinition;
11
12
  export declare function clampSpan(span: {
@@ -73,8 +74,19 @@ export declare function setRelativeOffset(def: DashboardDefinition, id: string,
73
74
  fine?: boolean;
74
75
  bounds?: RelativeBounds;
75
76
  }): DashboardDefinition;
77
+ export declare function setRelativeOffset(def: DashboardDefinition, id: string, offset: {
78
+ x: number;
79
+ y: number;
80
+ }, options?: {
81
+ fine?: boolean;
82
+ bounds?: RelativeBounds;
83
+ breakpoint?: string;
84
+ }): DashboardDefinition;
76
85
  export declare function canHostRelative(def: DashboardDefinition, id: string, hostId: string): boolean;
77
86
  export declare function placeRelative(def: DashboardDefinition, id: string, hostId: string, anchor: PlacementAnchor): DashboardDefinition;
87
+ export declare function placeRelative(def: DashboardDefinition, id: string, hostId: string, anchor: PlacementAnchor, breakpoint?: string): DashboardDefinition;
78
88
  export declare function setRelativeAnchor(def: DashboardDefinition, id: string, anchor: PlacementAnchor): DashboardDefinition;
89
+ export declare function setRelativeAnchor(def: DashboardDefinition, id: string, anchor: PlacementAnchor, breakpoint?: string): DashboardDefinition;
79
90
  export declare function placeOnGrid(def: DashboardDefinition, id: string): DashboardDefinition;
91
+ export declare function placeOnGrid(def: DashboardDefinition, id: string, breakpoint?: string): DashboardDefinition;
80
92
  export declare function fitToLimits(def: DashboardDefinition, spanLimitsOf: (widget: WidgetInstance) => SpanLimits | undefined): DashboardDefinition;
package/dist/index.js CHANGED
@@ -702,12 +702,14 @@ function checkWidgetCompatibility(id, ref, descriptor) {
702
702
  if (ref.optionsVersion !== descriptor.optionsVersion) {
703
703
  return {
704
704
  code: "options-version",
705
+ versions: { authored: ref.optionsVersion, installed: descriptor.optionsVersion },
705
706
  message: `"${id}" was saved with settings version ${ref.optionsVersion}; the installed widget uses settings version ${descriptor.optionsVersion}`
706
707
  };
707
708
  }
708
709
  if (ref.sdkApiVersion !== void 0 && ref.sdkApiVersion !== descriptor.sdkApiVersion) {
709
710
  return {
710
711
  code: "sdk-incompatible",
712
+ versions: { authored: ref.sdkApiVersion, installed: descriptor.sdkApiVersion },
711
713
  message: `"${id}" was authored for widget SDK API ${ref.sdkApiVersion}; the installed widget uses API ${descriptor.sdkApiVersion}`
712
714
  };
713
715
  }
@@ -4811,31 +4813,37 @@ function resolveEverywhere(def, first, aside = false) {
4811
4813
  return next;
4812
4814
  }
4813
4815
  function setWidgetPlacement(def, id, placement, options = {}) {
4816
+ const breakpoint = options.breakpoint ?? BASE_BREAKPOINT;
4814
4817
  const next = {
4815
4818
  ...def,
4816
- widgets: def.widgets.map((w) => w.id === id ? { ...w, layout: { ...w.layout, [BASE_BREAKPOINT]: placement } } : w)
4819
+ widgets: def.widgets.map((w) => w.id === id ? { ...w, layout: { ...w.layout, [breakpoint]: placement } } : w)
4817
4820
  };
4818
- return placement.mode === "grid" ? resolveEverywhere(next, [id], options.nearest) : next;
4821
+ if (placement.mode !== "grid") return next;
4822
+ return breakpoint === BASE_BREAKPOINT ? resolveEverywhere(next, [id], options.nearest) : resolveCollisions(next, breakpoint, [id], options.nearest);
4819
4823
  }
4820
4824
  function clampSpan(span, limits, room) {
4821
4825
  const cols = Math.min(room, Math.max(limits?.minSpan?.cols ?? 1, Math.min(limits?.maxSpan?.cols ?? Infinity, span.cols)));
4822
4826
  const rows = Math.max(limits?.minSpan?.rows ?? 1, Math.min(limits?.maxSpan?.rows ?? Infinity, span.rows));
4823
4827
  return { cols: Math.max(1, Math.round(cols)), rows: Math.max(1, Math.round(rows)) };
4824
4828
  }
4825
- function moveWidget(def, id, to, columns = def.canvas.grid.columns, options = {}) {
4829
+ function moveWidget(def, id, to, columns, options = {}) {
4826
4830
  const w = def.widgets.find((x) => x.id === id);
4827
4831
  if (!w) return def;
4828
- const p = basePlacement(w);
4832
+ const breakpoint = options.breakpoint ?? BASE_BREAKPOINT;
4833
+ columns ?? (columns = breakpointColumns(def.canvas, breakpoint));
4834
+ const p = resolvePlacement(w.layout, breakpoint);
4829
4835
  if (p.mode !== "grid") return def;
4830
4836
  const col = Math.min(Math.max(0, Math.round(to.col)), Math.max(0, columns - p.cols));
4831
4837
  const row = Math.max(0, Math.round(to.row));
4832
4838
  if (col === p.col && row === p.row) return def;
4833
4839
  return setWidgetPlacement(def, id, { ...p, col, row }, options);
4834
4840
  }
4835
- function resizeWidget(def, id, target, limits, edges = {}, columns = def.canvas.grid.columns, options = {}) {
4841
+ function resizeWidget(def, id, target, limits, edges = {}, columns, options = {}) {
4836
4842
  const w = def.widgets.find((x) => x.id === id);
4837
4843
  if (!w) return def;
4838
- const p = basePlacement(w);
4844
+ const breakpoint = options.breakpoint ?? BASE_BREAKPOINT;
4845
+ columns ?? (columns = breakpointColumns(def.canvas, breakpoint));
4846
+ const p = resolvePlacement(w.layout, breakpoint);
4839
4847
  if (p.mode !== "grid") return def;
4840
4848
  const right = target.col + target.cols;
4841
4849
  const bottom = target.row + target.rows;
@@ -4955,7 +4963,7 @@ function pxToGridPlacement(px, geom, columns) {
4955
4963
  };
4956
4964
  }
4957
4965
  function formatSpan(span) {
4958
- return `${span.cols}\xD7${span.rows}`;
4966
+ return `${span.cols}×${span.rows}`;
4959
4967
  }
4960
4968
  var OFFSET_STEP = 1;
4961
4969
  var OFFSET_STEP_FINE = 0.25;
@@ -4964,23 +4972,24 @@ function snapOffset(value, fine = false) {
4964
4972
  const snapped = Math.round(value / step) * step;
4965
4973
  return snapped === 0 ? 0 : snapped;
4966
4974
  }
4967
- function replaceBase(def, id, placement) {
4975
+ function replacePlacement(def, id, placement, breakpoint) {
4968
4976
  return {
4969
4977
  ...def,
4970
- widgets: def.widgets.map((w) => w.id === id ? { ...w, layout: { ...w.layout, [BASE_BREAKPOINT]: placement } } : w)
4978
+ widgets: def.widgets.map((w) => w.id === id ? { ...w, layout: { ...w.layout, [breakpoint]: placement } } : w)
4971
4979
  };
4972
4980
  }
4973
4981
  function setRelativeOffset(def, id, offset, options = {}) {
4974
4982
  const w = def.widgets.find((x2) => x2.id === id);
4975
4983
  if (!w) return def;
4976
- const p = basePlacement(w);
4984
+ const breakpoint = options.breakpoint ?? BASE_BREAKPOINT;
4985
+ const p = resolvePlacement(w.layout, breakpoint);
4977
4986
  if (p.mode !== "relative") return def;
4978
4987
  const step = options.fine ? OFFSET_STEP_FINE : OFFSET_STEP;
4979
4988
  let x = snapOffset(offset.x, options.fine);
4980
4989
  let y = snapOffset(offset.y, options.fine);
4981
4990
  const { bounds } = options;
4982
4991
  if (bounds) {
4983
- const host = layoutRects(def.widgets, BASE_BREAKPOINT, bounds.cell, bounds.gutter).get(p.host);
4992
+ const host = layoutRects(def.widgets, breakpoint, bounds.cell, bounds.gutter).get(p.host);
4984
4993
  if (host) {
4985
4994
  const r = relativeRect({ ...p, offset: { x, y } }, host, bounds.cell, bounds.gutter);
4986
4995
  const px = (bounds.cell + bounds.gutter.x) * step;
@@ -4995,7 +5004,7 @@ function setRelativeOffset(def, id, offset, options = {}) {
4995
5004
  if (x === 0 && y === 0) delete next.offset;
4996
5005
  else next.offset = { x, y };
4997
5006
  if ((next.offset?.x ?? 0) === (p.offset?.x ?? 0) && (next.offset?.y ?? 0) === (p.offset?.y ?? 0)) return def;
4998
- return replaceBase(def, id, next);
5007
+ return replacePlacement(def, id, next, breakpoint);
4999
5008
  }
5000
5009
  function canHost(def, host, id) {
5001
5010
  if (!host || host.id === id) return false;
@@ -5006,32 +5015,32 @@ function canHost(def, host, id) {
5006
5015
  function canHostRelative(def, id, hostId) {
5007
5016
  return canHost(def, def.widgets.find((x) => x.id === hostId), id);
5008
5017
  }
5009
- function placeRelative(def, id, hostId, anchor) {
5018
+ function placeRelative(def, id, hostId, anchor, breakpoint = BASE_BREAKPOINT) {
5010
5019
  const w = def.widgets.find((x) => x.id === id);
5011
5020
  if (!w) return def;
5012
5021
  if (!canHost(def, def.widgets.find((x) => x.id === hostId), id)) return def;
5013
- const p = basePlacement(w);
5022
+ const p = resolvePlacement(w.layout, breakpoint);
5014
5023
  const size = p.mode === "grid" ? { cols: p.cols, rows: p.rows } : p.size;
5015
- return replaceBase(def, id, { mode: "relative", host: hostId, anchor, size });
5024
+ return replacePlacement(def, id, { mode: "relative", host: hostId, anchor, size }, breakpoint);
5016
5025
  }
5017
- function setRelativeAnchor(def, id, anchor) {
5026
+ function setRelativeAnchor(def, id, anchor, breakpoint = BASE_BREAKPOINT) {
5018
5027
  const w = def.widgets.find((x) => x.id === id);
5019
- const p = w && basePlacement(w);
5028
+ const p = w && resolvePlacement(w.layout, breakpoint);
5020
5029
  if (!p || p.mode !== "relative" || p.anchor === anchor) return def;
5021
- return replaceBase(def, id, { ...p, anchor });
5030
+ return replacePlacement(def, id, { ...p, anchor }, breakpoint);
5022
5031
  }
5023
- function placeOnGrid(def, id) {
5032
+ function placeOnGrid(def, id, breakpoint = BASE_BREAKPOINT) {
5024
5033
  const w = def.widgets.find((x) => x.id === id);
5025
5034
  if (!w) return def;
5026
- const p = basePlacement(w);
5035
+ const p = resolvePlacement(w.layout, breakpoint);
5027
5036
  if (p.mode !== "relative") return def;
5028
- const columns = def.canvas.grid.columns;
5029
- const size = "fill" in p.size ? hostSpan(p, def, BASE_BREAKPOINT) : p.size;
5037
+ const columns = breakpointColumns(def.canvas, breakpoint);
5038
+ const size = "fill" in p.size ? hostSpan(p, def, breakpoint) : p.size;
5030
5039
  const cols = Math.min(size.cols, columns);
5031
- const occupied = def.widgets.filter((o) => o.id !== id).map(basePlacement).filter((o) => o.mode === "grid");
5040
+ const occupied = def.widgets.filter((o) => o.id !== id).map((o) => resolvePlacement(o.layout, breakpoint)).filter((o) => o.mode === "grid");
5032
5041
  const slot = firstFreeSlot(occupied, cols, size.rows, columns);
5033
5042
  const grid = { mode: "grid", col: slot.col, row: slot.row, cols, rows: size.rows };
5034
- return resolveEverywhere(replaceBase(def, id, grid), [id]);
5043
+ return setWidgetPlacement(def, id, grid, { breakpoint });
5035
5044
  }
5036
5045
  function fitToLimits(def, spanLimitsOf) {
5037
5046
  let changed = false;
@@ -5476,12 +5485,17 @@ function createDeviceResolver() {
5476
5485
  batch.set(deviceType, waiting);
5477
5486
  });
5478
5487
  },
5479
- // The device type's token, from the same names read the roster uses. A failed read rejects (the
5480
- // caller treats that as unknown) and is not cached.
5488
+ // The device type's token, from the same names read the roster uses. Cache only a positive
5489
+ // result so a missing device or unassigned type can recover on a later lookup. Concurrent
5490
+ // reads share the in-flight promise; a failed read rejects and is not cached.
5481
5491
  deviceType(deviceToken) {
5482
5492
  let pending = typeCache.get(deviceToken);
5483
5493
  if (!pending) {
5484
- pending = gql3("device-management", DEVICE_NAMES, { tokens: [deviceToken] }).then((r) => r.devicesByToken.find((d) => d.token === deviceToken)?.deviceType?.token ?? null).catch((err) => {
5494
+ pending = gql3("device-management", DEVICE_NAMES, { tokens: [deviceToken] }).then((r) => {
5495
+ const deviceType = r.devicesByToken.find((d) => d.token === deviceToken)?.deviceType?.token ?? null;
5496
+ if (deviceType === null) typeCache.delete(deviceToken);
5497
+ return deviceType;
5498
+ }).catch((err) => {
5485
5499
  typeCache.delete(deviceToken);
5486
5500
  throw err;
5487
5501
  });