reifyui 0.13.9 → 0.13.12

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.
@@ -29,12 +29,15 @@ function keepPresenceAlive(awareness, intervalMs = TICK_MS) {
29
29
  // src/collab/useLiveDoc.js
30
30
  import { useCallback, useEffect, useRef, useState } from "react";
31
31
  import { HocuspocusProvider } from "@hocuspocus/provider";
32
+ var REFUSED = "refused";
33
+ var DISCONNECT_GRACE_MS = 2e3;
32
34
  function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
33
35
  const [doc, setDoc] = useState(null);
34
36
  const [provider, setProvider] = useState(null);
35
37
  const [ready, setReady] = useState(false);
36
38
  const [states, setStates] = useState([]);
37
39
  const [selfClientId, setSelfClientId] = useState(null);
40
+ const [stopped, setStopped] = useState(false);
38
41
  const providerRef = useRef(null);
39
42
  const lastRef = useRef(null);
40
43
  useEffect(() => {
@@ -42,6 +45,7 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
42
45
  let dead = false;
43
46
  let current = null;
44
47
  let retry = null;
48
+ let grace = null;
45
49
  let stopKeepAlive = () => {
46
50
  };
47
51
  async function connect() {
@@ -52,21 +56,48 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
52
56
  ticket = null;
53
57
  }
54
58
  if (dead) return;
59
+ if (ticket && ticket.refused) {
60
+ setStopped(true);
61
+ return;
62
+ }
55
63
  if (!ticket) {
56
64
  retry = window.setTimeout(connect, 6e4);
57
65
  return;
58
66
  }
67
+ let first = ticket.token;
59
68
  current = new HocuspocusProvider({
60
69
  url: ticket.url,
61
70
  name: ticket.doc,
62
- parameters: { cg_token: ticket.token },
63
- onAuthenticationFailed: () => {
71
+ token: async () => {
72
+ if (first) {
73
+ const t = first;
74
+ first = null;
75
+ return t;
76
+ }
77
+ let next = null;
78
+ try {
79
+ next = await fetchTicket(resourceId);
80
+ } catch {
81
+ next = null;
82
+ }
83
+ if (next && next.refused) throw new Error(REFUSED);
84
+ if (!next || !next.token) throw new Error("unavailable");
85
+ return next.token;
86
+ },
87
+ onAuthenticationFailed: ({ reason } = {}) => {
64
88
  stopKeepAlive();
65
89
  try {
66
90
  current.destroy();
67
91
  } catch {
68
92
  }
69
- if (!dead) retry = window.setTimeout(connect, 5e3);
93
+ if (dead) return;
94
+ window.clearTimeout(grace);
95
+ setReady(false);
96
+ if (String(reason || "").includes(REFUSED)) {
97
+ setStopped(true);
98
+ return;
99
+ }
100
+ retry = window.setTimeout(connect, 5e3);
70
101
  }
71
102
  });
72
103
  stopKeepAlive = keepPresenceAlive(current.awareness);
@@ -83,18 +114,28 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
83
114
  });
84
115
  current.on("synced", () => {
85
116
  if (dead) return;
117
+ window.clearTimeout(grace);
86
118
  setSelfClientId(current.awareness ? current.awareness.clientID : null);
87
119
  setDoc(current.document);
88
120
  setReady(true);
89
121
  });
90
122
  current.on("status", ({ status }) => {
91
- if (!dead && status === "disconnected") setReady(false);
123
+ if (dead) return;
124
+ if (status === "connected") window.clearTimeout(grace);
125
+ if (status === "disconnected") {
126
+ window.clearTimeout(grace);
127
+ grace = window.setTimeout(() => {
128
+ if (!dead) setReady(false);
129
+ }, DISCONNECT_GRACE_MS);
130
+ }
92
131
  });
93
132
  }
133
+ setStopped(false);
94
134
  connect();
95
135
  return () => {
96
136
  dead = true;
97
137
  window.clearTimeout(retry);
138
+ window.clearTimeout(grace);
98
139
  stopKeepAlive();
99
140
  if (current) {
100
141
  try {
@@ -120,11 +161,11 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
120
161
  }
121
162
  }
122
163
  }, [field]);
123
- return { doc, provider, ready, states, selfClientId, setAwareness };
164
+ return { doc, provider, ready, stopped, states, selfClientId, setAwareness };
124
165
  }
125
166
 
126
167
  export {
127
168
  keepPresenceAlive,
128
169
  useLiveDoc
129
170
  };
130
- //# sourceMappingURL=chunk-UKG4Z6QK.js.map
171
+ //# sourceMappingURL=chunk-Q342JSCO.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/collab/keepAlive.js","../src/collab/useLiveDoc.js"],"sourcesContent":["// keepPresenceAlive — renew this client's awareness on a clock the browser\n// does not throttle.\n//\n// Yjs awareness renews a client's own state every 15s on a main-thread timer\n// and every other client drops a state it has not heard from for 30s. A tab\n// in the background gets its timers throttled to once a minute, so its\n// presence expires for everyone else and reappears when the timer finally\n// fires: a flag that flickers every minute on every other screen. Worker\n// clocks are not throttled, so the renewal runs there and the main thread\n// only relays it. One helper for every live document (sheets, canvases,\n// docs).\nconst TICK_MS = 10_000;\n\nexport function keepPresenceAlive(awareness, intervalMs = TICK_MS) {\n if (!awareness || typeof Worker === 'undefined' || typeof Blob === 'undefined') return () => {};\n let url = '';\n let worker = null;\n try {\n url = URL.createObjectURL(new Blob([`setInterval(() => postMessage(0), ${Math.max(1000, intervalMs)});`],\n { type: 'text/javascript' }));\n worker = new Worker(url);\n } catch {\n if (url) URL.revokeObjectURL(url);\n return () => {};\n }\n worker.onmessage = () => {\n const state = awareness.getLocalState();\n if (state !== null && state !== undefined) awareness.setLocalState(state); // same state, new clock\n };\n return () => {\n worker.terminate();\n URL.revokeObjectURL(url);\n };\n}\n","// useLiveDoc — the one provider lifecycle for every live document on the\n// platform's realtime sidecar: a sheet, a graph canvas, a doc. It takes a\n// host-authenticated ticket ({url, doc, token}), connects, and asks the host\n// for a fresh ticket on EVERY connect (the provider's token): the sidecar closes\n// a connection when its ticket expires, so the person's access is checked again.\n// fetchTicket returns a ticket, null (unreachable: retried quietly), or\n// {refused: true} (the person's access was refused: `stopped`, no retry). It exposes\n// the document once synced, the awareness states of everyone in it, and one\n// field for what THIS person publishes (re-applied on every reconnect).\n// Surfaces build their own shape on top: what to read from the document and\n// what presence means for them.\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { HocuspocusProvider } from '@hocuspocus/provider';\nimport { keepPresenceAlive } from './keepAlive.js';\n\nconst REFUSED = 'refused';\nconst DISCONNECT_GRACE_MS = 2_000; // a ticket's planned close re-syncs well inside this\n\nexport function useLiveDoc(resourceId, { fetchTicket, field = 'presence' } = {}) {\n const [doc, setDoc] = useState(null); // the Y.Doc, once synced\n const [provider, setProvider] = useState(null);\n const [ready, setReady] = useState(false);\n const [states, setStates] = useState([]); // [{clientId, ...awareness}]\n const [selfClientId, setSelfClientId] = useState(null);\n const [stopped, setStopped] = useState(false); // live access refused (removed, signed out): reload\n const providerRef = useRef(null);\n const lastRef = useRef(null); // this person's last published value\n\n useEffect(() => {\n if (!resourceId || typeof fetchTicket !== 'function') return undefined;\n let dead = false;\n let current = null;\n let retry = null;\n let grace = null;\n let stopKeepAlive = () => {};\n\n async function connect() {\n let ticket = null;\n try { ticket = await fetchTicket(resourceId); } catch { ticket = null; }\n if (dead) return;\n if (ticket && ticket.refused) { setStopped(true); return; }\n if (!ticket) { retry = window.setTimeout(connect, 60_000); return; } // realtime unreachable: try later\n let first = ticket.token;\n current = new HocuspocusProvider({\n url: ticket.url, name: ticket.doc,\n token: async () => {\n if (first) { const t = first; first = null; return t; }\n let next = null;\n try { next = await fetchTicket(resourceId); } catch { next = null; }\n if (next && next.refused) throw new Error(REFUSED);\n if (!next || !next.token) throw new Error('unavailable');\n return next.token;\n },\n onAuthenticationFailed: ({ reason } = {}) => {\n stopKeepAlive();\n try { current.destroy(); } catch { /* down */ }\n if (dead) return;\n window.clearTimeout(grace);\n setReady(false);\n if (String(reason || '').includes(REFUSED)) { setStopped(true); return; }\n retry = window.setTimeout(connect, 5_000); // unavailable for now: a fresh ticket shortly\n },\n });\n stopKeepAlive = keepPresenceAlive(current.awareness);\n providerRef.current = current;\n setProvider(current);\n if (lastRef.current) { try { current.setAwarenessField(field, lastRef.current); } catch { /* not yet */ } }\n current.on('awarenessUpdate', ({ states: next }) => { if (!dead) setStates(next || []); });\n current.on('synced', () => {\n if (dead) return;\n window.clearTimeout(grace);\n setSelfClientId(current.awareness ? current.awareness.clientID : null);\n setDoc(current.document);\n setReady(true);\n });\n current.on('status', ({ status }) => {\n if (dead) return;\n if (status === 'connected') window.clearTimeout(grace);\n // a planned close at ticket expiry reconnects at once; only a real outage reads as offline\n if (status === 'disconnected') {\n window.clearTimeout(grace);\n grace = window.setTimeout(() => { if (!dead) setReady(false); }, DISCONNECT_GRACE_MS);\n }\n });\n }\n setStopped(false);\n connect();\n return () => {\n dead = true; window.clearTimeout(retry); window.clearTimeout(grace);\n stopKeepAlive();\n if (current) { try { current.destroy(); } catch { /* closing */ } }\n providerRef.current = null;\n setProvider(null); setDoc(null); setReady(false); setStates([]); setSelfClientId(null);\n };\n // fetchTicket is a host constant; re-subscribing on its identity would reconnect every render.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [resourceId]);\n\n /** Publish what this person is doing; kept and re-applied across reconnects. */\n const setAwareness = useCallback((value) => {\n lastRef.current = value;\n const p = providerRef.current;\n if (p) { try { p.setAwarenessField(field, value); } catch { /* closing */ } }\n }, [field]);\n\n return { doc, provider, ready, stopped, states, selfClientId, setAwareness };\n}\n"],"mappings":";AAWA,IAAM,UAAU;AAET,SAAS,kBAAkB,WAAW,aAAa,SAAS;AACjE,MAAI,CAAC,aAAa,OAAO,WAAW,eAAe,OAAO,SAAS,YAAa,QAAO,MAAM;AAAA,EAAC;AAC9F,MAAI,MAAM;AACV,MAAI,SAAS;AACb,MAAI;AACF,UAAM,IAAI,gBAAgB,IAAI;AAAA,MAAK,CAAC,qCAAqC,KAAK,IAAI,KAAM,UAAU,CAAC,IAAI;AAAA,MACnE,EAAE,MAAM,kBAAkB;AAAA,IAAC,CAAC;AAChE,aAAS,IAAI,OAAO,GAAG;AAAA,EACzB,QAAQ;AACN,QAAI,IAAK,KAAI,gBAAgB,GAAG;AAChC,WAAO,MAAM;AAAA,IAAC;AAAA,EAChB;AACA,SAAO,YAAY,MAAM;AACvB,UAAM,QAAQ,UAAU,cAAc;AACtC,QAAI,UAAU,QAAQ,UAAU,OAAW,WA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AAU,CAAC,EAAE,OAAO,MAAM;AACnC,YAAI,KAAM;AACV,YAAI,WAAW,YAAa,QAAO,aAAa,KAAK;AAErD,YAAI,WAAW,gBAAgB;AAC7B,iBAAO,aAAa,KAAK;AACzB,kBAAQ,OAAO,WAAW,MAAM;AAAE,gBAAI,CAAC,KAAM,UAAS,KAAK;AAAA,UAAG,GAAG,mBAAmB;AAAA,QACtF;AAAA,MACF,CAAC;AAAA,IACH;AACA,eAAW,KAAK;AAChB,YAAQ;AACR,WAAO,MAAM;AACX,aAAO;AAAM,aAAO,aAAa,KAAK;AAAG,aAAO,aAAa,KAAK;AAClE,oBAAc;AACd,UAAI,SAAS;AAAE,YAAI;AAAE,kBAAQ,QAAQ;AAAA,QAAG,QAAQ;AAAA,QAAgB;AAAA,MAAE;AAClE,kBAAY,UAAU;AACtB,kBAAY,IAAI;AAAG,aAAO,IAAI;AAAG,eAAS,KAAK;AAAG,gBAAU,CAAC,CAAC;AAAG,sBAAgB,IAAI;AAAA,IACvF;AAAA,EAGF,GAAG,CAAC,UAAU,CAAC;AAGf,QAAM,eAAe,YAAY,CAAC,UAAU;AAC1C,YAAQ,UAAU;AAClB,UAAM,IAAI,YAAY;AACtB,QAAI,GAAG;AAAE,UAAI;AAAE,UAAE,kBAAkB,OAAO,KAAK;AAAA,MAAG,QAAQ;AAAA,MAAgB;AAAA,IAAE;AAAA,EAC9E,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO,EAAE,KAAK,UAAU,OAAO,SAAS,QAAQ,cAAc,aAAa;AAC7E;","names":[]}
package/dist/collab.cjs CHANGED
@@ -60,12 +60,15 @@ function keepPresenceAlive(awareness, intervalMs = TICK_MS) {
60
60
  }
61
61
 
62
62
  // src/collab/useLiveDoc.js
63
+ var REFUSED = "refused";
64
+ var DISCONNECT_GRACE_MS = 2e3;
63
65
  function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
64
66
  const [doc, setDoc] = (0, import_react.useState)(null);
65
67
  const [provider, setProvider] = (0, import_react.useState)(null);
66
68
  const [ready, setReady] = (0, import_react.useState)(false);
67
69
  const [states, setStates] = (0, import_react.useState)([]);
68
70
  const [selfClientId, setSelfClientId] = (0, import_react.useState)(null);
71
+ const [stopped, setStopped] = (0, import_react.useState)(false);
69
72
  const providerRef = (0, import_react.useRef)(null);
70
73
  const lastRef = (0, import_react.useRef)(null);
71
74
  (0, import_react.useEffect)(() => {
@@ -73,6 +76,7 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
73
76
  let dead = false;
74
77
  let current = null;
75
78
  let retry = null;
79
+ let grace = null;
76
80
  let stopKeepAlive = () => {
77
81
  };
78
82
  async function connect() {
@@ -83,21 +87,48 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
83
87
  ticket = null;
84
88
  }
85
89
  if (dead) return;
90
+ if (ticket && ticket.refused) {
91
+ setStopped(true);
92
+ return;
93
+ }
86
94
  if (!ticket) {
87
95
  retry = window.setTimeout(connect, 6e4);
88
96
  return;
89
97
  }
98
+ let first = ticket.token;
90
99
  current = new import_provider.HocuspocusProvider({
91
100
  url: ticket.url,
92
101
  name: ticket.doc,
93
- parameters: { cg_token: ticket.token },
94
- onAuthenticationFailed: () => {
102
+ token: async () => {
103
+ if (first) {
104
+ const t = first;
105
+ first = null;
106
+ return t;
107
+ }
108
+ let next = null;
109
+ try {
110
+ next = await fetchTicket(resourceId);
111
+ } catch {
112
+ next = null;
113
+ }
114
+ if (next && next.refused) throw new Error(REFUSED);
115
+ if (!next || !next.token) throw new Error("unavailable");
116
+ return next.token;
117
+ },
118
+ onAuthenticationFailed: ({ reason } = {}) => {
95
119
  stopKeepAlive();
96
120
  try {
97
121
  current.destroy();
98
122
  } catch {
99
123
  }
100
- if (!dead) retry = window.setTimeout(connect, 5e3);
124
+ if (dead) return;
125
+ window.clearTimeout(grace);
126
+ setReady(false);
127
+ if (String(reason || "").includes(REFUSED)) {
128
+ setStopped(true);
129
+ return;
130
+ }
131
+ retry = window.setTimeout(connect, 5e3);
101
132
  }
102
133
  });
103
134
  stopKeepAlive = keepPresenceAlive(current.awareness);
@@ -114,18 +145,28 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
114
145
  });
115
146
  current.on("synced", () => {
116
147
  if (dead) return;
148
+ window.clearTimeout(grace);
117
149
  setSelfClientId(current.awareness ? current.awareness.clientID : null);
118
150
  setDoc(current.document);
119
151
  setReady(true);
120
152
  });
121
153
  current.on("status", ({ status }) => {
122
- if (!dead && status === "disconnected") setReady(false);
154
+ if (dead) return;
155
+ if (status === "connected") window.clearTimeout(grace);
156
+ if (status === "disconnected") {
157
+ window.clearTimeout(grace);
158
+ grace = window.setTimeout(() => {
159
+ if (!dead) setReady(false);
160
+ }, DISCONNECT_GRACE_MS);
161
+ }
123
162
  });
124
163
  }
164
+ setStopped(false);
125
165
  connect();
126
166
  return () => {
127
167
  dead = true;
128
168
  window.clearTimeout(retry);
169
+ window.clearTimeout(grace);
129
170
  stopKeepAlive();
130
171
  if (current) {
131
172
  try {
@@ -151,7 +192,7 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
151
192
  }
152
193
  }
153
194
  }, [field]);
154
- return { doc, provider, ready, states, selfClientId, setAwareness };
195
+ return { doc, provider, ready, stopped, states, selfClientId, setAwareness };
155
196
  }
156
197
 
157
198
  // src/collab/colors.js
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/collab/index.js","../src/collab/useLiveDoc.js","../src/collab/keepAlive.js","../src/collab/colors.js"],"sourcesContent":["// reifyui/collab — one live-document core for any collaborative surface.\n//\n// A separate entry because it pulls in yjs and the Hocuspocus provider: importing a Button\n// from the root never costs a CRDT. Presence colors and the worker-clock keep-alive ride along\n// (they have no dependencies) so a host wires collaboration from one import.\nexport { useLiveDoc } from './useLiveDoc.js';\nexport { keepPresenceAlive } from './keepAlive.js';\nexport { PEER_PALETTE, peerColor, withAlpha } from './colors.js';\n","// useLiveDoc — the one provider lifecycle for every live document on the\n// platform's realtime sidecar: a sheet, a graph canvas, a doc. It takes a\n// host-authenticated ticket ({url, doc, token}), connects, re-tickets when the\n// token ages out, retries quietly when realtime is unreachable, and exposes\n// the document once synced, the awareness states of everyone in it, and one\n// field for what THIS person publishes (re-applied on every reconnect).\n// Surfaces build their own shape on top: what to read from the document and\n// what presence means for them.\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { HocuspocusProvider } from '@hocuspocus/provider';\nimport { keepPresenceAlive } from './keepAlive.js';\n\nexport function useLiveDoc(resourceId, { fetchTicket, field = 'presence' } = {}) {\n const [doc, setDoc] = useState(null); // the Y.Doc, once synced\n const [provider, setProvider] = useState(null);\n const [ready, setReady] = useState(false);\n const [states, setStates] = useState([]); // [{clientId, ...awareness}]\n const [selfClientId, setSelfClientId] = useState(null);\n const providerRef = useRef(null);\n const lastRef = useRef(null); // this person's last published value\n\n useEffect(() => {\n if (!resourceId || typeof fetchTicket !== 'function') return undefined;\n let dead = false;\n let current = null;\n let retry = null;\n let stopKeepAlive = () => {};\n\n async function connect() {\n let ticket = null;\n try { ticket = await fetchTicket(resourceId); } catch { ticket = null; }\n if (dead) return;\n if (!ticket) { retry = window.setTimeout(connect, 60_000); return; } // realtime unreachable: try later\n current = new HocuspocusProvider({\n url: ticket.url, name: ticket.doc, parameters: { cg_token: ticket.token },\n onAuthenticationFailed: () => {\n // The token aged out (or access was revoked): tear down and re-ticket.\n stopKeepAlive();\n try { current.destroy(); } catch { /* down */ }\n if (!dead) retry = window.setTimeout(connect, 5_000);\n },\n });\n stopKeepAlive = keepPresenceAlive(current.awareness);\n providerRef.current = current;\n setProvider(current);\n if (lastRef.current) { try { current.setAwarenessField(field, lastRef.current); } catch { /* not yet */ } }\n current.on('awarenessUpdate', ({ states: next }) => { if (!dead) setStates(next || []); });\n current.on('synced', () => {\n if (dead) return;\n setSelfClientId(current.awareness ? current.awareness.clientID : null);\n setDoc(current.document);\n setReady(true);\n });\n current.on('status', ({ status }) => { if (!dead && status === 'disconnected') setReady(false); });\n }\n connect();\n return () => {\n dead = true; window.clearTimeout(retry);\n stopKeepAlive();\n if (current) { try { current.destroy(); } catch { /* closing */ } }\n providerRef.current = null;\n setProvider(null); setDoc(null); setReady(false); setStates([]); setSelfClientId(null);\n };\n // fetchTicket is a host constant; re-subscribing on its identity would reconnect every render.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [resourceId]);\n\n /** Publish what this person is doing; kept and re-applied across reconnects. */\n const setAwareness = useCallback((value) => {\n lastRef.current = value;\n const p = providerRef.current;\n if (p) { try { p.setAwarenessField(field, value); } catch { /* closing */ } }\n }, [field]);\n\n return { doc, provider, ready, states, selfClientId, setAwareness };\n}\n","// keepPresenceAlive — renew this client's awareness on a clock the browser\n// does not throttle.\n//\n// Yjs awareness renews a client's own state every 15s on a main-thread timer\n// and every other client drops a state it has not heard from for 30s. A tab\n// in the background gets its timers throttled to once a minute, so its\n// presence expires for everyone else and reappears when the timer finally\n// fires: a flag that flickers every minute on every other screen. Worker\n// clocks are not throttled, so the renewal runs there and the main thread\n// only relays it. One helper for every live document (sheets, canvases,\n// docs).\nconst TICK_MS = 10_000;\n\nexport function keepPresenceAlive(awareness, intervalMs = TICK_MS) {\n if (!awareness || typeof Worker === 'undefined' || typeof Blob === 'undefined') return () => {};\n let url = '';\n let worker = null;\n try {\n url = URL.createObjectURL(new Blob([`setInterval(() => postMessage(0), ${Math.max(1000, intervalMs)});`],\n { type: 'text/javascript' }));\n worker = new Worker(url);\n } catch {\n if (url) URL.revokeObjectURL(url);\n return () => {};\n }\n worker.onmessage = () => {\n const state = awareness.getLocalState();\n if (state !== null && state !== undefined) awareness.setLocalState(state); // same state, new clock\n };\n return () => {\n worker.terminate();\n URL.revokeObjectURL(url);\n };\n}\n","// One palette for everyone who shows up in a shared document: the cursor in a\n// doc, the selection in a sheet, the drag on a graph canvas. A person's color\n// comes from their member id, so they look the same in every surface and to\n// every viewer, with no per-document assignment to disagree.\nexport const PEER_PALETTE = ['#1e40af', '#c2410c', '#15803d', '#6d28d9', '#be123c', '#0e7490', '#a16207', '#4338ca'];\n\nexport function peerColor(key) {\n let h = 0;\n for (const ch of String(key || '')) h = (h * 31 + ch.charCodeAt(0)) >>> 0;\n return PEER_PALETTE[h % PEER_PALETTE.length];\n}\n\n/** rgba() of a #rrggbb color. */\nexport function withAlpha(hex, alpha) {\n const m = /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(String(hex || ''));\n if (!m) return hex;\n return `rgba(${parseInt(m[1], 16)}, ${parseInt(m[2], 16)}, ${parseInt(m[3], 16)}, ${alpha})`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACQA,mBAAyD;AACzD,sBAAmC;;;ACEnC,IAAM,UAAU;AAET,SAAS,kBAAkB,WAAW,aAAa,SAAS;AACjE,MAAI,CAAC,aAAa,OAAO,WAAW,eAAe,OAAO,SAAS,YAAa,QAAO,MAAM;AAAA,EAAC;AAC9F,MAAI,MAAM;AACV,MAAI,SAAS;AACb,MAAI;AACF,UAAM,IAAI,gBAAgB,IAAI;AAAA,MAAK,CAAC,qCAAqC,KAAK,IAAI,KAAM,UAAU,CAAC,IAAI;AAAA,MACnE,EAAE,MAAM,kBAAkB;AAAA,IAAC,CAAC;AAChE,aAAS,IAAI,OAAO,GAAG;AAAA,EACzB,QAAQ;AACN,QAAI,IAAK,KAAI,gBAAgB,GAAG;AAChC,WAAO,MAAM;AAAA,IAAC;AAAA,EAChB;AACA,SAAO,YAAY,MAAM;AACvB,UAAM,QAAQ,UAAU,cAAc;AACtC,QAAI,UAAU,QAAQ,UAAU,OAAW,WAAU,cAAc,KAAK;AAAA,EAC1E;AACA,SAAO,MAAM;AACX,WAAO,UAAU;AACjB,QAAI,gBAAgB,GAAG;AAAA,EACzB;AACF;;;ADrBO,SAAS,WAAW,YAAY,EAAE,aAAa,QAAQ,WAAW,IAAI,CAAC,GAAG;AAC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;AACxE,SAAO,aAAa,IAAI,aAAa,MAAM;AAC7C;AAGO,SAAS,UAAU,KAAK,OAAO;AACpC,QAAM,IAAI,+CAA+C,KAAK,OAAO,OAAO,EAAE,CAAC;AAC/E,MAAI,CAAC,EAAG,QAAO;AACf,SAAO,QAAQ,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,KAAK;AAC3F;","names":[]}
1
+ {"version":3,"sources":["../src/collab/index.js","../src/collab/useLiveDoc.js","../src/collab/keepAlive.js","../src/collab/colors.js"],"sourcesContent":["// reifyui/collab — one live-document core for any collaborative surface.\n//\n// A separate entry because it pulls in yjs and the Hocuspocus provider: importing a Button\n// from the root never costs a CRDT. Presence colors and the worker-clock keep-alive ride along\n// (they have no dependencies) so a host wires collaboration from one import.\nexport { useLiveDoc } from './useLiveDoc.js';\nexport { keepPresenceAlive } from './keepAlive.js';\nexport { PEER_PALETTE, peerColor, withAlpha } from './colors.js';\n","// useLiveDoc — the one provider lifecycle for every live document on the\n// platform's realtime sidecar: a sheet, a graph canvas, a doc. It takes a\n// host-authenticated ticket ({url, doc, token}), connects, and asks the host\n// for a fresh ticket on EVERY connect (the provider's token): the sidecar closes\n// a connection when its ticket expires, so the person's access is checked again.\n// fetchTicket returns a ticket, null (unreachable: retried quietly), or\n// {refused: true} (the person's access was refused: `stopped`, no retry). It exposes\n// the document once synced, the awareness states of everyone in it, and one\n// field for what THIS person publishes (re-applied on every reconnect).\n// Surfaces build their own shape on top: what to read from the document and\n// what presence means for them.\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { HocuspocusProvider } from '@hocuspocus/provider';\nimport { keepPresenceAlive } from './keepAlive.js';\n\nconst REFUSED = 'refused';\nconst DISCONNECT_GRACE_MS = 2_000; // a ticket's planned close re-syncs well inside this\n\nexport function useLiveDoc(resourceId, { fetchTicket, field = 'presence' } = {}) {\n const [doc, setDoc] = useState(null); // the Y.Doc, once synced\n const [provider, setProvider] = useState(null);\n const [ready, setReady] = useState(false);\n const [states, setStates] = useState([]); // [{clientId, ...awareness}]\n const [selfClientId, setSelfClientId] = useState(null);\n const [stopped, setStopped] = useState(false); // live access refused (removed, signed out): reload\n const providerRef = useRef(null);\n const lastRef = useRef(null); // this person's last published value\n\n useEffect(() => {\n if (!resourceId || typeof fetchTicket !== 'function') return undefined;\n let dead = false;\n let current = null;\n let retry = null;\n let grace = null;\n let stopKeepAlive = () => {};\n\n async function connect() {\n let ticket = null;\n try { ticket = await fetchTicket(resourceId); } catch { ticket = null; }\n if (dead) return;\n if (ticket && ticket.refused) { setStopped(true); return; }\n if (!ticket) { retry = window.setTimeout(connect, 60_000); return; } // realtime unreachable: try later\n let first = ticket.token;\n current = new HocuspocusProvider({\n url: ticket.url, name: ticket.doc,\n token: async () => {\n if (first) { const t = first; first = null; return t; }\n let next = null;\n try { next = await fetchTicket(resourceId); } catch { next = null; }\n if (next && next.refused) throw new Error(REFUSED);\n if (!next || !next.token) throw new Error('unavailable');\n return next.token;\n },\n onAuthenticationFailed: ({ reason } = {}) => {\n stopKeepAlive();\n try { current.destroy(); } catch { /* down */ }\n if (dead) return;\n window.clearTimeout(grace);\n setReady(false);\n if (String(reason || '').includes(REFUSED)) { setStopped(true); return; }\n retry = window.setTimeout(connect, 5_000); // unavailable for now: a fresh ticket shortly\n },\n });\n stopKeepAlive = keepPresenceAlive(current.awareness);\n providerRef.current = current;\n setProvider(current);\n if (lastRef.current) { try { current.setAwarenessField(field, lastRef.current); } catch { /* not yet */ } }\n current.on('awarenessUpdate', ({ states: next }) => { if (!dead) setStates(next || []); });\n current.on('synced', () => {\n if (dead) return;\n window.clearTimeout(grace);\n setSelfClientId(current.awareness ? current.awareness.clientID : null);\n setDoc(current.document);\n setReady(true);\n });\n current.on('status', ({ status }) => {\n if (dead) return;\n if (status === 'connected') window.clearTimeout(grace);\n // a planned close at ticket expiry reconnects at once; only a real outage reads as offline\n if (status === 'disconnected') {\n window.clearTimeout(grace);\n grace = window.setTimeout(() => { if (!dead) setReady(false); }, DISCONNECT_GRACE_MS);\n }\n });\n }\n setStopped(false);\n connect();\n return () => {\n dead = true; window.clearTimeout(retry); window.clearTimeout(grace);\n stopKeepAlive();\n if (current) { try { current.destroy(); } catch { /* closing */ } }\n providerRef.current = null;\n setProvider(null); setDoc(null); setReady(false); setStates([]); setSelfClientId(null);\n };\n // fetchTicket is a host constant; re-subscribing on its identity would reconnect every render.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [resourceId]);\n\n /** Publish what this person is doing; kept and re-applied across reconnects. */\n const setAwareness = useCallback((value) => {\n lastRef.current = value;\n const p = providerRef.current;\n if (p) { try { p.setAwarenessField(field, value); } catch { /* closing */ } }\n }, [field]);\n\n return { doc, provider, ready, stopped, states, selfClientId, setAwareness };\n}\n","// keepPresenceAlive — renew this client's awareness on a clock the browser\n// does not throttle.\n//\n// Yjs awareness renews a client's own state every 15s on a main-thread timer\n// and every other client drops a state it has not heard from for 30s. A tab\n// in the background gets its timers throttled to once a minute, so its\n// presence expires for everyone else and reappears when the timer finally\n// fires: a flag that flickers every minute on every other screen. Worker\n// clocks are not throttled, so the renewal runs there and the main thread\n// only relays it. One helper for every live document (sheets, canvases,\n// docs).\nconst TICK_MS = 10_000;\n\nexport function keepPresenceAlive(awareness, intervalMs = TICK_MS) {\n if (!awareness || typeof Worker === 'undefined' || typeof Blob === 'undefined') return () => {};\n let url = '';\n let worker = null;\n try {\n url = URL.createObjectURL(new Blob([`setInterval(() => postMessage(0), ${Math.max(1000, intervalMs)});`],\n { type: 'text/javascript' }));\n worker = new Worker(url);\n } catch {\n if (url) URL.revokeObjectURL(url);\n return () => {};\n }\n worker.onmessage = () => {\n const state = awareness.getLocalState();\n if (state !== null && state !== undefined) awareness.setLocalState(state); // same state, new clock\n };\n return () => {\n worker.terminate();\n URL.revokeObjectURL(url);\n };\n}\n","// One palette for everyone who shows up in a shared document: the cursor in a\n// doc, the selection in a sheet, the drag on a graph canvas. A person's color\n// comes from their member id, so they look the same in every surface and to\n// every viewer, with no per-document assignment to disagree.\nexport const PEER_PALETTE = ['#1e40af', '#c2410c', '#15803d', '#6d28d9', '#be123c', '#0e7490', '#a16207', '#4338ca'];\n\nexport function peerColor(key) {\n let h = 0;\n for (const ch of String(key || '')) h = (h * 31 + ch.charCodeAt(0)) >>> 0;\n return PEER_PALETTE[h % PEER_PALETTE.length];\n}\n\n/** rgba() of a #rrggbb color. */\nexport function withAlpha(hex, alpha) {\n const m = /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(String(hex || ''));\n if (!m) return hex;\n return `rgba(${parseInt(m[1], 16)}, ${parseInt(m[2], 16)}, ${parseInt(m[3], 16)}, ${alpha})`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWA,mBAAyD;AACzD,sBAAmC;;;ACDnC,IAAM,UAAU;AAET,SAAS,kBAAkB,WAAW,aAAa,SAAS;AACjE,MAAI,CAAC,aAAa,OAAO,WAAW,eAAe,OAAO,SAAS,YAAa,QAAO,MAAM;AAAA,EAAC;AAC9F,MAAI,MAAM;AACV,MAAI,SAAS;AACb,MAAI;AACF,UAAM,IAAI,gBAAgB,IAAI;AAAA,MAAK,CAAC,qCAAqC,KAAK,IAAI,KAAM,UAAU,CAAC,IAAI;AAAA,MACnE,EAAE,MAAM,kBAAkB;AAAA,IAAC,CAAC;AAChE,aAAS,IAAI,OAAO,GAAG;AAAA,EACzB,QAAQ;AACN,QAAI,IAAK,KAAI,gBAAgB,GAAG;AAChC,WAAO,MAAM;AAAA,IAAC;AAAA,EAChB;AACA,SAAO,YAAY,MAAM;AACvB,UAAM,QAAQ,UAAU,cAAc;AACtC,QAAI,UAAU,QAAQ,UAAU,OAAW,WAAU,cAAc,KAAK;AAAA,EAC1E;AACA,SAAO,MAAM;AACX,W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AQ,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,KAAK;AAC3F;","names":[]}
package/dist/collab.d.ts CHANGED
@@ -18,6 +18,8 @@ export interface LiveDoc {
18
18
  synced: boolean;
19
19
  status: string;
20
20
  awareness: unknown;
21
+ /** Live editing was refused for this person (the host's ticket route said 401/403): no retry. */
22
+ stopped?: boolean;
21
23
  [k: string]: unknown;
22
24
  }
23
25
  /** One live-document core: provider lifecycle, sync state, awareness, for any live surface. */
package/dist/collab.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  keepPresenceAlive,
3
3
  useLiveDoc
4
- } from "./chunk-UKG4Z6QK.js";
4
+ } from "./chunk-Q342JSCO.js";
5
5
  import {
6
6
  PEER_PALETTE,
7
7
  peerColor,
package/dist/graph.cjs CHANGED
@@ -86,12 +86,15 @@ function keepPresenceAlive(awareness, intervalMs = TICK_MS) {
86
86
  }
87
87
 
88
88
  // src/collab/useLiveDoc.js
89
+ var REFUSED = "refused";
90
+ var DISCONNECT_GRACE_MS = 2e3;
89
91
  function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
90
92
  const [doc, setDoc] = (0, import_react.useState)(null);
91
93
  const [provider, setProvider] = (0, import_react.useState)(null);
92
94
  const [ready, setReady] = (0, import_react.useState)(false);
93
95
  const [states, setStates] = (0, import_react.useState)([]);
94
96
  const [selfClientId, setSelfClientId] = (0, import_react.useState)(null);
97
+ const [stopped, setStopped] = (0, import_react.useState)(false);
95
98
  const providerRef = (0, import_react.useRef)(null);
96
99
  const lastRef = (0, import_react.useRef)(null);
97
100
  (0, import_react.useEffect)(() => {
@@ -99,6 +102,7 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
99
102
  let dead = false;
100
103
  let current = null;
101
104
  let retry = null;
105
+ let grace = null;
102
106
  let stopKeepAlive = () => {
103
107
  };
104
108
  async function connect() {
@@ -109,21 +113,48 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
109
113
  ticket = null;
110
114
  }
111
115
  if (dead) return;
116
+ if (ticket && ticket.refused) {
117
+ setStopped(true);
118
+ return;
119
+ }
112
120
  if (!ticket) {
113
121
  retry = window.setTimeout(connect, 6e4);
114
122
  return;
115
123
  }
124
+ let first = ticket.token;
116
125
  current = new import_provider.HocuspocusProvider({
117
126
  url: ticket.url,
118
127
  name: ticket.doc,
119
- parameters: { cg_token: ticket.token },
120
- onAuthenticationFailed: () => {
128
+ token: async () => {
129
+ if (first) {
130
+ const t = first;
131
+ first = null;
132
+ return t;
133
+ }
134
+ let next = null;
135
+ try {
136
+ next = await fetchTicket(resourceId);
137
+ } catch {
138
+ next = null;
139
+ }
140
+ if (next && next.refused) throw new Error(REFUSED);
141
+ if (!next || !next.token) throw new Error("unavailable");
142
+ return next.token;
143
+ },
144
+ onAuthenticationFailed: ({ reason } = {}) => {
121
145
  stopKeepAlive();
122
146
  try {
123
147
  current.destroy();
124
148
  } catch {
125
149
  }
126
- if (!dead) retry = window.setTimeout(connect, 5e3);
150
+ if (dead) return;
151
+ window.clearTimeout(grace);
152
+ setReady(false);
153
+ if (String(reason || "").includes(REFUSED)) {
154
+ setStopped(true);
155
+ return;
156
+ }
157
+ retry = window.setTimeout(connect, 5e3);
127
158
  }
128
159
  });
129
160
  stopKeepAlive = keepPresenceAlive(current.awareness);
@@ -140,18 +171,28 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
140
171
  });
141
172
  current.on("synced", () => {
142
173
  if (dead) return;
174
+ window.clearTimeout(grace);
143
175
  setSelfClientId(current.awareness ? current.awareness.clientID : null);
144
176
  setDoc(current.document);
145
177
  setReady(true);
146
178
  });
147
179
  current.on("status", ({ status }) => {
148
- if (!dead && status === "disconnected") setReady(false);
180
+ if (dead) return;
181
+ if (status === "connected") window.clearTimeout(grace);
182
+ if (status === "disconnected") {
183
+ window.clearTimeout(grace);
184
+ grace = window.setTimeout(() => {
185
+ if (!dead) setReady(false);
186
+ }, DISCONNECT_GRACE_MS);
187
+ }
149
188
  });
150
189
  }
190
+ setStopped(false);
151
191
  connect();
152
192
  return () => {
153
193
  dead = true;
154
194
  window.clearTimeout(retry);
195
+ window.clearTimeout(grace);
155
196
  stopKeepAlive();
156
197
  if (current) {
157
198
  try {
@@ -177,7 +218,7 @@ function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
177
218
  }
178
219
  }
179
220
  }, [field]);
180
- return { doc, provider, ready, states, selfClientId, setAwareness };
221
+ return { doc, provider, ready, stopped, states, selfClientId, setAwareness };
181
222
  }
182
223
 
183
224
  // src/collab/colors.js
@@ -388,6 +429,8 @@ function useGraphCollab(graphId, user, { fetchTicket } = {}) {
388
429
  }, []);
389
430
  return {
390
431
  ready,
432
+ stopped: live.stopped,
433
+ // live editing was refused for this person: the host says so
391
434
  liveGraph,
392
435
  peers,
393
436
  copilotBuilding,