@assistant-ui/react-statewire 0.31.0 → 0.32.0

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.
@@ -1,4 +1,4 @@
1
- import { useEffect, useMemo, useRef } from "react";
1
+ import { useEffect, useMemo, useRef, useState } from "react";
2
2
  import { resource, useResource } from "@assistant-ui/tap";
3
3
  import { KeyValueSessionStorage } from "statewire/storage";
4
4
  import type { StatewireClient } from "statewire";
@@ -180,6 +180,8 @@ const SESSION_LOCK_PREFIX = "aui:statewire-session-lock:";
180
180
 
181
181
  const SESSION_ACTIVE_PREFIX = "aui:statewire-session-active:";
182
182
 
183
+ const TAB_LOCK_PREFIX = "statewire-tab:";
184
+
183
185
  const sessionBackend: KeyValueSessionStorage.Backend = {
184
186
  getItem: (key) => localStorage.getItem(key),
185
187
  setItem: (key, value) => localStorage.setItem(key, value),
@@ -222,6 +224,19 @@ const useLocalStorageSession = ({
222
224
  usePrincipal(principal);
223
225
  const storageKey = SESSION_PREFIX + scopeKey;
224
226
  const lockName = SESSION_LOCK_PREFIX + scopeKey;
227
+ const [tabId] = useState(() => crypto.randomUUID());
228
+ const latestOnError = useRef(onError);
229
+ latestOnError.current = onError;
230
+ useEffect(() => {
231
+ let release: (() => void) | undefined;
232
+ const held = new Promise<void>((resolve) => {
233
+ release = resolve;
234
+ });
235
+ void navigator.locks
236
+ .request(TAB_LOCK_PREFIX + tabId, () => held)
237
+ .catch((error: unknown) => latestOnError.current?.(error));
238
+ return () => release?.();
239
+ }, [tabId]);
225
240
  useEffect(() => {
226
241
  let release: (() => void) | undefined;
227
242
  let disposed = false;
@@ -253,6 +268,11 @@ const useLocalStorageSession = ({
253
268
  (): StatewireClient.SessionStorage => ({
254
269
  ...storage,
255
270
  lock: (fn) => navigator.locks.request(lockName, async () => fn()),
271
+ owner: {
272
+ id: tabId,
273
+ whenGone: (id, signal) =>
274
+ navigator.locks.request(TAB_LOCK_PREFIX + id, { signal }, () => {}),
275
+ },
256
276
  subscribe: (listener) => {
257
277
  const onStorage = (event: StorageEvent) => {
258
278
  if (event.key === storageKey) listener();
@@ -261,7 +281,7 @@ const useLocalStorageSession = ({
261
281
  return () => window.removeEventListener("storage", onStorage);
262
282
  },
263
283
  }),
264
- [storage, storageKey, lockName],
284
+ [storage, storageKey, lockName, tabId],
265
285
  );
266
286
  };
267
287
 
@@ -6,7 +6,7 @@ import { runsOptimistic } from "./runsOptimistic";
6
6
 
7
7
  type Draft = Record<string, any>;
8
8
 
9
- const ctx = { ts: 0, seq: 7 };
9
+ const ctx = { ts: 0, seq: 7, held: null };
10
10
 
11
11
  const mount = (entry?: Draft): Draft =>
12
12
  entry === undefined
@@ -147,7 +147,7 @@ export const runsOptimistic = (
147
147
  const draft = mount as RunsDraft;
148
148
  const commandRunId = (command.params[0] as { runId?: string } | undefined)
149
149
  ?.runId;
150
- const runId = commandRunId ?? `optimistic-${ctx.seq}`;
150
+ const runId = commandRunId ?? `optimistic-${ctx.seq ?? `q${ctx.ts}`}`;
151
151
  switch (command.method) {
152
152
  case "run/enqueue":
153
153
  case "run/steer": {