@device-portal/react 0.0.23 → 0.1.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
@@ -168,6 +168,30 @@ const ConsumerComponent = () => {
168
168
 
169
169
  The WebRTC connection is designed to be resilient. If the connection to the signaling server is temporarily lost, any established peer-to-peer connections will remain active. The client will attempt to reconnect to the signaling server in the background to handle any future connection negotiations.
170
170
 
171
+ ### Connection Status
172
+
173
+ `useDevicePortalConsumer` exposes a `connectionStatus` field so the UI can react when the peer link drops:
174
+
175
+ ```jsx
176
+ const ConsumerComponent = () => {
177
+ const { value, connectionStatus } = useDevicePortalConsumer('my-test-room')
178
+
179
+ return (
180
+ <>
181
+ <p>Value from provider: {value}</p>
182
+ {connectionStatus === 'reconnecting' && <p>Reconnecting to provider…</p>}
183
+ </>
184
+ )
185
+ }
186
+ ```
187
+
188
+ The status is one of:
189
+
190
+ - `'connected'` — the peer link is up.
191
+ - `'reconnecting'` — the peer link dropped (peer left, ICE failed); the consumer is retrying in the background. The last received `value` is kept.
192
+
193
+ `'connecting'` is intentionally not exposed: the hook suspends until the first value arrives, so by the time your component renders the consumer was already connected at least once.
194
+
171
195
  ### Browser Direct Communication
172
196
 
173
197
  When peers are in the same browser (different tabs or same tab), the library automatically uses direct browser APIs for communication instead of WebRTC. This results in a near-instant connection and works offline.
@@ -10,6 +10,16 @@ export type DevicePortalConsumerOptions = {
10
10
  /** Whether to automatically send the last 'send()' value back to the provider on reconnect. Default: false. */
11
11
  sendLastMessageOnReconnect?: boolean;
12
12
  };
13
+ /**
14
+ * Connection state reported by {@link useDevicePortalConsumer}.
15
+ *
16
+ * The hook suspends until the first value arrives, so `'connecting'` is never
17
+ * observed — by the time the component renders, the consumer was at least once
18
+ * connected. After that, the status flips to `'reconnecting'` whenever the
19
+ * peer link drops (peer left, ICE failure) and back to `'connected'` once the
20
+ * link is re-established.
21
+ */
22
+ type ConnectionStatus = 'connected' | 'reconnecting';
13
23
  /**
14
24
  * A React hook that joins a Device Portal room and receives values from the
15
25
  * provider. **Suspends** the component until the first value is received —
@@ -23,8 +33,18 @@ export type DevicePortalConsumerOptions = {
23
33
  *
24
34
  * @param room - The unique room ID.
25
35
  * @param options - Consumer configuration options.
36
+ * @returns An object with:
37
+ * - `value`: the latest value pushed by the provider. After Suspense
38
+ * resolves, this never goes "stale" to `undefined` — if the peer link
39
+ * drops, the last received value is retained.
40
+ * - `connectionStatus`: `'connected'` or `'reconnecting'`. Use this to
41
+ * render a banner / indicator when the peer link is temporarily down.
42
+ * The hook keeps trying to reconnect in the background.
43
+ * - `sendMessageToProvider`: send a message back to the provider.
26
44
  */
27
45
  export declare const useDevicePortalConsumer: (room: string, options?: DevicePortalConsumerOptions) => {
28
46
  value: string;
47
+ connectionStatus: ConnectionStatus;
29
48
  sendMessageToProvider: (message: string) => void;
30
49
  };
50
+ export {};
@@ -37,7 +37,9 @@ function getOrCreateEntry(key, room, peerId, options) {
37
37
  firstValuePromise: firstValuePromise,
38
38
  latestValue: undefined,
39
39
  lastSentValue: undefined,
40
+ connectionStatus: 'reconnecting',
40
41
  setValue: null,
42
+ setConnectionStatus: null,
41
43
  destroyTimer: null,
42
44
  room: room,
43
45
  optionsSnapshot: newOptionsKey
@@ -50,10 +52,18 @@ function getOrCreateEntry(key, room, peerId, options) {
50
52
  (_entry$setValue = entry.setValue) === null || _entry$setValue === void 0 || _entry$setValue.call(entry, value);
51
53
  },
52
54
  onConnected: function onConnected() {
55
+ var _entry$setConnectionS;
56
+ entry.connectionStatus = 'connected';
57
+ (_entry$setConnectionS = entry.setConnectionStatus) === null || _entry$setConnectionS === void 0 || _entry$setConnectionS.call(entry, 'connected');
53
58
  if (sendLastMessageOnReconnect && entry.lastSentValue !== undefined) {
54
59
  entry.consumer.send(entry.lastSentValue);
55
60
  }
56
61
  },
62
+ onDisconnected: function onDisconnected() {
63
+ var _entry$setConnectionS2;
64
+ entry.connectionStatus = 'reconnecting';
65
+ (_entry$setConnectionS2 = entry.setConnectionStatus) === null || _entry$setConnectionS2 === void 0 || _entry$setConnectionS2.call(entry, 'reconnecting');
66
+ },
57
67
  webSocketSignalingServer: options.webSocketSignalingServer,
58
68
  browserDirect: options.browserDirect,
59
69
  peerId: peerId
@@ -85,6 +95,14 @@ function scheduleDestroy(key) {
85
95
  *
86
96
  * @param room - The unique room ID.
87
97
  * @param options - Consumer configuration options.
98
+ * @returns An object with:
99
+ * - `value`: the latest value pushed by the provider. After Suspense
100
+ * resolves, this never goes "stale" to `undefined` — if the peer link
101
+ * drops, the last received value is retained.
102
+ * - `connectionStatus`: `'connected'` or `'reconnecting'`. Use this to
103
+ * render a banner / indicator when the peer link is temporarily down.
104
+ * The hook keeps trying to reconnect in the background.
105
+ * - `sendMessageToProvider`: send a message back to the provider.
88
106
  */
89
107
  var useDevicePortalConsumer = function useDevicePortalConsumer(room) {
90
108
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -104,17 +122,27 @@ var useDevicePortalConsumer = function useDevicePortalConsumer(room) {
104
122
  _useState2 = _slicedToArray(_useState, 2),
105
123
  value = _useState2[0],
106
124
  setValue = _useState2[1];
107
- // Wire up the entry's setValue so the Consumer can push updates.
125
+ var _useState3 = useState(function () {
126
+ return entry.connectionStatus;
127
+ }),
128
+ _useState4 = _slicedToArray(_useState3, 2),
129
+ connectionStatus = _useState4[0],
130
+ setConnectionStatus = _useState4[1];
131
+ // Wire up the entry's setters so the Consumer can push updates.
108
132
  entry.setValue = setValue;
133
+ entry.setConnectionStatus = setConnectionStatus;
109
134
  useEffect(function () {
110
135
  var e = getOrCreateEntry(cacheKey, room, peerIdRef.current, options);
111
136
  e.setValue = setValue;
112
- // Sync in case a value arrived between render and effect commit.
137
+ e.setConnectionStatus = setConnectionStatus;
138
+ // Sync in case state changed between render and effect commit.
113
139
  if (e.latestValue !== undefined) {
114
140
  setValue(e.latestValue);
115
141
  }
142
+ setConnectionStatus(e.connectionStatus);
116
143
  return function () {
117
144
  e.setValue = null;
145
+ e.setConnectionStatus = null;
118
146
  scheduleDestroy(cacheKey);
119
147
  };
120
148
  }, [cacheKey, room, options.webSocketSignalingServer, options.browserDirect, options.sendLastMessageOnReconnect]);
@@ -124,6 +152,7 @@ var useDevicePortalConsumer = function useDevicePortalConsumer(room) {
124
152
  }, [entry]);
125
153
  return {
126
154
  value: value,
155
+ connectionStatus: connectionStatus,
127
156
  sendMessageToProvider: sendMessageToProvider
128
157
  };
129
158
  };
@@ -1 +1 @@
1
- {"version":3,"file":"useDevicePortalConsumer.js","sources":["../../src/consumer/useDevicePortalConsumer.ts"],"sourcesContent":["import {\n\tClient,\n\tgeneratePeerId,\n\ttype BrowserDirectOption,\n\ttype PeerId,\n} from '@device-portal/client'\nimport { use, useCallback, useEffect, useRef, useState } from 'react'\n\n/**\n * Configuration options for the Device Portal Consumer.\n */\nexport type DevicePortalConsumerOptions = {\n\t/** URL of the signaling server, or null to disable. */\n\twebSocketSignalingServer?: string | null\n\t/** Browser direct signaling options. */\n\tbrowserDirect?: BrowserDirectOption\n\t/** Whether to automatically send the last 'send()' value back to the provider on reconnect. Default: false. */\n\tsendLastMessageOnReconnect?: boolean\n}\n\n// ---------------------------------------------------------------------------\n// Module-level cache for Consumer entries.\n//\n// Why module-level? The cache must survive React Suspense throws (which\n// discard in-progress render state) and React Strict Mode's synchronous\n// unmount/remount cycle. The entry is keyed by room + options, which is\n// stable across both scenarios.\n//\n// Cleanup uses a grace-period timeout so that Strict Mode's immediate\n// remount can reclaim the entry before it's destroyed.\n// ---------------------------------------------------------------------------\n\ntype ConsumerEntry = {\n\tconsumer: Client\n\tfirstValuePromise: Promise<string>\n\tlatestValue: string | undefined\n\tlastSentValue: string | undefined\n\tsetValue: ((value: string) => void) | null\n\tdestroyTimer: ReturnType<typeof setTimeout> | null\n\troom: string\n\toptionsSnapshot: string\n}\n\nconst entries = new Map<string, ConsumerEntry>()\n\nfunction optionsKey(options: DevicePortalConsumerOptions): string {\n\treturn `${options.webSocketSignalingServer ?? ''}\\0${options.browserDirect ?? ''}\\0${options.sendLastMessageOnReconnect ?? ''}`\n}\n\nfunction getOrCreateEntry(\n\tkey: string,\n\troom: string,\n\tpeerId: PeerId,\n\toptions: DevicePortalConsumerOptions,\n): ConsumerEntry {\n\tconst existing = entries.get(key)\n\tconst newOptionsKey = optionsKey(options)\n\n\tif (existing) {\n\t\t// Cancel any pending destruction\n\t\tif (existing.destroyTimer !== null) {\n\t\t\tclearTimeout(existing.destroyTimer)\n\t\t\texisting.destroyTimer = null\n\t\t}\n\t\t// Reuse if room + options unchanged\n\t\tif (existing.room === room && existing.optionsSnapshot === newOptionsKey) {\n\t\t\treturn existing\n\t\t}\n\t\t// Room/options changed – destroy old entry synchronously\n\t\texisting.consumer.destroy()\n\t\tentries.delete(key)\n\t}\n\n\t// Create a deferred promise for Suspense via use()\n\tlet resolveFirst!: (value: string) => void\n\tconst firstValuePromise = new Promise<string>((resolve) => {\n\t\tresolveFirst = resolve\n\t})\n\n\tconst sendLastMessageOnReconnect = options.sendLastMessageOnReconnect ?? false\n\n\tconst entry: ConsumerEntry = {\n\t\tconsumer: undefined!, // assigned below\n\t\tfirstValuePromise,\n\t\tlatestValue: undefined,\n\t\tlastSentValue: undefined,\n\t\tsetValue: null,\n\t\tdestroyTimer: null,\n\t\troom,\n\t\toptionsSnapshot: newOptionsKey,\n\t}\n\n\tentry.consumer = new Client(room, {\n\t\tonMessage: (value) => {\n\t\t\tentry.latestValue = value\n\t\t\tresolveFirst(value)\n\t\t\tentry.setValue?.(value)\n\t\t},\n\t\tonConnected: () => {\n\t\t\tif (sendLastMessageOnReconnect && entry.lastSentValue !== undefined) {\n\t\t\t\tentry.consumer.send(entry.lastSentValue)\n\t\t\t}\n\t\t},\n\t\twebSocketSignalingServer: options.webSocketSignalingServer,\n\t\tbrowserDirect: options.browserDirect,\n\t\tpeerId,\n\t})\n\n\tentries.set(key, entry)\n\treturn entry\n}\n\nfunction scheduleDestroy(key: string) {\n\tconst entry = entries.get(key)\n\tif (!entry || entry.destroyTimer !== null) return\n\n\t// setTimeout(0) so React Strict Mode's synchronous unmount/remount\n\t// reclaims the entry before it's destroyed, while real unmounts\n\t// (navigation, conditional render) clean up on the next microtask.\n\tentry.destroyTimer = setTimeout(() => {\n\t\tentry.consumer.destroy()\n\t\tentries.delete(key)\n\t}, 0)\n}\n\n/**\n * A React hook that joins a Device Portal room and receives values from the\n * provider. **Suspends** the component until the first value is received —\n * wrap the consumer in a `<Suspense>` boundary.\n *\n * Requires React 19+. Uses `use()` for Suspense integration.\n *\n * Safe under React Strict Mode and concurrent features: the underlying\n * Consumer is cached with a grace-period cleanup so synchronous\n * unmount/remount cycles do not create duplicate connections.\n *\n * @param room - The unique room ID.\n * @param options - Consumer configuration options.\n */\nexport const useDevicePortalConsumer = (\n\troom: string,\n\toptions: DevicePortalConsumerOptions = {},\n): {\n\tvalue: string\n\tsendMessageToProvider: (message: string) => void\n} => {\n\t// Key by room + options rather than useId(), because useId() is not\n\t// stable across Suspense re-throws when sibling state updates cause\n\t// the component tree to re-mount.\n\tconst cacheKey = `${room}\\0${optionsKey(options)}`\n\tconst peerIdRef = useRef<PeerId>(generatePeerId())\n\n\tconst entry = getOrCreateEntry(cacheKey, room, peerIdRef.current, options)\n\n\t// Suspends until the first value arrives (React 19 use() API).\n\tconst firstValue = use(entry.firstValuePromise)\n\n\t// After the first value, track subsequent updates via useState.\n\tconst [value, setValue] = useState(() => entry.latestValue ?? firstValue)\n\n\t// Wire up the entry's setValue so the Consumer can push updates.\n\tentry.setValue = setValue\n\n\tuseEffect(() => {\n\t\tconst e = getOrCreateEntry(cacheKey, room, peerIdRef.current, options)\n\t\te.setValue = setValue\n\n\t\t// Sync in case a value arrived between render and effect commit.\n\t\tif (e.latestValue !== undefined) {\n\t\t\tsetValue(e.latestValue)\n\t\t}\n\n\t\treturn () => {\n\t\t\te.setValue = null\n\t\t\tscheduleDestroy(cacheKey)\n\t\t}\n\t}, [\n\t\tcacheKey,\n\t\troom,\n\t\toptions.webSocketSignalingServer,\n\t\toptions.browserDirect,\n\t\toptions.sendLastMessageOnReconnect,\n\t])\n\n\tconst sendMessageToProvider = useCallback(\n\t\t(message: string) => {\n\t\t\tentry.lastSentValue = message\n\t\t\tentry.consumer.send(message)\n\t\t},\n\t\t[entry],\n\t)\n\n\treturn { value, sendMessageToProvider }\n}\n"],"names":["entries","Map","optionsKey","options","_options$webSocketSig","_options$browserDirec","_options$sendLastMess","concat","webSocketSignalingServer","browserDirect","sendLastMessageOnReconnect","getOrCreateEntry","key","room","peerId","_options$sendLastMess2","existing","get","newOptionsKey","destroyTimer","clearTimeout","optionsSnapshot","consumer","destroy","resolveFirst","firstValuePromise","Promise","resolve","entry","undefined","latestValue","lastSentValue","setValue","Client","onMessage","value","_entry$setValue","call","onConnected","send","set","scheduleDestroy","setTimeout","useDevicePortalConsumer","arguments","length","cacheKey","peerIdRef","useRef","generatePeerId","current","firstValue","use","_useState","useState","_entry$latestValue","_useState2","_slicedToArray","useEffect","e","sendMessageToProvider","useCallback","message"],"mappings":";;;;AA2CA,IAAMA,OAAO,GAAG,IAAIC,GAAG,EAAyB;AAEhD,SAASC,UAAUA,CAACC,OAAoC,EAAA;AAAA,EAAA,IAAAC,qBAAA,EAAAC,qBAAA,EAAAC,qBAAA;AACvD,EAAA,OAAA,EAAA,CAAAC,MAAA,CAAAH,CAAAA,qBAAA,GAAUD,OAAO,CAACK,wBAAwB,MAAAJ,IAAAA,IAAAA,qBAAA,KAAAA,MAAAA,GAAAA,qBAAA,GAAI,EAAE,EAAA,IAAA,CAAA,CAAAG,MAAA,CAAAF,CAAAA,qBAAA,GAAKF,OAAO,CAACM,aAAa,MAAA,IAAA,IAAAJ,qBAAA,KAAAA,MAAAA,GAAAA,qBAAA,GAAI,EAAE,EAAA,IAAA,CAAA,CAAAE,MAAA,CAAAD,CAAAA,qBAAA,GAAKH,OAAO,CAACO,0BAA0B,MAAA,IAAA,IAAAJ,qBAAA,KAAAA,MAAAA,GA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AAEkC,SAAS,CAACG,OAAO,EAAE/C,OAAO,CAAC;IACtEwD,CAAC,CAAC3B,QAAQ,GAAGA,QAAQ;AAErB;AACA,IAAA,IAAI2B,CAAC,CAAC7B,WAAW,KAAKD,SAAS,EAAE;AAChCG,MAAAA,QAAQ,CAAC2B,CAAC,CAAC7B,WAAW,CAAC;AACxB;AAEA,IAAA,OAAO,YAAK;MACX6B,CAAC,CAAC3B,QAAQ,GAAG,IAAI;MACjBS,eAAe,CAACK,QAAQ,CAAC;KACzB;AACF,GAAC,EAAE,CACFA,QAAQ,EACRjC,IAAI,EACJV,OAAO,CAACK,wBAAwB,EAChCL,OAAO,CAACM,aAAa,EACrBN,OAAO,CAACO,0BAA0B,CAClC,CAAC;AAEF,EAAA,IAAMkD,qBAAqB,GAAGC,WAAW,CACxC,UAACC,OAAe,EAAI;IACnBlC,KAAK,CAACG,aAAa,GAAG+B,OAAO;AAC7BlC,IAAAA,KAAK,CAACN,QAAQ,CAACiB,IAAI,CAACuB,OAAO,CAAC;AAC7B,GAAC,EACD,CAAClC,KAAK,CAAC,CACP;EAED,OAAO;AAAEO,IAAAA,KAAK,EAALA,KAAK;AAAEyB,IAAAA,qBAAqB,EAArBA;GAAuB;AACxC;;;;"}
1
+ {"version":3,"file":"useDevicePortalConsumer.js","sources":["../../src/consumer/useDevicePortalConsumer.ts"],"sourcesContent":["import {\n\tClient,\n\tgeneratePeerId,\n\ttype BrowserDirectOption,\n\ttype PeerId,\n} from '@device-portal/client'\nimport { use, useCallback, useEffect, useRef, useState } from 'react'\n\n/**\n * Configuration options for the Device Portal Consumer.\n */\nexport type DevicePortalConsumerOptions = {\n\t/** URL of the signaling server, or null to disable. */\n\twebSocketSignalingServer?: string | null\n\t/** Browser direct signaling options. */\n\tbrowserDirect?: BrowserDirectOption\n\t/** Whether to automatically send the last 'send()' value back to the provider on reconnect. Default: false. */\n\tsendLastMessageOnReconnect?: boolean\n}\n\n// ---------------------------------------------------------------------------\n// Module-level cache for Consumer entries.\n//\n// Why module-level? The cache must survive React Suspense throws (which\n// discard in-progress render state) and React Strict Mode's synchronous\n// unmount/remount cycle. The entry is keyed by room + options, which is\n// stable across both scenarios.\n//\n// Cleanup uses a grace-period timeout so that Strict Mode's immediate\n// remount can reclaim the entry before it's destroyed.\n// ---------------------------------------------------------------------------\n\n/**\n * Connection state reported by {@link useDevicePortalConsumer}.\n *\n * The hook suspends until the first value arrives, so `'connecting'` is never\n * observed — by the time the component renders, the consumer was at least once\n * connected. After that, the status flips to `'reconnecting'` whenever the\n * peer link drops (peer left, ICE failure) and back to `'connected'` once the\n * link is re-established.\n */\ntype ConnectionStatus = 'connected' | 'reconnecting'\n\ntype ConsumerEntry = {\n\tconsumer: Client\n\tfirstValuePromise: Promise<string>\n\tlatestValue: string | undefined\n\tlastSentValue: string | undefined\n\tconnectionStatus: ConnectionStatus\n\tsetValue: ((value: string) => void) | null\n\tsetConnectionStatus: ((status: ConnectionStatus) => void) | null\n\tdestroyTimer: ReturnType<typeof setTimeout> | null\n\troom: string\n\toptionsSnapshot: string\n}\n\nconst entries = new Map<string, ConsumerEntry>()\n\nfunction optionsKey(options: DevicePortalConsumerOptions): string {\n\treturn `${options.webSocketSignalingServer ?? ''}\\0${options.browserDirect ?? ''}\\0${options.sendLastMessageOnReconnect ?? ''}`\n}\n\nfunction getOrCreateEntry(\n\tkey: string,\n\troom: string,\n\tpeerId: PeerId,\n\toptions: DevicePortalConsumerOptions,\n): ConsumerEntry {\n\tconst existing = entries.get(key)\n\tconst newOptionsKey = optionsKey(options)\n\n\tif (existing) {\n\t\t// Cancel any pending destruction\n\t\tif (existing.destroyTimer !== null) {\n\t\t\tclearTimeout(existing.destroyTimer)\n\t\t\texisting.destroyTimer = null\n\t\t}\n\t\t// Reuse if room + options unchanged\n\t\tif (existing.room === room && existing.optionsSnapshot === newOptionsKey) {\n\t\t\treturn existing\n\t\t}\n\t\t// Room/options changed – destroy old entry synchronously\n\t\texisting.consumer.destroy()\n\t\tentries.delete(key)\n\t}\n\n\t// Create a deferred promise for Suspense via use()\n\tlet resolveFirst!: (value: string) => void\n\tconst firstValuePromise = new Promise<string>((resolve) => {\n\t\tresolveFirst = resolve\n\t})\n\n\tconst sendLastMessageOnReconnect = options.sendLastMessageOnReconnect ?? false\n\n\tconst entry: ConsumerEntry = {\n\t\tconsumer: undefined!, // assigned below\n\t\tfirstValuePromise,\n\t\tlatestValue: undefined,\n\t\tlastSentValue: undefined,\n\t\tconnectionStatus: 'reconnecting',\n\t\tsetValue: null,\n\t\tsetConnectionStatus: null,\n\t\tdestroyTimer: null,\n\t\troom,\n\t\toptionsSnapshot: newOptionsKey,\n\t}\n\n\tentry.consumer = new Client(room, {\n\t\tonMessage: (value) => {\n\t\t\tentry.latestValue = value\n\t\t\tresolveFirst(value)\n\t\t\tentry.setValue?.(value)\n\t\t},\n\t\tonConnected: () => {\n\t\t\tentry.connectionStatus = 'connected'\n\t\t\tentry.setConnectionStatus?.('connected')\n\t\t\tif (sendLastMessageOnReconnect && entry.lastSentValue !== undefined) {\n\t\t\t\tentry.consumer.send(entry.lastSentValue)\n\t\t\t}\n\t\t},\n\t\tonDisconnected: () => {\n\t\t\tentry.connectionStatus = 'reconnecting'\n\t\t\tentry.setConnectionStatus?.('reconnecting')\n\t\t},\n\t\twebSocketSignalingServer: options.webSocketSignalingServer,\n\t\tbrowserDirect: options.browserDirect,\n\t\tpeerId,\n\t})\n\n\tentries.set(key, entry)\n\treturn entry\n}\n\nfunction scheduleDestroy(key: string) {\n\tconst entry = entries.get(key)\n\tif (!entry || entry.destroyTimer !== null) return\n\n\t// setTimeout(0) so React Strict Mode's synchronous unmount/remount\n\t// reclaims the entry before it's destroyed, while real unmounts\n\t// (navigation, conditional render) clean up on the next microtask.\n\tentry.destroyTimer = setTimeout(() => {\n\t\tentry.consumer.destroy()\n\t\tentries.delete(key)\n\t}, 0)\n}\n\n/**\n * A React hook that joins a Device Portal room and receives values from the\n * provider. **Suspends** the component until the first value is received —\n * wrap the consumer in a `<Suspense>` boundary.\n *\n * Requires React 19+. Uses `use()` for Suspense integration.\n *\n * Safe under React Strict Mode and concurrent features: the underlying\n * Consumer is cached with a grace-period cleanup so synchronous\n * unmount/remount cycles do not create duplicate connections.\n *\n * @param room - The unique room ID.\n * @param options - Consumer configuration options.\n * @returns An object with:\n * - `value`: the latest value pushed by the provider. After Suspense\n * resolves, this never goes \"stale\" to `undefined` — if the peer link\n * drops, the last received value is retained.\n * - `connectionStatus`: `'connected'` or `'reconnecting'`. Use this to\n * render a banner / indicator when the peer link is temporarily down.\n * The hook keeps trying to reconnect in the background.\n * - `sendMessageToProvider`: send a message back to the provider.\n */\nexport const useDevicePortalConsumer = (\n\troom: string,\n\toptions: DevicePortalConsumerOptions = {},\n): {\n\tvalue: string\n\tconnectionStatus: ConnectionStatus\n\tsendMessageToProvider: (message: string) => void\n} => {\n\t// Key by room + options rather than useId(), because useId() is not\n\t// stable across Suspense re-throws when sibling state updates cause\n\t// the component tree to re-mount.\n\tconst cacheKey = `${room}\\0${optionsKey(options)}`\n\tconst peerIdRef = useRef<PeerId>(generatePeerId())\n\n\tconst entry = getOrCreateEntry(cacheKey, room, peerIdRef.current, options)\n\n\t// Suspends until the first value arrives (React 19 use() API).\n\tconst firstValue = use(entry.firstValuePromise)\n\n\t// After the first value, track subsequent updates via useState.\n\tconst [value, setValue] = useState(() => entry.latestValue ?? firstValue)\n\tconst [connectionStatus, setConnectionStatus] = useState<ConnectionStatus>(\n\t\t() => entry.connectionStatus,\n\t)\n\n\t// Wire up the entry's setters so the Consumer can push updates.\n\tentry.setValue = setValue\n\tentry.setConnectionStatus = setConnectionStatus\n\n\tuseEffect(() => {\n\t\tconst e = getOrCreateEntry(cacheKey, room, peerIdRef.current, options)\n\t\te.setValue = setValue\n\t\te.setConnectionStatus = setConnectionStatus\n\n\t\t// Sync in case state changed between render and effect commit.\n\t\tif (e.latestValue !== undefined) {\n\t\t\tsetValue(e.latestValue)\n\t\t}\n\t\tsetConnectionStatus(e.connectionStatus)\n\n\t\treturn () => {\n\t\t\te.setValue = null\n\t\t\te.setConnectionStatus = null\n\t\t\tscheduleDestroy(cacheKey)\n\t\t}\n\t}, [\n\t\tcacheKey,\n\t\troom,\n\t\toptions.webSocketSignalingServer,\n\t\toptions.browserDirect,\n\t\toptions.sendLastMessageOnReconnect,\n\t])\n\n\tconst sendMessageToProvider = useCallback(\n\t\t(message: string) => {\n\t\t\tentry.lastSentValue = message\n\t\t\tentry.consumer.send(message)\n\t\t},\n\t\t[entry],\n\t)\n\n\treturn { value, connectionStatus, sendMessageToProvider }\n}\n"],"names":["entries","Map","optionsKey","options","_options$webSocketSig","_options$browserDirec","_options$sendLastMess","concat","webSocketSignalingServer","browserDirect","sendLastMessageOnReconnect","getOrCreateEntry","key","room","peerId","_options$sendLastMess2","existing","get","newOptionsKey","destroyTimer","clearTimeout","optionsSnapshot","consumer","destroy","resolveFirst","firstValuePromise","Promise","resolve","entry","undefined","latestValue","lastSentValue","connectionStatus","setValue","setConnectionStatus","Client","onMessage","value","_entry$setValue","call","onConnected","_entry$setConnectionS","send","onDisconnected","_entry$setConnectionS2","set","scheduleDestroy","setTimeout","useDevicePortalConsumer","arguments","length","cacheKey","peerIdRef","useRef","generatePeerId","current","firstValue","use","_useState","useState","_entry$latestValue","_useState2","_slicedToArray","_useState3","_useState4","useEffect","e","sendMessageToProvider","useCallback","message"],"mappings":";;;;AAwDA,IAAMA,OAAO,GAAG,IAAIC,GAAG,EAAyB;AAEhD,SAASC,UAAUA,CAACC,OAAoC,EAAA;AAAA,EAAA,IAAAC,qBAAA,EAAAC,qBAAA,EAAAC,qBAAA;AACvD,EAAA,OAAA,EAAA,CAAAC,MAAA,CAAAH,CAAAA,qBAAA,GAAUD,OAAO,CAACK,wBAAwB,MAAAJ,IAAAA,IAAAA,qBAAA,KAAAA,MAAAA,GAAAA,qBAAA,GAAI,EAAE,EAAA,IAAA,CAAA,CAAAG,MAAA,CAAAF,CAAAA,qBAAA,GAAKF,OAAO,CAACM,aAA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AAE3C;AACA,IAAA,IAAIgC,CAAC,CAACpC,WAAW,KAAKD,SAAS,EAAE;AAChCI,MAAAA,QAAQ,CAACiC,CAAC,CAACpC,WAAW,CAAC;AACxB;AACAI,IAAAA,mBAAmB,CAACgC,CAAC,CAAClC,gBAAgB,CAAC;AAEvC,IAAA,OAAO,YAAK;MACXkC,CAAC,CAACjC,QAAQ,GAAG,IAAI;MACjBiC,CAAC,CAAChC,mBAAmB,GAAG,IAAI;MAC5BY,eAAe,CAACK,QAAQ,CAAC;KACzB;AACF,GAAC,EAAE,CACFA,QAAQ,EACRtC,IAAI,EACJV,OAAO,CAACK,wBAAwB,EAChCL,OAAO,CAACM,aAAa,EACrBN,OAAO,CAACO,0BAA0B,CAClC,CAAC;AAEF,EAAA,IAAMyD,qBAAqB,GAAGC,WAAW,CACxC,UAACC,OAAe,EAAI;IACnBzC,KAAK,CAACG,aAAa,GAAGsC,OAAO;AAC7BzC,IAAAA,KAAK,CAACN,QAAQ,CAACoB,IAAI,CAAC2B,OAAO,CAAC;AAC7B,GAAC,EACD,CAACzC,KAAK,CAAC,CACP;EAED,OAAO;AAAES,IAAAA,KAAK,EAALA,KAAK;AAAEL,IAAAA,gBAAgB,EAAhBA,gBAAgB;AAAEmC,IAAAA,qBAAqB,EAArBA;GAAuB;AAC1D;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@device-portal/react",
3
- "version": "0.0.23",
3
+ "version": "0.1.1",
4
4
  "description": "Simple WebRTC data channel for React.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -39,7 +39,7 @@
39
39
  "react": ">=19"
40
40
  },
41
41
  "dependencies": {
42
- "@device-portal/client": "^0.0.23"
42
+ "@device-portal/client": "^0.1.1"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@babel/core": "^7.24.0",