@sweidos/eidos 1.0.24 → 1.0.30
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 +150 -11
- package/dist/action.js +98 -74
- package/dist/action.js.map +1 -1
- package/dist/devtools.d.ts +2 -0
- package/dist/devtools.js +597 -0
- package/dist/eidos.cjs.js +2 -2
- package/dist/eidos.cjs.js.map +1 -1
- package/dist/index.d.ts +64 -7
- package/dist/index.js +29 -27
- package/dist/nextjs.d.ts +2 -0
- package/dist/nextjs.js +12 -0
- package/dist/react/Devtools.d.ts +7 -0
- package/dist/react/hooks.js +41 -32
- package/dist/react/hooks.js.map +1 -1
- package/dist/resource.js +65 -55
- package/dist/resource.js.map +1 -1
- package/dist/runtime.js +12 -12
- package/dist/runtime.js.map +1 -1
- package/dist/store.js +35 -26
- package/dist/store.js.map +1 -1
- package/dist/stores.js +34 -21
- package/dist/stores.js.map +1 -1
- package/dist/sveltekit.d.ts +19 -0
- package/dist/sveltekit.js +9 -0
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +14 -2
package/dist/index.js
CHANGED
|
@@ -1,37 +1,39 @@
|
|
|
1
|
-
import { resource as s, setQueryInvalidator as r } from "./resource.js";
|
|
2
|
-
import { action as t, clearQueue as
|
|
3
|
-
import { _resetEidos as
|
|
4
|
-
import { EidosProvider as
|
|
5
|
-
import { useEidos as S, useEidosAction as x, useEidosOnDrain as
|
|
6
|
-
import { VERSION as
|
|
7
|
-
import { isBgSyncSupported as
|
|
8
|
-
import { useEidosStore as
|
|
9
|
-
import { eidosAction as
|
|
1
|
+
import { resource as s, setQueryInvalidator as r, warmCache as i } from "./resource.js";
|
|
2
|
+
import { action as t, clearQueue as d, replayQueue as a } from "./action.js";
|
|
3
|
+
import { _resetEidos as p, initEidos as f } from "./runtime.js";
|
|
4
|
+
import { EidosProvider as c } from "./react/Provider.js";
|
|
5
|
+
import { useEidos as S, useEidosAction as x, useEidosOnDrain as Q, useEidosQueue as l, useEidosQueueStats as R, useEidosResource as y, useEidosResources as O, useEidosStatus as v } from "./react/hooks.js";
|
|
6
|
+
import { VERSION as I } from "./version.js";
|
|
7
|
+
import { isBgSyncSupported as h, setOfflineSimulation as w } from "./sw-bridge.js";
|
|
8
|
+
import { useEidosStore as C } from "./store.js";
|
|
9
|
+
import { eidosAction as N, eidosQueue as P, eidosQueueStats as V, eidosResource as _, eidosStatus as b, eidosStore as j } from "./stores.js";
|
|
10
10
|
export {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
c as EidosProvider,
|
|
12
|
+
I as VERSION,
|
|
13
|
+
p as _resetEidos,
|
|
14
14
|
t as action,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
d as clearQueue,
|
|
16
|
+
N as eidosAction,
|
|
17
|
+
P as eidosQueue,
|
|
18
|
+
V as eidosQueueStats,
|
|
19
|
+
_ as eidosResource,
|
|
20
|
+
b as eidosStatus,
|
|
21
21
|
j as eidosStore,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
f as initEidos,
|
|
23
|
+
h as isBgSyncSupported,
|
|
24
|
+
a as replayQueue,
|
|
25
25
|
s as resource,
|
|
26
|
-
|
|
26
|
+
w as setOfflineSimulation,
|
|
27
27
|
r as setQueryInvalidator,
|
|
28
28
|
S as useEidos,
|
|
29
29
|
x as useEidosAction,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
Q as useEidosOnDrain,
|
|
31
|
+
l as useEidosQueue,
|
|
32
|
+
R as useEidosQueueStats,
|
|
33
33
|
y as useEidosResource,
|
|
34
|
-
O as
|
|
35
|
-
|
|
34
|
+
O as useEidosResources,
|
|
35
|
+
v as useEidosStatus,
|
|
36
|
+
C as useEidosStore,
|
|
37
|
+
i as warmCache
|
|
36
38
|
};
|
|
37
39
|
//# sourceMappingURL=index.js.map
|
package/dist/nextjs.d.ts
ADDED
package/dist/nextjs.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { EidosProvider, useEidos, useEidosAction, useEidosOnDrain, useEidosQueue, useEidosQueueStats, useEidosResource, useEidosStatus } from "@sweidos/eidos";
|
|
3
|
+
export {
|
|
4
|
+
EidosProvider,
|
|
5
|
+
useEidos,
|
|
6
|
+
useEidosAction,
|
|
7
|
+
useEidosOnDrain,
|
|
8
|
+
useEidosQueue,
|
|
9
|
+
useEidosQueueStats,
|
|
10
|
+
useEidosResource,
|
|
11
|
+
useEidosStatus
|
|
12
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface EidosDevtoolsProps {
|
|
2
|
+
/** Corner to anchor the panel. Default: 'bottom-right'. */
|
|
3
|
+
position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
|
|
4
|
+
/** Start expanded. Default: false. */
|
|
5
|
+
defaultOpen?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function EidosDevtools({ position, defaultOpen }: EidosDevtoolsProps): import("react").JSX.Element;
|
package/dist/react/hooks.js
CHANGED
|
@@ -1,42 +1,51 @@
|
|
|
1
|
-
import { useRef as
|
|
2
|
-
import { useEidosStore as
|
|
3
|
-
function
|
|
4
|
-
const t = e ?? ((
|
|
5
|
-
return l
|
|
6
|
-
}
|
|
7
|
-
function E() {
|
|
8
|
-
return n();
|
|
9
|
-
}
|
|
10
|
-
function g(e) {
|
|
11
|
-
return n((t) => t.resources[e]);
|
|
12
|
-
}
|
|
13
|
-
function S() {
|
|
14
|
-
return n((e) => e.queue);
|
|
15
|
-
}
|
|
16
|
-
function p(e) {
|
|
17
|
-
return n((t) => t.queue.find((u) => u.id === e));
|
|
1
|
+
import { useRef as a, useEffect as E, useSyncExternalStore as p } from "react";
|
|
2
|
+
import { useEidosStore as l } from "../store.js";
|
|
3
|
+
function s(e) {
|
|
4
|
+
const t = e ?? ((n) => n);
|
|
5
|
+
return p(l.subscribe, () => t(l.getState()));
|
|
18
6
|
}
|
|
19
7
|
function q() {
|
|
20
|
-
|
|
21
|
-
return { isOnline: e, swStatus: t, swError: u };
|
|
8
|
+
return s();
|
|
22
9
|
}
|
|
23
|
-
function
|
|
24
|
-
|
|
25
|
-
return { pending: e, failed: t, replaying: u, total: s };
|
|
10
|
+
function R() {
|
|
11
|
+
return s((e) => e.resources);
|
|
26
12
|
}
|
|
27
13
|
function m(e) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
14
|
+
return s((t) => t.resources[e]);
|
|
15
|
+
}
|
|
16
|
+
function w() {
|
|
17
|
+
return s((e) => e.queue);
|
|
18
|
+
}
|
|
19
|
+
function y(e) {
|
|
20
|
+
return s((t) => t.queue.find((n) => n.id === e));
|
|
21
|
+
}
|
|
22
|
+
function $() {
|
|
23
|
+
const e = s((u) => u.isOnline), t = s((u) => u.swStatus), n = s((u) => u.swError);
|
|
24
|
+
return { isOnline: e, swStatus: t, swError: n };
|
|
25
|
+
}
|
|
26
|
+
function O() {
|
|
27
|
+
const e = s((i) => {
|
|
28
|
+
let c = 0, f = 0, d = 0;
|
|
29
|
+
for (const o of i.queue)
|
|
30
|
+
o.status === "pending" ? c++ : o.status === "failed" ? f++ : o.status === "replaying" && d++;
|
|
31
|
+
return `${c},${f},${d},${i.queue.length}`;
|
|
32
|
+
}), [t, n, u, r] = e.split(",");
|
|
33
|
+
return { pending: +t, failed: +n, replaying: +u, total: +r };
|
|
34
|
+
}
|
|
35
|
+
function b(e) {
|
|
36
|
+
const t = s((r) => r.queue.length), n = a(0), u = a(e);
|
|
37
|
+
u.current = e, E(() => {
|
|
38
|
+
n.current > 0 && t === 0 && u.current(), n.current = t;
|
|
31
39
|
}, [t]);
|
|
32
40
|
}
|
|
33
41
|
export {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
q as useEidos,
|
|
43
|
+
y as useEidosAction,
|
|
44
|
+
b as useEidosOnDrain,
|
|
45
|
+
w as useEidosQueue,
|
|
46
|
+
O as useEidosQueueStats,
|
|
47
|
+
m as useEidosResource,
|
|
48
|
+
R as useEidosResources,
|
|
49
|
+
$ as useEidosStatus
|
|
41
50
|
};
|
|
42
51
|
//# sourceMappingURL=hooks.js.map
|
package/dist/react/hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.js","sources":["../../src/react/hooks.ts"],"sourcesContent":["import { useEffect, useRef, useSyncExternalStore } from 'react'\nimport { useEidosStore } from '../store'\nimport type { EidosStore } from '../store'\n\nfunction useStore(): EidosStore\nfunction useStore<T>(selector: (state: EidosStore) => T): T\nfunction useStore<T = EidosStore>(selector?: (state: EidosStore) => T): T {\n const fn = selector ?? ((s: EidosStore) => s as unknown as T)\n return useSyncExternalStore(useEidosStore.subscribe, () => fn(useEidosStore.getState()))\n}\n\n/** Full Eidos store — prefer the narrower hooks below for performance. */\nexport function useEidos() {\n return useStore()\n}\n\n/** Live state for a single registered resource URL. */\nexport function useEidosResource(url: string) {\n return useStore((s) => s.resources[url])\n}\n\n/** The current action queue. */\nexport function useEidosQueue() {\n return useStore((s) => s.queue)\n}\n\n/**\n * Live state for a single queue item by ID. Only re-renders when that specific\n * item changes — cheaper than `useEidosQueue().find(id)` which re-renders on\n * any queue mutation.\n */\nexport function useEidosAction(id: string) {\n return useStore((s) => s.queue.find((item) => item.id === id))\n}\n\n/**\n * Online + SW status — cheap subscription, safe to use in header components.\n * Three separate primitive selectors so each only triggers a re-render when\n * its own value changes (no object-reference churn from a combined selector).\n */\nexport function useEidosStatus() {\n const isOnline = useStore((s) => s.isOnline)\n const swStatus = useStore((s) => s.swStatus)\n const swError = useStore((s) => s.swError)\n return { isOnline, swStatus, swError }\n}\n\n/**\n * Queue counts —
|
|
1
|
+
{"version":3,"file":"hooks.js","sources":["../../src/react/hooks.ts"],"sourcesContent":["import { useEffect, useRef, useSyncExternalStore } from 'react'\nimport { useEidosStore } from '../store'\nimport type { EidosStore } from '../store'\n\nfunction useStore(): EidosStore\nfunction useStore<T>(selector: (state: EidosStore) => T): T\nfunction useStore<T = EidosStore>(selector?: (state: EidosStore) => T): T {\n const fn = selector ?? ((s: EidosStore) => s as unknown as T)\n return useSyncExternalStore(useEidosStore.subscribe, () => fn(useEidosStore.getState()))\n}\n\n/** Full Eidos store — prefer the narrower hooks below for performance. */\nexport function useEidos() {\n return useStore()\n}\n\n/** All registered resources — only re-renders when the resources map changes, not on queue mutations. */\nexport function useEidosResources() {\n return useStore((s) => s.resources)\n}\n\n/** Live state for a single registered resource URL. */\nexport function useEidosResource(url: string) {\n return useStore((s) => s.resources[url])\n}\n\n/** The current action queue. */\nexport function useEidosQueue() {\n return useStore((s) => s.queue)\n}\n\n/**\n * Live state for a single queue item by ID. Only re-renders when that specific\n * item changes — cheaper than `useEidosQueue().find(id)` which re-renders on\n * any queue mutation.\n */\nexport function useEidosAction(id: string) {\n return useStore((s) => s.queue.find((item) => item.id === id))\n}\n\n/**\n * Online + SW status — cheap subscription, safe to use in header components.\n * Three separate primitive selectors so each only triggers a re-render when\n * its own value changes (no object-reference churn from a combined selector).\n */\nexport function useEidosStatus() {\n const isOnline = useStore((s) => s.isOnline)\n const swStatus = useStore((s) => s.swStatus)\n const swError = useStore((s) => s.swError)\n return { isOnline, swStatus, swError }\n}\n\n/**\n * Queue counts — single subscription, single loop. Re-renders only when a\n * count changes, not on every queue mutation. Use for badges and status bars\n * instead of `useEidosQueue()` when you only need numbers, not full items.\n */\nexport function useEidosQueueStats() {\n // Encode as a comma-separated string so useSyncExternalStore's Object.is\n // comparison bails out correctly when counts haven't changed. One loop,\n // one subscription — cheaper than four separate filter() passes.\n const encoded = useStore((s) => {\n let pending = 0, failed = 0, replaying = 0\n for (const q of s.queue) {\n if (q.status === 'pending') pending++\n else if (q.status === 'failed') failed++\n else if (q.status === 'replaying') replaying++\n }\n return `${pending},${failed},${replaying},${s.queue.length}`\n })\n const [p, f, r, t] = encoded.split(',')\n return { pending: +p, failed: +f, replaying: +r, total: +t }\n}\n\n/**\n * Calls `callback` once each time the action queue drains from non-empty → 0.\n * Stable callback reference not required — always calls the latest version.\n * Use for \"all offline actions synced!\" toasts.\n *\n * @example\n * useEidosOnDrain(() => toast.success('All offline actions synced!'))\n */\nexport function useEidosOnDrain(callback: () => void) {\n const total = useStore((s) => s.queue.length)\n const prevRef = useRef(0)\n const callbackRef = useRef(callback)\n callbackRef.current = callback\n\n useEffect(() => {\n if (prevRef.current > 0 && total === 0) {\n callbackRef.current()\n }\n prevRef.current = total\n }, [total])\n}\n"],"names":["useStore","selector","fn","s","useSyncExternalStore","useEidosStore","useEidos","useEidosResources","useEidosResource","url","useEidosQueue","useEidosAction","id","item","useEidosStatus","isOnline","swStatus","swError","useEidosQueueStats","encoded","pending","failed","replaying","q","p","f","r","t","useEidosOnDrain","callback","total","prevRef","useRef","callbackRef","useEffect"],"mappings":";;AAMA,SAASA,EAAyBC,GAAwC;AACxE,QAAMC,IAAKD,MAAa,CAACE,MAAkBA;AAC3C,SAAOC,EAAqBC,EAAc,WAAW,MAAMH,EAAGG,EAAc,SAAA,CAAU,CAAC;AACzF;AAGO,SAASC,IAAW;AACzB,SAAON,EAAA;AACT;AAGO,SAASO,IAAoB;AAClC,SAAOP,EAAS,CAACG,MAAMA,EAAE,SAAS;AACpC;AAGO,SAASK,EAAiBC,GAAa;AAC5C,SAAOT,EAAS,CAACG,MAAMA,EAAE,UAAUM,CAAG,CAAC;AACzC;AAGO,SAASC,IAAgB;AAC9B,SAAOV,EAAS,CAACG,MAAMA,EAAE,KAAK;AAChC;AAOO,SAASQ,EAAeC,GAAY;AACzC,SAAOZ,EAAS,CAACG,MAAMA,EAAE,MAAM,KAAK,CAACU,MAASA,EAAK,OAAOD,CAAE,CAAC;AAC/D;AAOO,SAASE,IAAiB;AAC/B,QAAMC,IAAWf,EAAS,CAACG,MAAMA,EAAE,QAAQ,GACrCa,IAAWhB,EAAS,CAACG,MAAMA,EAAE,QAAQ,GACrCc,IAAUjB,EAAS,CAACG,MAAMA,EAAE,OAAO;AACzC,SAAO,EAAE,UAAAY,GAAU,UAAAC,GAAU,SAAAC,EAAA;AAC/B;AAOO,SAASC,IAAqB;AAInC,QAAMC,IAAUnB,EAAS,CAACG,MAAM;AAC9B,QAAIiB,IAAU,GAAGC,IAAS,GAAGC,IAAY;AACzC,eAAWC,KAAKpB,EAAE;AAChB,MAAIoB,EAAE,WAAW,YAAWH,MACnBG,EAAE,WAAW,WAAUF,MACvBE,EAAE,WAAW,eAAaD;AAErC,WAAO,GAAGF,CAAO,IAAIC,CAAM,IAAIC,CAAS,IAAInB,EAAE,MAAM,MAAM;AAAA,EAC5D,CAAC,GACK,CAACqB,GAAGC,GAAGC,GAAGC,CAAC,IAAIR,EAAQ,MAAM,GAAG;AACtC,SAAO,EAAE,SAAS,CAACK,GAAG,QAAQ,CAACC,GAAG,WAAW,CAACC,GAAG,OAAO,CAACC,EAAA;AAC3D;AAUO,SAASC,EAAgBC,GAAsB;AACpD,QAAMC,IAAW9B,EAAS,CAACG,MAAMA,EAAE,MAAM,MAAM,GACzC4B,IAAWC,EAAO,CAAC,GACnBC,IAAcD,EAAOH,CAAQ;AACnC,EAAAI,EAAY,UAAUJ,GAEtBK,EAAU,MAAM;AACd,IAAIH,EAAQ,UAAU,KAAKD,MAAU,KACnCG,EAAY,QAAA,GAEdF,EAAQ,UAAUD;AAAA,EACpB,GAAG,CAACA,CAAK,CAAC;AACZ;"}
|
package/dist/resource.js
CHANGED
|
@@ -5,24 +5,24 @@ let m = null;
|
|
|
5
5
|
function N(e) {
|
|
6
6
|
m = e;
|
|
7
7
|
}
|
|
8
|
-
function
|
|
8
|
+
function l(e) {
|
|
9
9
|
return e.includes("*") || /:[^/]+/.test(e);
|
|
10
10
|
}
|
|
11
11
|
function k(e) {
|
|
12
12
|
return "^" + e.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, ".+").replace(/\*/g, "[^/]+").replace(/:[^/]+/g, "[^/]+") + "$";
|
|
13
13
|
}
|
|
14
|
-
function w(e,
|
|
14
|
+
function w(e, a) {
|
|
15
15
|
return new Error(
|
|
16
|
-
`[eidos] resource('${e}') is a URL pattern — ${
|
|
16
|
+
`[eidos] resource('${e}') is a URL pattern — ${a}() is not supported on pattern handles. The SW intercepts matching requests automatically; call fetch(specificUrl) directly in your app code.`
|
|
17
17
|
);
|
|
18
18
|
}
|
|
19
|
-
function q(e,
|
|
19
|
+
function q(e, a) {
|
|
20
20
|
if (p.has(e))
|
|
21
21
|
return p.get(e);
|
|
22
|
-
const
|
|
22
|
+
const s = E(e, a), t = l(e) ? k(e) : void 0, o = {
|
|
23
23
|
url: e,
|
|
24
|
-
config:
|
|
25
|
-
strategy:
|
|
24
|
+
config: a,
|
|
25
|
+
strategy: s,
|
|
26
26
|
status: "idle",
|
|
27
27
|
cacheHits: 0,
|
|
28
28
|
cacheMisses: 0
|
|
@@ -30,49 +30,50 @@ function q(e, t) {
|
|
|
30
30
|
h.getState().registerResource(e, o), g({
|
|
31
31
|
type: "EIDOS_REGISTER_RESOURCE",
|
|
32
32
|
url: e,
|
|
33
|
-
strategy:
|
|
34
|
-
cacheName:
|
|
35
|
-
...
|
|
33
|
+
strategy: s.swStrategy,
|
|
34
|
+
cacheName: s.cacheName,
|
|
35
|
+
...t !== void 0 && { pattern: t }
|
|
36
36
|
});
|
|
37
|
-
const
|
|
37
|
+
const c = {
|
|
38
38
|
url: e,
|
|
39
|
-
config:
|
|
40
|
-
strategy:
|
|
39
|
+
config: a,
|
|
40
|
+
strategy: s,
|
|
41
41
|
fetch: async () => {
|
|
42
|
-
if (
|
|
42
|
+
if (l(e)) throw w(e, "fetch");
|
|
43
43
|
const r = y.get(e);
|
|
44
|
-
if (r) return r.then((
|
|
45
|
-
const i =
|
|
46
|
-
return y.set(e, i), i.finally(() => y.delete(e))
|
|
44
|
+
if (r) return r.then((n) => n.clone());
|
|
45
|
+
const i = S(e, a, s);
|
|
46
|
+
return y.set(e, i), i.finally(() => y.delete(e)).catch(() => {
|
|
47
|
+
}), i.then((n) => n.clone());
|
|
47
48
|
},
|
|
48
49
|
json: async () => {
|
|
49
|
-
if (
|
|
50
|
-
return (await
|
|
50
|
+
if (l(e)) throw w(e, "json");
|
|
51
|
+
return (await c.fetch()).json();
|
|
51
52
|
},
|
|
52
53
|
query: () => {
|
|
53
|
-
if (
|
|
54
|
+
if (l(e)) throw w(e, "query");
|
|
54
55
|
return {
|
|
55
56
|
queryKey: ["eidos", e],
|
|
56
|
-
queryFn: () =>
|
|
57
|
+
queryFn: () => c.json()
|
|
57
58
|
};
|
|
58
59
|
},
|
|
59
60
|
prefetch: async () => {
|
|
60
|
-
if (
|
|
61
|
-
await
|
|
61
|
+
if (l(e)) throw w(e, "prefetch");
|
|
62
|
+
await c.fetch();
|
|
62
63
|
},
|
|
63
64
|
invalidate: async () => {
|
|
64
65
|
g({ type: "EIDOS_CLEAR_CACHE", url: e });
|
|
65
|
-
const r = await caches.open(
|
|
66
|
+
const r = await caches.open(s.cacheName).catch(() => null);
|
|
66
67
|
if (r) {
|
|
67
|
-
const i = await r.keys(),
|
|
68
|
+
const i = await r.keys(), n = t ? new RegExp(t) : null, u = e.startsWith("http");
|
|
68
69
|
await Promise.all(
|
|
69
70
|
i.filter((f) => {
|
|
70
71
|
const d = f.url, v = new URL(d).pathname;
|
|
71
|
-
return
|
|
72
|
+
return n ? n.test(u ? d : v) : u ? d === e : d === e || v === e;
|
|
72
73
|
}).map((f) => r.delete(f))
|
|
73
74
|
);
|
|
74
75
|
}
|
|
75
|
-
|
|
76
|
+
l(e) || h.getState().updateResource(e, {
|
|
76
77
|
status: "stale",
|
|
77
78
|
cachedAt: void 0,
|
|
78
79
|
lastEvent: "cache-cleared",
|
|
@@ -84,21 +85,21 @@ function q(e, t) {
|
|
|
84
85
|
p.delete(e), g({ type: "EIDOS_UNREGISTER_RESOURCE", url: e }), h.getState().unregisterResource(e);
|
|
85
86
|
}
|
|
86
87
|
};
|
|
87
|
-
return p.set(e,
|
|
88
|
+
return p.set(e, c), c;
|
|
88
89
|
}
|
|
89
|
-
async function
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
const o = await caches.open(
|
|
90
|
+
async function S(e, a, s) {
|
|
91
|
+
const t = h.getState();
|
|
92
|
+
t.updateResource(e, { status: "fetching", fetchedAt: Date.now() });
|
|
93
|
+
const o = await caches.open(s.cacheName).catch(() => null);
|
|
93
94
|
try {
|
|
94
|
-
if (
|
|
95
|
-
const i = o ? await o.match(e).catch(() => null) : null,
|
|
96
|
-
if (i && !
|
|
97
|
-
return
|
|
95
|
+
if (s.swStrategy !== "network-first") {
|
|
96
|
+
const i = o ? await o.match(e).catch(() => null) : null, n = h.getState().resources[e], u = a.maxAge !== void 0 && (n == null ? void 0 : n.cachedAt) !== void 0 && Date.now() - n.cachedAt > a.maxAge;
|
|
97
|
+
if (i && !u)
|
|
98
|
+
return t.updateResource(e, {
|
|
98
99
|
status: "fresh",
|
|
99
100
|
lastEvent: "cache-hit",
|
|
100
|
-
cacheHits: ((
|
|
101
|
-
}),
|
|
101
|
+
cacheHits: ((n == null ? void 0 : n.cacheHits) ?? 0) + 1
|
|
102
|
+
}), s.swStrategy === "stale-while-revalidate" && fetch(e, { signal: AbortSignal.timeout(5e3) }).then(async (d) => {
|
|
102
103
|
d.ok && o && (await o.put(e, d.clone()), h.getState().updateResource(e, {
|
|
103
104
|
cachedAt: Date.now(),
|
|
104
105
|
lastEvent: "cache-updated"
|
|
@@ -106,38 +107,38 @@ async function E(e, t, a) {
|
|
|
106
107
|
}).catch(() => {
|
|
107
108
|
}), i;
|
|
108
109
|
const f = h.getState().resources[e];
|
|
109
|
-
|
|
110
|
+
t.updateResource(e, {
|
|
110
111
|
cacheMisses: ((f == null ? void 0 : f.cacheMisses) ?? 0) + 1
|
|
111
112
|
});
|
|
112
113
|
}
|
|
113
|
-
const
|
|
114
|
-
if (
|
|
115
|
-
return o && await o.put(e,
|
|
114
|
+
const c = await fetch(e);
|
|
115
|
+
if (c.ok)
|
|
116
|
+
return o && await o.put(e, c.clone()), t.updateResource(e, {
|
|
116
117
|
status: "fresh",
|
|
117
118
|
cachedAt: Date.now(),
|
|
118
119
|
lastEvent: "cache-updated"
|
|
119
|
-
}),
|
|
120
|
-
|
|
121
|
-
const r =
|
|
120
|
+
}), c;
|
|
121
|
+
t.updateResource(e, { status: c.status === 503 ? "offline" : "error" });
|
|
122
|
+
const r = c.headers.get("X-Eidos-Offline") === "true";
|
|
122
123
|
throw new Error(
|
|
123
|
-
r ? `offline: no cached response for ${e}` : `${
|
|
124
|
+
r ? `offline: no cached response for ${e}` : `${c.status} ${c.statusText}`
|
|
124
125
|
);
|
|
125
|
-
} catch (
|
|
126
|
+
} catch (c) {
|
|
126
127
|
const r = o ? await o.match(e).catch(() => null) : null;
|
|
127
128
|
if (r) {
|
|
128
129
|
const i = h.getState().resources[e];
|
|
129
|
-
return
|
|
130
|
+
return t.updateResource(e, {
|
|
130
131
|
status: "fresh",
|
|
131
132
|
lastEvent: "cache-hit",
|
|
132
133
|
cacheHits: ((i == null ? void 0 : i.cacheHits) ?? 0) + 1
|
|
133
134
|
}), r;
|
|
134
135
|
}
|
|
135
|
-
throw
|
|
136
|
+
throw t.updateResource(e, { status: "error" }), c;
|
|
136
137
|
}
|
|
137
138
|
}
|
|
138
|
-
function
|
|
139
|
-
const
|
|
140
|
-
return
|
|
139
|
+
function E(e, a) {
|
|
140
|
+
const s = a.strategy;
|
|
141
|
+
return a.offline ? R(s ?? "stale-while-revalidate", e, a.cacheName) : R(s ?? "network-first", e, a.cacheName);
|
|
141
142
|
}
|
|
142
143
|
const x = {
|
|
143
144
|
"stale-while-revalidate": {
|
|
@@ -186,15 +187,24 @@ new NetworkFirst({
|
|
|
186
187
|
})`
|
|
187
188
|
}
|
|
188
189
|
};
|
|
189
|
-
function R(e,
|
|
190
|
+
function R(e, a, s) {
|
|
190
191
|
return {
|
|
191
192
|
...x[e],
|
|
192
193
|
swStrategy: e,
|
|
193
|
-
cacheName:
|
|
194
|
+
cacheName: s ?? "eidos-resources-v1"
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
async function A(e) {
|
|
198
|
+
const a = await Promise.allSettled(e.map((t) => t.prefetch())), s = a.filter((t) => t.status === "rejected").map((t) => t.reason);
|
|
199
|
+
return {
|
|
200
|
+
warmed: a.filter((t) => t.status === "fulfilled").length,
|
|
201
|
+
failed: s.length,
|
|
202
|
+
errors: s
|
|
194
203
|
};
|
|
195
204
|
}
|
|
196
205
|
export {
|
|
197
206
|
q as resource,
|
|
198
|
-
N as setQueryInvalidator
|
|
207
|
+
N as setQueryInvalidator,
|
|
208
|
+
A as warmCache
|
|
199
209
|
};
|
|
200
210
|
//# sourceMappingURL=resource.js.map
|
package/dist/resource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resource.js","sources":["../src/resource.ts"],"sourcesContent":["import { useEidosStore } from './store'\nimport { sendToWorker } from './sw-bridge'\nimport type {\n ResourceConfig,\n ResourceHandle,\n ResourceEntry,\n GeneratedStrategy,\n CacheStrategy,\n} from './types'\n\nconst _registry = new Map<string, ResourceHandle>()\n\n// ── Request deduplication ─────────────────────────────────────────────────────\n// If multiple callers invoke handle.fetch() simultaneously for the same URL,\n// only one network request is made. Each caller gets its own cloned Response.\n// Keyed by URL; entry is deleted when the request settles.\nconst _inflightRequests = /* @__PURE__ */ new Map<string, Promise<Response>>()\n\n// ── TanStack Query bridge (optional) ─────────────────────────────────────────\n// Set by @sweidos/eidos/query when withEidosQueryClient() is called.\n// Lets handle.invalidate() also invalidate the matching TQ cache entry.\ntype QueryInvalidator = (queryKey: [string, string]) => void\nlet _queryInvalidator: QueryInvalidator | null = null\n\n/** @internal Called by @sweidos/eidos/query. */\nexport function setQueryInvalidator(fn: QueryInvalidator): void {\n _queryInvalidator = fn\n}\n\n// ── URL pattern helpers ───────────────────────────────────────────────────────\n\n/** Returns true if `url` contains wildcard or :param segments. */\nfunction isPattern(url: string): boolean {\n return url.includes('*') || /:[^/]+/.test(url)\n}\n\n/**\n * Converts a URL pattern to a regex source string for SW fetch matching.\n * `**` → multi-segment wildcard (`.+`)\n * `*` → single-segment wildcard (`[^/]+`)\n * `:param` → named single segment (`[^/]+`)\n *\n * Special regex characters in the pattern (e.g. `.`) are escaped first so\n * they match literally.\n *\n * @example\n * patternToRegexStr('/api/products/*') // '^/api/products/[^/]+$'\n * patternToRegexStr('/api/products/**') // '^/api/products/.+$'\n * patternToRegexStr('/api/users/:id') // '^/api/users/[^/]+$'\n */\nfunction patternToRegexStr(pattern: string): string {\n // Escape all regex-special chars except `*`, `/`, `:` (handled below)\n const escaped = pattern.replace(/[.+?^${}()|[\\]\\\\]/g, '\\\\$&')\n return (\n '^' +\n escaped\n .replace(/\\*\\*/g, '.+') // ** → multi-segment wildcard\n .replace(/\\*/g, '[^/]+') // * → single-segment wildcard\n .replace(/:[^/]+/g, '[^/]+') // :param → single-segment wildcard\n + '$'\n )\n}\n\nfunction _patternError(url: string, method: string): Error {\n return new Error(\n `[eidos] resource('${url}') is a URL pattern — ${method}() is not supported on pattern handles. ` +\n `The SW intercepts matching requests automatically; call fetch(specificUrl) directly in your app code.`,\n )\n}\n\n// ── resource() ────────────────────────────────────────────────────────────────\n\nexport function resource<T = unknown>(\n url: string,\n config: ResourceConfig,\n): ResourceHandle<T> {\n if (_registry.has(url)) {\n if (import.meta.env.DEV) {\n const existing = _registry.get(url)!\n const existingCfg = existing.config\n if (\n existingCfg.offline !== config.offline ||\n existingCfg.strategy !== config.strategy ||\n existingCfg.cacheName !== config.cacheName\n ) {\n console.warn(\n `[eidos] resource('${url}') already registered with a different config — returning cached handle. Call resource.unregister() first to re-register.`,\n { registered: existingCfg, ignored: config },\n )\n }\n }\n return _registry.get(url) as ResourceHandle<T>\n }\n\n const strategy = deriveStrategy(url, config)\n const regexStr = isPattern(url) ? patternToRegexStr(url) : undefined\n\n const entry: ResourceEntry = {\n url,\n config,\n strategy,\n status: 'idle',\n cacheHits: 0,\n cacheMisses: 0,\n }\n\n useEidosStore.getState().registerResource(url, entry)\n\n sendToWorker({\n type: 'EIDOS_REGISTER_RESOURCE',\n url,\n strategy: strategy.swStrategy,\n cacheName: strategy.cacheName,\n ...(regexStr !== undefined && { pattern: regexStr }),\n })\n\n const handle: ResourceHandle<T> = {\n url,\n config,\n strategy,\n\n fetch: async () => {\n if (isPattern(url)) throw _patternError(url, 'fetch')\n\n // ── Deduplication: coalesce concurrent fetches for the same URL ─────\n // If a request is already in-flight, piggyback on it and return a clone\n // so each caller gets an independent readable Response body.\n const existing = _inflightRequests.get(url)\n if (existing) return existing.then((r) => r.clone())\n\n // Store the raw-response promise. All callers (including the primary)\n // receive a clone — the raw response stays unconsumed in the map so\n // any caller arriving while the promise is still pending can clone it.\n const task = _fetchResource(url, config, strategy)\n _inflightRequests.set(url, task)\n task.finally(() => _inflightRequests.delete(url))\n return task.then((r) => r.clone())\n },\n\n json: async () => {\n if (isPattern(url)) throw _patternError(url, 'json')\n const res = await handle.fetch()\n return res.json() as Promise<T>\n },\n\n query: () => {\n if (isPattern(url)) throw _patternError(url, 'query')\n return {\n queryKey: ['eidos', url] as [string, string],\n queryFn: () => handle.json(),\n }\n },\n\n prefetch: async () => {\n if (isPattern(url)) throw _patternError(url, 'prefetch')\n await handle.fetch()\n },\n\n invalidate: async () => {\n sendToWorker({ type: 'EIDOS_CLEAR_CACHE', url })\n const cache = await caches.open(strategy.cacheName).catch(() => null)\n if (cache) {\n const keys = await cache.keys()\n const patternRe = regexStr ? new RegExp(regexStr) : null\n const isCrossOrigin = url.startsWith('http')\n await Promise.all(\n keys\n .filter((r) => {\n const rUrl = r.url\n const p = new URL(rUrl).pathname\n if (patternRe) {\n // Cross-origin patterns were compiled from absolute URLs; test full URL.\n return patternRe.test(isCrossOrigin ? rUrl : p)\n }\n return isCrossOrigin ? rUrl === url : (rUrl === url || p === url)\n })\n .map((r) => cache.delete(r)),\n )\n }\n // For exact-URL resources update the store entry; patterns don't have a\n // single entry to update (individual URLs are not tracked per-pattern).\n if (!isPattern(url)) {\n useEidosStore.getState().updateResource(url, {\n status: 'stale',\n cachedAt: undefined,\n lastEvent: 'cache-cleared',\n cacheHits: 0,\n cacheMisses: 0,\n })\n }\n // Notify TanStack Query bridge if registered.\n _queryInvalidator?.(['eidos', url])\n },\n\n unregister: () => {\n _registry.delete(url)\n sendToWorker({ type: 'EIDOS_UNREGISTER_RESOURCE', url })\n useEidosStore.getState().unregisterResource(url)\n },\n }\n\n _registry.set(url, handle)\n return handle\n}\n\n// ── _fetchResource ─────────────────────────────────────────────────────────────\n// The actual network/cache implementation. Separated from handle.fetch() so the\n// deduplication wrapper can store the Promise and share it across concurrent callers.\n// Returns the raw (unconsumed) Response — callers MUST .clone() before reading body.\nasync function _fetchResource(\n url: string,\n config: ResourceConfig,\n strategy: GeneratedStrategy,\n): Promise<Response> {\n const store = useEidosStore.getState()\n store.updateResource(url, { status: 'fetching', fetchedAt: Date.now() })\n\n // Open cache once and reuse across try/catch — avoids a redundant\n // caches.open() call in the error fallback path.\n const cache = await caches.open(strategy.cacheName).catch(() => null)\n\n try {\n // ── network-first: skip cache check, go straight to network ─────────\n // For cache-first / SWR the cache check below is correct. For\n // network-first, reading cache first and returning early would\n // contradict the strategy — fresh data is the priority.\n if (strategy.swStrategy !== 'network-first') {\n // ── Direct Cache API check ─────────────────────────────────────────\n // We read the cache in the main thread rather than waiting for\n // an async SW postMessage. This gives instant, reliable status\n // updates regardless of SW message timing.\n const cached = cache ? await cache.match(url).catch(() => null) : null\n\n // Treat cache as miss if maxAge exceeded\n const current = useEidosStore.getState().resources[url]\n const expired =\n config.maxAge !== undefined &&\n current?.cachedAt !== undefined &&\n Date.now() - current.cachedAt > config.maxAge\n\n if (cached && !expired) {\n store.updateResource(url, {\n status: 'fresh',\n lastEvent: 'cache-hit',\n cacheHits: (current?.cacheHits ?? 0) + 1,\n })\n\n // Background revalidation for SWR (stale-while-revalidate)\n if (strategy.swStrategy === 'stale-while-revalidate') {\n fetch(url)\n .then(async (resp) => {\n if (resp.ok && cache) {\n await cache.put(url, resp.clone())\n useEidosStore.getState().updateResource(url, {\n cachedAt: Date.now(),\n lastEvent: 'cache-updated',\n })\n }\n })\n .catch(() => {\n /* offline — cached version stays valid */\n })\n }\n\n return cached\n }\n\n // Cache miss (or expired)\n const storeEntry = useEidosStore.getState().resources[url]\n store.updateResource(url, {\n cacheMisses: (storeEntry?.cacheMisses ?? 0) + 1,\n })\n }\n\n const response = await fetch(url)\n\n if (response.ok) {\n if (cache) await cache.put(url, response.clone())\n store.updateResource(url, {\n status: 'fresh',\n cachedAt: Date.now(),\n lastEvent: 'cache-updated',\n })\n return response\n }\n\n // Non-2xx response (e.g. 503 from offline SW) — update status and throw\n // so callers get a proper error instead of a plain-object body they can't use.\n store.updateResource(url, { status: response.status === 503 ? 'offline' : 'error' })\n\n // Check if the SW tagged this as an offline response\n const isOffline = response.headers.get('X-Eidos-Offline') === 'true'\n throw new Error(\n isOffline ? `offline: no cached response for ${url}` : `${response.status} ${response.statusText}`,\n )\n } catch (err) {\n // Network failure — try cache one more time as fallback\n const fallback = cache ? await cache.match(url).catch(() => null) : null\n\n if (fallback) {\n const current = useEidosStore.getState().resources[url]\n store.updateResource(url, {\n status: 'fresh',\n lastEvent: 'cache-hit',\n cacheHits: (current?.cacheHits ?? 0) + 1,\n })\n return fallback\n }\n\n store.updateResource(url, { status: 'error' })\n throw err\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Strategy derivation — intent → deterministic caching strategy\n// ─────────────────────────────────────────────────────────────────────────────\n\nfunction deriveStrategy(url: string, config: ResourceConfig): GeneratedStrategy {\n const explicit = config.strategy\n if (config.offline) return buildStrategy(explicit ?? 'stale-while-revalidate', url, config.cacheName)\n return buildStrategy(explicit ?? 'network-first', url, config.cacheName)\n}\n\nconst STRATEGY_META: Record<CacheStrategy, Omit<GeneratedStrategy, 'swStrategy' | 'cacheName'>> = {\n 'stale-while-revalidate': {\n name: 'StaleWhileRevalidate',\n reasoning:\n 'offline: true signals resilience. SWR returns cached data instantly while revalidating in the background — the best tradeoff between speed and freshness for offline-capable resources.',\n behavior: [\n 'Cache hit → return immediately, kick off background revalidation',\n 'Cache miss → fetch from network, cache the response, return it',\n 'Offline → return cached version if available, 503 if not',\n 'Reconnect → next request triggers a background refresh',\n ],\n equivalentCode: `// Workbox equivalent\nnew StaleWhileRevalidate({\n cacheName: 'eidos-resources-v1',\n plugins: [new ExpirationPlugin({ maxEntries: 60 })],\n})`,\n },\n 'cache-first': {\n name: 'CacheFirst',\n reasoning:\n 'cache-first maximises speed and offline availability. Network is consulted only on cache miss. Best for static or infrequently-updated data.',\n behavior: [\n 'Cache hit → return immediately, no network request made',\n 'Cache miss → fetch from network, cache the response, return it',\n 'Offline → return cached version, 503 if cache is empty',\n 'Cache never expires unless explicitly invalidated',\n ],\n equivalentCode: `// Workbox equivalent\nnew CacheFirst({\n cacheName: 'eidos-resources-v1',\n plugins: [new ExpirationPlugin({ maxEntries: 60 })],\n})`,\n },\n 'network-first': {\n name: 'NetworkFirst',\n reasoning:\n 'network-first prioritises fresh data. Cache acts as a safety net when offline. Best for frequently-updated resources where stale data causes problems.',\n behavior: [\n 'Always try network first',\n 'Network success → update cache, return fresh response',\n 'Network failure → fall back to cached version',\n 'Offline with empty cache → return 503 error response',\n ],\n equivalentCode: `// Workbox equivalent\nnew NetworkFirst({\n cacheName: 'eidos-resources-v1',\n networkTimeoutSeconds: 3,\n})`,\n },\n}\n\nfunction buildStrategy(swStrategy: CacheStrategy, _url: string, cacheName?: string): GeneratedStrategy {\n return {\n ...STRATEGY_META[swStrategy],\n swStrategy,\n cacheName: cacheName ?? 'eidos-resources-v1',\n }\n}\n"],"names":["_registry","_inflightRequests","_queryInvalidator","setQueryInvalidator","fn","isPattern","url","patternToRegexStr","pattern","_patternError","method","resource","config","strategy","deriveStrategy","regexStr","entry","useEidosStore","sendToWorker","handle","existing","r","task","_fetchResource","cache","keys","patternRe","isCrossOrigin","rUrl","p","store","cached","current","expired","resp","storeEntry","response","isOffline","err","fallback","explicit","buildStrategy","STRATEGY_META","swStrategy","_url","cacheName"],"mappings":";;AAUA,MAAMA,wBAAgB,IAAA,GAMhBC,wBAAwC,IAAA;AAM9C,IAAIC,IAA6C;AAG1C,SAASC,EAAoBC,GAA4B;AAC9D,EAAAF,IAAoBE;AACtB;AAKA,SAASC,EAAUC,GAAsB;AACvC,SAAOA,EAAI,SAAS,GAAG,KAAK,SAAS,KAAKA,CAAG;AAC/C;AAgBA,SAASC,EAAkBC,GAAyB;AAGlD,SACE,MAFcA,EAAQ,QAAQ,sBAAsB,MAAM,EAIvD,QAAQ,SAAS,IAAI,EACrB,QAAQ,OAAO,OAAO,EACtB,QAAQ,WAAW,OAAO,IAC3B;AAEN;AAEA,SAASC,EAAcH,GAAaI,GAAuB;AACzD,SAAO,IAAI;AAAA,IACT,qBAAqBJ,CAAG,yBAAyBI,CAAM;AAAA,EAAA;AAG3D;AAIO,SAASC,EACdL,GACAM,GACmB;AACnB,MAAIZ,EAAU,IAAIM,CAAG;AAenB,WAAON,EAAU,IAAIM,CAAG;AAG1B,QAAMO,IAAWC,EAAeR,GAAKM,CAAM,GACrCG,IAAWV,EAAUC,CAAG,IAAIC,EAAkBD,CAAG,IAAI,QAErDU,IAAuB;AAAA,IAC3B,KAAAV;AAAA,IACA,QAAAM;AAAA,IACA,UAAAC;AAAA,IACA,QAAQ;AAAA,IACR,WAAW;AAAA,IACX,aAAa;AAAA,EAAA;AAGf,EAAAI,EAAc,SAAA,EAAW,iBAAiBX,GAAKU,CAAK,GAEpDE,EAAa;AAAA,IACX,MAAM;AAAA,IACN,KAAAZ;AAAA,IACA,UAAUO,EAAS;AAAA,IACnB,WAAWA,EAAS;AAAA,IACpB,GAAIE,MAAa,UAAa,EAAE,SAASA,EAAA;AAAA,EAAS,CACnD;AAED,QAAMI,IAA4B;AAAA,IAChC,KAAAb;A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AA,EAAA;AAE5B;"}
|
|
1
|
+
{"version":3,"file":"resource.js","sources":["../src/resource.ts"],"sourcesContent":["import { useEidosStore } from './store'\nimport { sendToWorker } from './sw-bridge'\nimport type {\n ResourceConfig,\n ResourceHandle,\n ResourceEntry,\n GeneratedStrategy,\n CacheStrategy,\n WarmCacheResult,\n} from './types'\n\nconst _registry = new Map<string, ResourceHandle>()\n\n// ── Request deduplication ─────────────────────────────────────────────────────\n// If multiple callers invoke handle.fetch() simultaneously for the same URL,\n// only one network request is made. Each caller gets its own cloned Response.\n// Keyed by URL; entry is deleted when the request settles.\nconst _inflightRequests = /* @__PURE__ */ new Map<string, Promise<Response>>()\n\n// ── TanStack Query bridge (optional) ─────────────────────────────────────────\n// Set by @sweidos/eidos/query when withEidosQueryClient() is called.\n// Lets handle.invalidate() also invalidate the matching TQ cache entry.\ntype QueryInvalidator = (queryKey: [string, string]) => void\nlet _queryInvalidator: QueryInvalidator | null = null\n\n/** @internal Called by @sweidos/eidos/query. */\nexport function setQueryInvalidator(fn: QueryInvalidator): void {\n _queryInvalidator = fn\n}\n\n// ── URL pattern helpers ───────────────────────────────────────────────────────\n\n/** Returns true if `url` contains wildcard or :param segments. */\nfunction isPattern(url: string): boolean {\n return url.includes('*') || /:[^/]+/.test(url)\n}\n\n/**\n * Converts a URL pattern to a regex source string for SW fetch matching.\n * `**` → multi-segment wildcard (`.+`)\n * `*` → single-segment wildcard (`[^/]+`)\n * `:param` → named single segment (`[^/]+`)\n *\n * Special regex characters in the pattern (e.g. `.`) are escaped first so\n * they match literally.\n *\n * @example\n * patternToRegexStr('/api/products/*') // '^/api/products/[^/]+$'\n * patternToRegexStr('/api/products/**') // '^/api/products/.+$'\n * patternToRegexStr('/api/users/:id') // '^/api/users/[^/]+$'\n */\nfunction patternToRegexStr(pattern: string): string {\n // Escape all regex-special chars except `*`, `/`, `:` (handled below)\n const escaped = pattern.replace(/[.+?^${}()|[\\]\\\\]/g, '\\\\$&')\n return (\n '^' +\n escaped\n .replace(/\\*\\*/g, '.+') // ** → multi-segment wildcard\n .replace(/\\*/g, '[^/]+') // * → single-segment wildcard\n .replace(/:[^/]+/g, '[^/]+') // :param → single-segment wildcard\n + '$'\n )\n}\n\nfunction _patternError(url: string, method: string): Error {\n return new Error(\n `[eidos] resource('${url}') is a URL pattern — ${method}() is not supported on pattern handles. ` +\n `The SW intercepts matching requests automatically; call fetch(specificUrl) directly in your app code.`,\n )\n}\n\n// ── resource() ────────────────────────────────────────────────────────────────\n\nexport function resource<T = unknown>(\n url: string,\n config: ResourceConfig,\n): ResourceHandle<T> {\n if (_registry.has(url)) {\n if (import.meta.env.DEV) {\n const existing = _registry.get(url)!\n const existingCfg = existing.config\n if (\n existingCfg.offline !== config.offline ||\n existingCfg.strategy !== config.strategy ||\n existingCfg.cacheName !== config.cacheName\n ) {\n console.warn(\n `[eidos] resource('${url}') already registered with a different config — returning cached handle. Call resource.unregister() first to re-register.`,\n { registered: existingCfg, ignored: config },\n )\n }\n }\n return _registry.get(url) as ResourceHandle<T>\n }\n\n const strategy = deriveStrategy(url, config)\n const regexStr = isPattern(url) ? patternToRegexStr(url) : undefined\n\n const entry: ResourceEntry = {\n url,\n config,\n strategy,\n status: 'idle',\n cacheHits: 0,\n cacheMisses: 0,\n }\n\n useEidosStore.getState().registerResource(url, entry)\n\n sendToWorker({\n type: 'EIDOS_REGISTER_RESOURCE',\n url,\n strategy: strategy.swStrategy,\n cacheName: strategy.cacheName,\n ...(regexStr !== undefined && { pattern: regexStr }),\n })\n\n const handle: ResourceHandle<T> = {\n url,\n config,\n strategy,\n\n fetch: async () => {\n if (isPattern(url)) throw _patternError(url, 'fetch')\n\n // ── Deduplication: coalesce concurrent fetches for the same URL ─────\n // If a request is already in-flight, piggyback on it and return a clone\n // so each caller gets an independent readable Response body.\n const existing = _inflightRequests.get(url)\n if (existing) return existing.then((r) => r.clone())\n\n // Store the raw-response promise. All callers (including the primary)\n // receive a clone — the raw response stays unconsumed in the map so\n // any caller arriving while the promise is still pending can clone it.\n const task = _fetchResource(url, config, strategy)\n _inflightRequests.set(url, task)\n // .catch() silences the unhandled-rejection on the cleanup promise;\n // the error still propagates to callers via task.then() below.\n task.finally(() => _inflightRequests.delete(url)).catch(() => {})\n return task.then((r) => r.clone())\n },\n\n json: async () => {\n if (isPattern(url)) throw _patternError(url, 'json')\n const res = await handle.fetch()\n return res.json() as Promise<T>\n },\n\n query: () => {\n if (isPattern(url)) throw _patternError(url, 'query')\n return {\n queryKey: ['eidos', url] as [string, string],\n queryFn: () => handle.json(),\n }\n },\n\n prefetch: async () => {\n if (isPattern(url)) throw _patternError(url, 'prefetch')\n await handle.fetch()\n },\n\n invalidate: async () => {\n sendToWorker({ type: 'EIDOS_CLEAR_CACHE', url })\n const cache = await caches.open(strategy.cacheName).catch(() => null)\n if (cache) {\n const keys = await cache.keys()\n const patternRe = regexStr ? new RegExp(regexStr) : null\n const isCrossOrigin = url.startsWith('http')\n await Promise.all(\n keys\n .filter((r) => {\n const rUrl = r.url\n const p = new URL(rUrl).pathname\n if (patternRe) {\n // Cross-origin patterns were compiled from absolute URLs; test full URL.\n return patternRe.test(isCrossOrigin ? rUrl : p)\n }\n return isCrossOrigin ? rUrl === url : (rUrl === url || p === url)\n })\n .map((r) => cache.delete(r)),\n )\n }\n // For exact-URL resources update the store entry; patterns don't have a\n // single entry to update (individual URLs are not tracked per-pattern).\n if (!isPattern(url)) {\n useEidosStore.getState().updateResource(url, {\n status: 'stale',\n cachedAt: undefined,\n lastEvent: 'cache-cleared',\n cacheHits: 0,\n cacheMisses: 0,\n })\n }\n // Notify TanStack Query bridge if registered.\n _queryInvalidator?.(['eidos', url])\n },\n\n unregister: () => {\n _registry.delete(url)\n sendToWorker({ type: 'EIDOS_UNREGISTER_RESOURCE', url })\n useEidosStore.getState().unregisterResource(url)\n },\n }\n\n _registry.set(url, handle)\n return handle\n}\n\n// ── _fetchResource ─────────────────────────────────────────────────────────────\n// The actual network/cache implementation. Separated from handle.fetch() so the\n// deduplication wrapper can store the Promise and share it across concurrent callers.\n// Returns the raw (unconsumed) Response — callers MUST .clone() before reading body.\nasync function _fetchResource(\n url: string,\n config: ResourceConfig,\n strategy: GeneratedStrategy,\n): Promise<Response> {\n const store = useEidosStore.getState()\n store.updateResource(url, { status: 'fetching', fetchedAt: Date.now() })\n\n // Open cache once and reuse across try/catch — avoids a redundant\n // caches.open() call in the error fallback path.\n const cache = await caches.open(strategy.cacheName).catch(() => null)\n\n try {\n // ── network-first: skip cache check, go straight to network ─────────\n // For cache-first / SWR the cache check below is correct. For\n // network-first, reading cache first and returning early would\n // contradict the strategy — fresh data is the priority.\n if (strategy.swStrategy !== 'network-first') {\n // ── Direct Cache API check ─────────────────────────────────────────\n // We read the cache in the main thread rather than waiting for\n // an async SW postMessage. This gives instant, reliable status\n // updates regardless of SW message timing.\n const cached = cache ? await cache.match(url).catch(() => null) : null\n\n // Treat cache as miss if maxAge exceeded\n const current = useEidosStore.getState().resources[url]\n const expired =\n config.maxAge !== undefined &&\n current?.cachedAt !== undefined &&\n Date.now() - current.cachedAt > config.maxAge\n\n if (cached && !expired) {\n store.updateResource(url, {\n status: 'fresh',\n lastEvent: 'cache-hit',\n cacheHits: (current?.cacheHits ?? 0) + 1,\n })\n\n // Background revalidation for SWR (stale-while-revalidate)\n if (strategy.swStrategy === 'stale-while-revalidate') {\n fetch(url, { signal: AbortSignal.timeout(5000) })\n .then(async (resp) => {\n if (resp.ok && cache) {\n await cache.put(url, resp.clone())\n useEidosStore.getState().updateResource(url, {\n cachedAt: Date.now(),\n lastEvent: 'cache-updated',\n })\n }\n })\n .catch(() => {\n /* offline or timed out — cached version stays valid */\n })\n }\n\n return cached\n }\n\n // Cache miss (or expired)\n const storeEntry = useEidosStore.getState().resources[url]\n store.updateResource(url, {\n cacheMisses: (storeEntry?.cacheMisses ?? 0) + 1,\n })\n }\n\n const response = await fetch(url)\n\n if (response.ok) {\n if (cache) await cache.put(url, response.clone())\n store.updateResource(url, {\n status: 'fresh',\n cachedAt: Date.now(),\n lastEvent: 'cache-updated',\n })\n return response\n }\n\n // Non-2xx response (e.g. 503 from offline SW) — update status and throw\n // so callers get a proper error instead of a plain-object body they can't use.\n store.updateResource(url, { status: response.status === 503 ? 'offline' : 'error' })\n\n // Check if the SW tagged this as an offline response\n const isOffline = response.headers.get('X-Eidos-Offline') === 'true'\n throw new Error(\n isOffline ? `offline: no cached response for ${url}` : `${response.status} ${response.statusText}`,\n )\n } catch (err) {\n // Network failure — try cache one more time as fallback\n const fallback = cache ? await cache.match(url).catch(() => null) : null\n\n if (fallback) {\n const current = useEidosStore.getState().resources[url]\n store.updateResource(url, {\n status: 'fresh',\n lastEvent: 'cache-hit',\n cacheHits: (current?.cacheHits ?? 0) + 1,\n })\n return fallback\n }\n\n store.updateResource(url, { status: 'error' })\n throw err\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Strategy derivation — intent → deterministic caching strategy\n// ─────────────────────────────────────────────────────────────────────────────\n\nfunction deriveStrategy(url: string, config: ResourceConfig): GeneratedStrategy {\n const explicit = config.strategy\n if (config.offline) return buildStrategy(explicit ?? 'stale-while-revalidate', url, config.cacheName)\n return buildStrategy(explicit ?? 'network-first', url, config.cacheName)\n}\n\nconst STRATEGY_META: Record<CacheStrategy, Omit<GeneratedStrategy, 'swStrategy' | 'cacheName'>> = {\n 'stale-while-revalidate': {\n name: 'StaleWhileRevalidate',\n reasoning:\n 'offline: true signals resilience. SWR returns cached data instantly while revalidating in the background — the best tradeoff between speed and freshness for offline-capable resources.',\n behavior: [\n 'Cache hit → return immediately, kick off background revalidation',\n 'Cache miss → fetch from network, cache the response, return it',\n 'Offline → return cached version if available, 503 if not',\n 'Reconnect → next request triggers a background refresh',\n ],\n equivalentCode: `// Workbox equivalent\nnew StaleWhileRevalidate({\n cacheName: 'eidos-resources-v1',\n plugins: [new ExpirationPlugin({ maxEntries: 60 })],\n})`,\n },\n 'cache-first': {\n name: 'CacheFirst',\n reasoning:\n 'cache-first maximises speed and offline availability. Network is consulted only on cache miss. Best for static or infrequently-updated data.',\n behavior: [\n 'Cache hit → return immediately, no network request made',\n 'Cache miss → fetch from network, cache the response, return it',\n 'Offline → return cached version, 503 if cache is empty',\n 'Cache never expires unless explicitly invalidated',\n ],\n equivalentCode: `// Workbox equivalent\nnew CacheFirst({\n cacheName: 'eidos-resources-v1',\n plugins: [new ExpirationPlugin({ maxEntries: 60 })],\n})`,\n },\n 'network-first': {\n name: 'NetworkFirst',\n reasoning:\n 'network-first prioritises fresh data. Cache acts as a safety net when offline. Best for frequently-updated resources where stale data causes problems.',\n behavior: [\n 'Always try network first',\n 'Network success → update cache, return fresh response',\n 'Network failure → fall back to cached version',\n 'Offline with empty cache → return 503 error response',\n ],\n equivalentCode: `// Workbox equivalent\nnew NetworkFirst({\n cacheName: 'eidos-resources-v1',\n networkTimeoutSeconds: 3,\n})`,\n },\n}\n\nfunction buildStrategy(swStrategy: CacheStrategy, _url: string, cacheName?: string): GeneratedStrategy {\n return {\n ...STRATEGY_META[swStrategy],\n swStrategy,\n cacheName: cacheName ?? 'eidos-resources-v1',\n }\n}\n\n// ── warmCache ─────────────────────────────────────────────────────────────────\n\n/**\n * Bulk-prefetch an array of resource handles concurrently, warming the cache\n * for each one. Useful on login / app init when you know which resources the\n * user will need offline.\n *\n * Pattern handles (containing `*`, `**`, or `:param`) are silently skipped —\n * they match multiple URLs so there is no single URL to prefetch.\n *\n * @example\n * import { warmCache } from '@sweidos/eidos'\n *\n * // In EidosProvider's onReady, or after login:\n * const { warmed, failed } = await warmCache([products, userProfile, settings])\n */\nexport async function warmCache(handles: ResourceHandle[]): Promise<WarmCacheResult> {\n const results = await Promise.allSettled(handles.map((h) => h.prefetch()))\n const errors = results\n .filter((r): r is PromiseRejectedResult => r.status === 'rejected')\n .map((r) => r.reason)\n\n if (import.meta.env.DEV && errors.length > 0) {\n console.warn(`[eidos] warmCache: ${errors.length} handle(s) failed to prefetch`, errors)\n }\n\n return {\n warmed: results.filter((r) => r.status === 'fulfilled').length,\n failed: errors.length,\n errors,\n }\n}\n"],"names":["_registry","_inflightRequests","_queryInvalidator","setQueryInvalidator","fn","isPattern","url","patternToRegexStr","pattern","_patternError","method","resource","config","strategy","deriveStrategy","regexStr","entry","useEidosStore","sendToWorker","handle","existing","r","task","_fetchResource","cache","keys","patternRe","isCrossOrigin","rUrl","p","store","cached","current","expired","resp","storeEntry","response","isOffline","err","fallback","explicit","buildStrategy","STRATEGY_META","swStrategy","_url","cacheName","warmCache","handles","results","h","errors"],"mappings":";;AAWA,MAAMA,wBAAgB,IAAA,GAMhBC,wBAAwC,IAAA;AAM9C,IAAIC,IAA6C;AAG1C,SAASC,EAAoBC,GAA4B;AAC9D,EAAAF,IAAoBE;AACtB;AAKA,SAASC,EAAUC,GAAsB;AACvC,SAAOA,EAAI,SAAS,GAAG,KAAK,SAAS,KAAKA,CAAG;AAC/C;AAgBA,SAASC,EAAkBC,GAAyB;AAGlD,SACE,MAFcA,EAAQ,QAAQ,sBAAsB,MAAM,EAIvD,QAAQ,SAAS,IAAI,EACrB,QAAQ,OAAO,OAAO,EACtB,QAAQ,WAAW,OAAO,IAC3B;AAEN;AAEA,SAASC,EAAcH,GAAaI,GAAuB;AACzD,SAAO,IAAI;AAAA,IACT,qBAAqBJ,CAAG,yBAAyBI,CAAM;AAAA,EAAA;AAG3D;AAIO,SAASC,EACdL,GACAM,GACmB;AACnB,MAAIZ,EAAU,IAAIM,CAAG;AAenB,WAAON,EAAU,IAAIM,CAAG;AAG1B,QAAMO,IAAWC,EAAeR,GAAKM,CAAM,GACrCG,IAAWV,EAAUC,CAAG,IAAIC,EAAkBD,CAAG,IAAI,QAErDU,IAAuB;AAAA,IAC3B,KAAAV;AAAA,IACA,QAAAM;AAAA,IACA,UAAAC;AAAA,IACA,QAAQ;AAAA,IACR,WAAW;AAAA,IACX,aAAa;AAAA,EAAA;AAGf,EAAAI,EAAc,SAAA,EAAW,iBAAiBX,GAAKU,CAAK,GAEpDE,EAAa;AAAA,IACX,MAAM;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;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,IAEF,gBAAgB;AAAA;AAAA;AAAA;AAAA;AAAA,EAAA;AAMpB;AAEA,SAASD,EAAcE,GAA2BC,GAAcC,GAAuC;AACrG,SAAO;AAAA,IACL,GAAGH,EAAcC,CAAU;AAAA,IAC3B,YAAAA;AAAA,IACA,WAAWE,KAAa;AAAA,EAAA;AAE5B;AAkBA,eAAsBC,EAAUC,GAAqD;AACnF,QAAMC,IAAU,MAAM,QAAQ,WAAWD,EAAQ,IAAI,CAACE,MAAMA,EAAE,SAAA,CAAU,CAAC,GACnEC,IAASF,EACZ,OAAO,CAAC3B,MAAkCA,EAAE,WAAW,UAAU,EACjE,IAAI,CAACA,MAAMA,EAAE,MAAM;AAMtB,SAAO;AAAA,IACL,QAAQ2B,EAAQ,OAAO,CAAC3B,MAAMA,EAAE,WAAW,WAAW,EAAE;AAAA,IACxD,QAAQ6B,EAAO;AAAA,IACf,QAAAA;AAAA,EAAA;AAEJ;"}
|
package/dist/runtime.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { registerServiceWorker as
|
|
1
|
+
import { registerServiceWorker as d, registerBgSyncHandler as m } from "./sw-bridge.js";
|
|
2
2
|
import { replayQueue as n } from "./action.js";
|
|
3
3
|
import { useEidosStore as e } from "./store.js";
|
|
4
4
|
import { idbGetQueue as p } from "./idb.js";
|
|
5
5
|
let o = !1, s = null;
|
|
6
|
-
async function
|
|
7
|
-
if (o) return;
|
|
6
|
+
async function w(r = {}) {
|
|
7
|
+
if (typeof window > "u" || o) return;
|
|
8
8
|
o = !0;
|
|
9
9
|
const u = r.swPath ?? "/eidos-sw.js", l = r.autoReplay ?? !0;
|
|
10
10
|
try {
|
|
@@ -13,26 +13,26 @@ async function O(r = {}) {
|
|
|
13
13
|
} catch {
|
|
14
14
|
}
|
|
15
15
|
try {
|
|
16
|
-
await
|
|
16
|
+
await d(u);
|
|
17
17
|
} catch {
|
|
18
18
|
}
|
|
19
|
-
if (
|
|
19
|
+
if (m(() => {
|
|
20
20
|
e.getState().isOnline && setTimeout(n, 200);
|
|
21
21
|
}), l) {
|
|
22
22
|
let t = e.getState().isOnline;
|
|
23
23
|
s = e.subscribe(() => {
|
|
24
|
-
const { isOnline: i } = e.getState(),
|
|
25
|
-
t = i,
|
|
24
|
+
const { isOnline: i } = e.getState(), c = i && !t;
|
|
25
|
+
t = i, c && setTimeout(n, 600);
|
|
26
26
|
});
|
|
27
|
-
const a = e.getState(),
|
|
28
|
-
a.isOnline &&
|
|
27
|
+
const a = e.getState(), f = a.queue.some((i) => i.status === "pending" || i.status === "failed");
|
|
28
|
+
a.isOnline && f && setTimeout(n, 1200);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
function
|
|
31
|
+
function O() {
|
|
32
32
|
s == null || s(), s = null, o = !1;
|
|
33
33
|
}
|
|
34
34
|
export {
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
O as _resetEidos,
|
|
36
|
+
w as initEidos
|
|
37
37
|
};
|
|
38
38
|
//# sourceMappingURL=runtime.js.map
|
package/dist/runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.js","sources":["../src/runtime.ts"],"sourcesContent":["import { registerServiceWorker, registerBgSyncHandler } from './sw-bridge'\nimport { replayQueue } from './action'\nimport { useEidosStore } from './store'\nimport { idbGetQueue } from './idb'\n\nexport interface EidosConfig {\n /** Path to the eidos service worker. Defaults to '/eidos-sw.js'. */\n swPath?: string\n /** Automatically replay the action queue on reconnect. Default: true. */\n autoReplay?: boolean\n}\n\nlet _initialized = false\nlet _unsubscribe: (() => void) | null = null\n\nexport async function initEidos(config: EidosConfig = {}): Promise<void> {\n if (_initialized) return\n _initialized = true\n\n const swPath = config.swPath ?? '/eidos-sw.js'\n const autoReplay = config.autoReplay ?? true\n\n // Restore persisted queue from IndexedDB on startup\n try {\n const persisted = await idbGetQueue()\n if (persisted.length > 0) {\n useEidosStore.getState().hydrateQueue(persisted)\n }\n } catch {\n // IndexedDB unavailable (Firefox private browsing) — silent fallback\n }\n\n try {\n await registerServiceWorker(swPath)\n } catch {\n // SW registration failed; app continues without offline support\n }\n\n // When the SW fires the Background Sync tag, replay the queue in the main thread.\n // This path runs even if the user briefly navigated away and back — the browser\n // triggers the sync event on the SW, which wakes up all open clients.\n registerBgSyncHandler(() => {\n if (useEidosStore.getState().isOnline) {\n setTimeout(replayQueue, 200)\n }\n })\n\n if (autoReplay) {\n // ── Subscribe to the store instead of window.addEventListener('online')\n //\n // WHY: setOfflineSimulation() updates the store directly but never fires a\n // real browser `online` event. Watching the store catches both:\n // • Real network reconnects (sw-bridge updates store on window.online)\n // • Simulation toggled off (setOfflineSimulation(false) → store.setOnline(true))\n //\n let prevIsOnline = useEidosStore.getState().isOnline\n\n _unsubscribe = useEidosStore.subscribe(() => {\n const { isOnline } = useEidosStore.getState()\n const justCameOnline = isOnline && !prevIsOnline\n prevIsOnline = isOnline\n\n if (justCameOnline) {\n // Small delay so the connection (or simulation reset) settles first\n setTimeout(replayQueue, 600)\n }\n })\n\n // Replay any pending items that survived a page reload\n const store = useEidosStore.getState()\n const hasPending = store.queue.some((q) => q.status === 'pending' || q.status === 'failed')\n if (store.isOnline && hasPending) {\n setTimeout(replayQueue, 1200)\n }\n }\n\n if (import.meta.env.DEV) {\n const store = useEidosStore.getState()\n console.groupCollapsed('%c⚡ Eidos', 'color:#38bdf8;font-weight:bold')\n console.log('SW path :', swPath)\n console.log('Auto-replay:', autoReplay)\n console.log('SW status :', store.swStatus)\n console.groupEnd()\n }\n}\n\nexport function _resetEidos() {\n _unsubscribe?.()\n _unsubscribe = null\n _initialized = false\n}\n"],"names":["_initialized","_unsubscribe","initEidos","config","swPath","autoReplay","persisted","idbGetQueue","useEidosStore","registerServiceWorker","registerBgSyncHandler","replayQueue","prevIsOnline","isOnline","justCameOnline","store","hasPending","q","_resetEidos"],"mappings":";;;;AAYA,IAAIA,IAAe,IACfC,IAAoC;AAExC,eAAsBC,EAAUC,IAAsB,IAAmB;
|
|
1
|
+
{"version":3,"file":"runtime.js","sources":["../src/runtime.ts"],"sourcesContent":["import { registerServiceWorker, registerBgSyncHandler } from './sw-bridge'\nimport { replayQueue } from './action'\nimport { useEidosStore } from './store'\nimport { idbGetQueue } from './idb'\n\nexport interface EidosConfig {\n /** Path to the eidos service worker. Defaults to '/eidos-sw.js'. */\n swPath?: string\n /** Automatically replay the action queue on reconnect. Default: true. */\n autoReplay?: boolean\n}\n\nlet _initialized = false\nlet _unsubscribe: (() => void) | null = null\n\nexport async function initEidos(config: EidosConfig = {}): Promise<void> {\n // Skip silently during SSR — SW, IndexedDB, and window are browser-only.\n if (typeof window === 'undefined') return\n if (_initialized) return\n _initialized = true\n\n const swPath = config.swPath ?? '/eidos-sw.js'\n const autoReplay = config.autoReplay ?? true\n\n // Restore persisted queue from IndexedDB on startup\n try {\n const persisted = await idbGetQueue()\n if (persisted.length > 0) {\n useEidosStore.getState().hydrateQueue(persisted)\n }\n } catch {\n // IndexedDB unavailable (Firefox private browsing) — silent fallback\n }\n\n try {\n await registerServiceWorker(swPath)\n } catch {\n // SW registration failed; app continues without offline support\n }\n\n // When the SW fires the Background Sync tag, replay the queue in the main thread.\n // This path runs even if the user briefly navigated away and back — the browser\n // triggers the sync event on the SW, which wakes up all open clients.\n registerBgSyncHandler(() => {\n if (useEidosStore.getState().isOnline) {\n setTimeout(replayQueue, 200)\n }\n })\n\n if (autoReplay) {\n // ── Subscribe to the store instead of window.addEventListener('online')\n //\n // WHY: setOfflineSimulation() updates the store directly but never fires a\n // real browser `online` event. Watching the store catches both:\n // • Real network reconnects (sw-bridge updates store on window.online)\n // • Simulation toggled off (setOfflineSimulation(false) → store.setOnline(true))\n //\n let prevIsOnline = useEidosStore.getState().isOnline\n\n _unsubscribe = useEidosStore.subscribe(() => {\n const { isOnline } = useEidosStore.getState()\n const justCameOnline = isOnline && !prevIsOnline\n prevIsOnline = isOnline\n\n if (justCameOnline) {\n // Small delay so the connection (or simulation reset) settles first\n setTimeout(replayQueue, 600)\n }\n })\n\n // Replay any pending items that survived a page reload\n const store = useEidosStore.getState()\n const hasPending = store.queue.some((q) => q.status === 'pending' || q.status === 'failed')\n if (store.isOnline && hasPending) {\n setTimeout(replayQueue, 1200)\n }\n }\n\n if (import.meta.env.DEV) {\n const store = useEidosStore.getState()\n console.groupCollapsed('%c⚡ Eidos', 'color:#38bdf8;font-weight:bold')\n console.log('SW path :', swPath)\n console.log('Auto-replay:', autoReplay)\n console.log('SW status :', store.swStatus)\n console.groupEnd()\n }\n}\n\nexport function _resetEidos() {\n _unsubscribe?.()\n _unsubscribe = null\n _initialized = false\n}\n"],"names":["_initialized","_unsubscribe","initEidos","config","swPath","autoReplay","persisted","idbGetQueue","useEidosStore","registerServiceWorker","registerBgSyncHandler","replayQueue","prevIsOnline","isOnline","justCameOnline","store","hasPending","q","_resetEidos"],"mappings":";;;;AAYA,IAAIA,IAAe,IACfC,IAAoC;AAExC,eAAsBC,EAAUC,IAAsB,IAAmB;AAGvE,MADI,OAAO,SAAW,OAClBH,EAAc;AAClB,EAAAA,IAAe;AAEf,QAAMI,IAASD,EAAO,UAAU,gBAC1BE,IAAaF,EAAO,cAAc;AAGxC,MAAI;AACF,UAAMG,IAAY,MAAMC,EAAA;AACxB,IAAID,EAAU,SAAS,KACrBE,EAAc,SAAA,EAAW,aAAaF,CAAS;AAAA,EAEnD,QAAQ;AAAA,EAER;AAEA,MAAI;AACF,UAAMG,EAAsBL,CAAM;AAAA,EACpC,QAAQ;AAAA,EAER;AAWA,MANAM,EAAsB,MAAM;AAC1B,IAAIF,EAAc,SAAA,EAAW,YAC3B,WAAWG,GAAa,GAAG;AAAA,EAE/B,CAAC,GAEGN,GAAY;AAQd,QAAIO,IAAeJ,EAAc,SAAA,EAAW;AAE5C,IAAAP,IAAeO,EAAc,UAAU,MAAM;AAC3C,YAAM,EAAE,UAAAK,EAAA,IAAaL,EAAc,SAAA,GAC7BM,IAAiBD,KAAY,CAACD;AACpC,MAAAA,IAAeC,GAEXC,KAEF,WAAWH,GAAa,GAAG;AAAA,IAE/B,CAAC;AAGD,UAAMI,IAAQP,EAAc,SAAA,GACtBQ,IAAaD,EAAM,MAAM,KAAK,CAACE,MAAMA,EAAE,WAAW,aAAaA,EAAE,WAAW,QAAQ;AAC1F,IAAIF,EAAM,YAAYC,KACpB,WAAWL,GAAa,IAAI;AAAA,EAEhC;AAUF;AAEO,SAASO,IAAc;AAC5B,EAAAjB,KAAA,QAAAA,KACAA,IAAe,MACfD,IAAe;AACjB;"}
|