@nivaro/react 0.3.7 → 0.3.9

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,194 +1,204 @@
1
- import { bindClientChaosSocket as _ } from "./client-chaos.js";
2
- const I = 1, D = "nvr-realtime-worker";
3
- function q() {
1
+ import { bindClientChaosSocket as x } from "./client-chaos.js";
2
+ import { onSignedIn as z, signedOutBackoff as F, isSignedOut as U } from "./signed-out.js";
3
+ const j = 1, B = "nvr-realtime-worker";
4
+ function N() {
4
5
  return typeof window < "u" && typeof window.SharedWorker == "function";
5
6
  }
6
- function x() {
7
+ function Y() {
7
8
  try {
8
- const o = localStorage.getItem(D);
9
+ const o = localStorage.getItem(B);
9
10
  return o === "on" || o === "off" ? o : null;
10
11
  } catch {
11
12
  return null;
12
13
  }
13
14
  }
14
- function z(o) {
15
- if (!q() || x() === "off") return !1;
15
+ function $(o) {
16
+ if (!N() || Y() === "off") return !1;
16
17
  try {
17
18
  return new URL(o, window.location.href).origin === window.location.origin;
18
19
  } catch {
19
20
  return !1;
20
21
  }
21
22
  }
22
- function j(o) {
23
- const l = /* @__PURE__ */ new Map(), y = /* @__PURE__ */ new Set();
24
- let i = "pending", s = null, n = null, u = !1, h = null, C = 0;
25
- const v = /* @__PURE__ */ new Map(), W = [];
26
- let A = 0, k = null, p = null, m = !1, w = !1;
27
- const S = (e, t) => {
28
- for (const r of [...l.get(e) ?? []])
23
+ function J(o) {
24
+ const u = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Set();
25
+ let c = "pending", s = null, n = null, d = !1, T = null, K = 0;
26
+ const E = /* @__PURE__ */ new Map(), M = [];
27
+ let R = 0, k = null, p = null, m = !1, w = !1, b = 0, h = 0, v = null;
28
+ const A = (e, t) => {
29
+ for (const r of [...u.get(e) ?? []])
29
30
  try {
30
31
  r(...t);
31
32
  } catch {
32
33
  }
33
- for (const r of [...y])
34
+ for (const r of [...g])
34
35
  try {
35
36
  r(e, ...t);
36
37
  } catch {
37
38
  }
38
- }, M = () => {
39
+ }, C = () => {
39
40
  if (k && clearTimeout(k), k = null, p && clearInterval(p), p = null, s)
40
41
  try {
41
42
  s.postMessage({ t: "bye" }), s.close();
42
43
  } catch {
43
44
  }
44
45
  s = null;
45
- }, a = (e) => {
46
- var c, g, b;
47
- if (i === "fallback" || m) return;
48
- const t = u;
49
- M(), i = "fallback", u = !1;
46
+ }, l = (e) => {
47
+ var i, y, S;
48
+ if (c === "fallback" || m) return;
49
+ const t = d;
50
+ C(), c = "fallback", d = !1;
50
51
  const r = o.fallback();
51
- n = r, w && ((c = r.disconnect) == null || c.call(r));
52
- for (const [E, T] of l) for (const P of T) r.on(E, P);
53
- y.size && ((g = r.onAny) == null || g.call(r, (E, ...T) => O(E, T))), t && S("disconnect", ["worker-fallback"]), (b = o.onMode) == null || b.call(o, "fallback", e);
52
+ n = r, w && ((i = r.disconnect) == null || i.call(r));
53
+ for (const [O, W] of u) for (const q of W) r.on(O, q);
54
+ g.size && ((y = r.onAny) == null || y.call(r, (O, ...W) => I(O, W))), t && A("disconnect", ["worker-fallback"]), (S = o.onMode) == null || S.call(o, "fallback", e);
54
55
  };
55
- function O(e, t) {
56
- for (const r of [...y])
56
+ function I(e, t) {
57
+ for (const r of [...g])
57
58
  try {
58
59
  r(e, ...t);
59
60
  } catch {
60
61
  }
61
62
  }
62
- const R = () => d({
63
+ const D = () => a({
63
64
  t: "hello",
64
65
  app: o.app,
65
66
  features: o.features ?? [],
66
67
  version: o.version ?? {},
67
68
  active: typeof document > "u" || document.visibilityState === "visible"
68
- }), d = (e) => {
69
+ }), a = (e) => {
69
70
  try {
70
71
  s == null || s.postMessage(e);
71
72
  } catch {
72
- a("port-closed");
73
+ l("port-closed");
73
74
  }
74
- }, K = (e) => {
75
+ }, L = (e) => {
75
76
  var r;
76
77
  const t = e.data;
77
78
  if (!(!t || typeof t != "object" || m))
78
- switch (A = Date.now(), t.t) {
79
+ switch (R = Date.now(), t.t) {
79
80
  case "ready": {
80
- if (k && clearTimeout(k), k = null, t.proto !== I) return a(`proto-${String(t.proto)}`);
81
- i = "worker", w || R(), p = setInterval(() => {
82
- Date.now() - A > (o.silentMs ?? 2e4) && a("worker-silent");
81
+ if (k && clearTimeout(k), k = null, t.proto !== j) return l(`proto-${String(t.proto)}`);
82
+ c = "worker", w || D(), p = setInterval(() => {
83
+ Date.now() - R > (o.silentMs ?? 2e4) && l("worker-silent");
83
84
  }, 5e3), (r = o.onMode) == null || r.call(o, "worker", "ready");
84
85
  return;
85
86
  }
86
87
  case "ping":
87
- d({ t: "pong" });
88
+ a({ t: "pong" });
88
89
  return;
89
90
  case "status": {
90
- u = !!t.connected;
91
- const c = typeof t.transport == "string" ? t.transport : null, g = c === "websocket" && h !== "websocket";
92
- if (h = c, g) for (const b of W.splice(0)) b();
91
+ d = !!t.connected;
92
+ const i = typeof t.transport == "string" ? t.transport : null, y = i === "websocket" && T !== "websocket";
93
+ if (T = i, y) for (const S of M.splice(0)) S();
93
94
  return;
94
95
  }
95
96
  case "need-token":
96
- o.authToken().catch(() => null).then((c) => d({ t: "token", req: t.req, token: c }));
97
+ P().then(
98
+ (i) => a({ t: "token", req: t.req, token: i, ...i ? {} : { signedOut: U() } })
99
+ );
97
100
  return;
98
101
  case "ev": {
99
102
  if (w) return;
100
- t.event === "connect" && (u = !0), t.event === "disconnect" && (u = !1), S(String(t.event), Array.isArray(t.args) ? t.args : []);
103
+ t.event === "connect" && (d = !0), t.event === "disconnect" && (d = !1), A(String(t.event), Array.isArray(t.args) ? t.args : []);
101
104
  return;
102
105
  }
103
106
  case "ack": {
104
- const c = v.get(t.id);
105
- v.delete(t.id), c == null || c(...Array.isArray(t.args) ? t.args : []);
107
+ const i = E.get(t.id);
108
+ E.delete(t.id), i == null || i(...Array.isArray(t.args) ? t.args : []);
106
109
  return;
107
110
  }
108
111
  case "fallback":
109
- a(String(t.reason ?? "worker"));
112
+ l(String(t.reason ?? "worker"));
110
113
  return;
111
114
  default:
112
115
  return;
113
116
  }
114
- }, L = () => {
115
- if (!z(o.url)) return a("unsupported");
117
+ }, P = async () => {
118
+ if (Date.now() < h) return null;
119
+ const e = await o.authToken().catch(() => null);
120
+ return e ? (b = 0, h = 0, e) : (b += 1, h = Date.now() + F(b), null);
121
+ }, _ = () => {
122
+ if (!$(o.url)) return l("unsupported");
116
123
  try {
117
124
  const e = new SharedWorker(o.url, { name: o.name ?? o.app });
118
- s = e.port, s.onmessage = K, s.onmessageerror = () => a("message-error"), e.onerror = () => a("worker-error"), s.start(), k = setTimeout(() => a("no-greeting"), o.readyTimeoutMs ?? 15e3);
125
+ s = e.port, s.onmessage = L, s.onmessageerror = () => l("message-error"), e.onerror = () => l("worker-error"), s.start(), k = setTimeout(() => l("no-greeting"), o.readyTimeoutMs ?? 15e3);
119
126
  } catch {
120
- a("construct-failed");
127
+ l("construct-failed");
121
128
  }
122
129
  if (typeof window < "u") {
123
130
  const e = () => {
124
- i === "worker" && document.visibilityState === "visible" && d({ t: "active" });
131
+ c === "worker" && document.visibilityState === "visible" && a({ t: "active" });
125
132
  };
126
133
  document.addEventListener("visibilitychange", e), window.addEventListener("focus", e), window.addEventListener("pagehide", () => {
127
- i === "worker" && d({ t: "bye" });
134
+ c === "worker" && a({ t: "bye" });
128
135
  });
129
136
  }
130
137
  }, f = {
131
138
  get connected() {
132
- return w && i !== "fallback" ? !1 : i === "fallback" ? !!(n != null && n.connected) : u;
139
+ return w && c !== "fallback" ? !1 : c === "fallback" ? !!(n != null && n.connected) : d;
133
140
  },
134
141
  on(e, t) {
135
- const r = l.get(e) ?? /* @__PURE__ */ new Set();
136
- return l.set(e, r), r.add(t), i === "fallback" && (n == null || n.on(e, t)), f;
142
+ const r = u.get(e) ?? /* @__PURE__ */ new Set();
143
+ return u.set(e, r), r.add(t), c === "fallback" && (n == null || n.on(e, t)), f;
137
144
  },
138
145
  off(e, t) {
139
146
  var r;
140
- return t ? (r = l.get(e)) == null || r.delete(t) : l.delete(e), i === "fallback" && (n == null || n.off(e, t)), f;
147
+ return t ? (r = u.get(e)) == null || r.delete(t) : u.delete(e), c === "fallback" && (n == null || n.off(e, t)), f;
141
148
  },
142
149
  once(e, t) {
143
- const r = (...c) => {
144
- f.off(e, r), t(...c);
150
+ const r = (...i) => {
151
+ f.off(e, r), t(...i);
145
152
  };
146
153
  return f.on(e, r);
147
154
  },
148
155
  onAny(e) {
149
156
  var r;
150
- const t = y.size === 0;
151
- return y.add(e), i === "fallback" && t && ((r = n == null ? void 0 : n.onAny) == null || r.call(n, (c, ...g) => O(c, g))), f;
157
+ const t = g.size === 0;
158
+ return g.add(e), c === "fallback" && t && ((r = n == null ? void 0 : n.onAny) == null || r.call(n, (i, ...y) => I(i, y))), f;
152
159
  },
153
160
  offAny(e) {
154
- return y.delete(e), f;
161
+ return g.delete(e), f;
155
162
  },
156
163
  listeners(e) {
157
- return [...l.get(e) ?? []];
164
+ return [...u.get(e) ?? []];
158
165
  },
159
166
  emit(e, ...t) {
160
- if (i === "fallback") return n == null ? void 0 : n.emit(e, ...t);
167
+ if (c === "fallback") return n == null ? void 0 : n.emit(e, ...t);
161
168
  const r = t[t.length - 1];
162
169
  if (w)
163
170
  return typeof r == "function" && setTimeout(() => r(), 0), f;
164
171
  if (typeof r == "function") {
165
- const c = ++C;
166
- v.set(c, r), d({ t: "emit", event: e, payload: t[0], ack: c });
167
- } else d({ t: "emit", event: e, payload: t[0] });
172
+ const i = ++K;
173
+ E.set(i, r), a({ t: "emit", event: e, payload: t[0], ack: i });
174
+ } else a({ t: "emit", event: e, payload: t[0] });
168
175
  return f;
169
176
  },
170
177
  connect() {
171
178
  var e;
172
- i === "fallback" ? (e = n == null ? void 0 : n.connect) == null || e.call(n) : d({ t: "connect" });
179
+ c === "fallback" ? (e = n == null ? void 0 : n.connect) == null || e.call(n) : a({ t: "connect" });
173
180
  },
174
181
  disconnect() {
175
182
  var e;
176
- m = !0, M(), (e = n == null ? void 0 : n.disconnect) == null || e.call(n);
183
+ m = !0, v == null || v(), C(), (e = n == null ? void 0 : n.disconnect) == null || e.call(n);
177
184
  },
178
- mode: () => i,
185
+ signedIn() {
186
+ b = 0, h = 0, !(c !== "worker" || m) && (a({ t: "signed-in" }), (typeof document > "u" || document.visibilityState === "visible") && a({ t: "active" }));
187
+ },
188
+ mode: () => c,
179
189
  chaosPause(e) {
180
190
  var t, r;
181
191
  if (!(e === w || m)) {
182
- if (w = e, i === "fallback") {
192
+ if (w = e, c === "fallback") {
183
193
  e ? (t = n == null ? void 0 : n.disconnect) == null || t.call(n) : (r = n == null ? void 0 : n.connect) == null || r.call(n);
184
194
  return;
185
195
  }
186
196
  if (e) {
187
- const c = u;
188
- u = !1, c && S("disconnect", ["transport close"]);
197
+ const i = d;
198
+ d = !1, i && A("disconnect", ["transport close"]);
189
199
  return;
190
200
  }
191
- i === "worker" && R(), typeof window < "u" && window.dispatchEvent(new CustomEvent("nvr:catchup-full"));
201
+ c === "worker" && D(), typeof window < "u" && window.dispatchEvent(new CustomEvent("nvr:catchup-full"));
192
202
  }
193
203
  },
194
204
  io: {
@@ -197,23 +207,23 @@ function j(o) {
197
207
  return {
198
208
  get name() {
199
209
  var e, t, r;
200
- return i === "fallback" ? (r = (t = (e = n == null ? void 0 : n.io) == null ? void 0 : e.engine) == null ? void 0 : t.transport) == null ? void 0 : r.name : h ?? void 0;
210
+ return c === "fallback" ? (r = (t = (e = n == null ? void 0 : n.io) == null ? void 0 : e.engine) == null ? void 0 : t.transport) == null ? void 0 : r.name : T ?? void 0;
201
211
  }
202
212
  };
203
213
  },
204
214
  once(e, t) {
205
- e === "upgrade" && W.push(t);
215
+ e === "upgrade" && M.push(t);
206
216
  }
207
217
  }
208
218
  }
209
219
  };
210
- return L(), _(f), f;
220
+ return v = z(() => f.signedIn()), _(), x(f), f;
211
221
  }
212
222
  export {
213
- D as WORKER_SOCKET_PREF_KEY,
214
- I as WORKER_SOCKET_PROTO,
215
- j as createWorkerSocket,
216
- q as sharedWorkerSupported,
217
- z as shouldUseWorkerSocket,
218
- x as workerSocketPreference
223
+ B as WORKER_SOCKET_PREF_KEY,
224
+ j as WORKER_SOCKET_PROTO,
225
+ J as createWorkerSocket,
226
+ N as sharedWorkerSupported,
227
+ $ as shouldUseWorkerSocket,
228
+ Y as workerSocketPreference
219
229
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nivaro/react",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "description": "React hooks and components for Nivaro CMS forms",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",