sibujs 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (302) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1630 -0
  3. package/dist/browser.cjs +815 -0
  4. package/dist/browser.d.cts +174 -0
  5. package/dist/browser.d.ts +174 -0
  6. package/dist/browser.js +458 -0
  7. package/dist/build.cjs +4970 -0
  8. package/dist/build.d.cts +630 -0
  9. package/dist/build.d.ts +630 -0
  10. package/dist/build.js +2478 -0
  11. package/dist/cdn.global.js +115 -0
  12. package/dist/chunk-27QC4FPL.js +67 -0
  13. package/dist/chunk-2ABBWCGC.js +65 -0
  14. package/dist/chunk-2MUNQYZ7.js +26 -0
  15. package/dist/chunk-2PSPKNUI.js +1711 -0
  16. package/dist/chunk-35CDLDX5.js +1758 -0
  17. package/dist/chunk-36MU4CFV.js +41 -0
  18. package/dist/chunk-3FIQOFI6.js +182 -0
  19. package/dist/chunk-3GHNC2BN.js +28 -0
  20. package/dist/chunk-3HLWWEPU.js +909 -0
  21. package/dist/chunk-3IVI3J54.js +252 -0
  22. package/dist/chunk-3KZ72WNW.js +944 -0
  23. package/dist/chunk-4AU64SQV.js +182 -0
  24. package/dist/chunk-4MOK7HAR.js +84 -0
  25. package/dist/chunk-4QK6FBDH.js +1429 -0
  26. package/dist/chunk-566Z7HXB.js +737 -0
  27. package/dist/chunk-5CRBB7XP.js +358 -0
  28. package/dist/chunk-5G67D3IZ.js +168 -0
  29. package/dist/chunk-5NCPAWBE.js +99 -0
  30. package/dist/chunk-5O2RKXR3.js +1444 -0
  31. package/dist/chunk-6BTBDO6A.js +633 -0
  32. package/dist/chunk-6IWEHW57.js +43 -0
  33. package/dist/chunk-6JGMNCD6.js +282 -0
  34. package/dist/chunk-6QRLJNXR.js +1425 -0
  35. package/dist/chunk-7HM5UE5T.js +270 -0
  36. package/dist/chunk-7JOLTGUH.js +58 -0
  37. package/dist/chunk-7MCWJCQK.js +909 -0
  38. package/dist/chunk-7RIIFP3E.js +1758 -0
  39. package/dist/chunk-7UASYN3G.js +254 -0
  40. package/dist/chunk-7W2WYHDI.js +741 -0
  41. package/dist/chunk-7Y35RDSJ.js +872 -0
  42. package/dist/chunk-A65GFJBL.js +65 -0
  43. package/dist/chunk-AD6ZIEDK.js +67 -0
  44. package/dist/chunk-AK5Y72F3.js +1426 -0
  45. package/dist/chunk-APOMMWH4.js +282 -0
  46. package/dist/chunk-ARZVTWIQ.js +1750 -0
  47. package/dist/chunk-AWWBM2BI.js +664 -0
  48. package/dist/chunk-AX5VEQTY.js +58 -0
  49. package/dist/chunk-AYTXVOW3.js +1708 -0
  50. package/dist/chunk-BG4A246G.js +1746 -0
  51. package/dist/chunk-BNFJJA2L.js +1425 -0
  52. package/dist/chunk-BPKPBVU5.js +59 -0
  53. package/dist/chunk-BPKPPSXC.js +282 -0
  54. package/dist/chunk-BPWKKK7F.js +1711 -0
  55. package/dist/chunk-CCKX6YTC.js +1735 -0
  56. package/dist/chunk-CIF5Z3MP.js +58 -0
  57. package/dist/chunk-CSXYU7IO.js +457 -0
  58. package/dist/chunk-D6JD4FDC.js +26 -0
  59. package/dist/chunk-E7NGA7X2.js +59 -0
  60. package/dist/chunk-EEPPJKAE.js +443 -0
  61. package/dist/chunk-EJMYGAGQ.js +717 -0
  62. package/dist/chunk-EL6Z5MDY.js +55 -0
  63. package/dist/chunk-EP7VRLEB.js +41 -0
  64. package/dist/chunk-ETMEC6FH.js +99 -0
  65. package/dist/chunk-EZ2WHYVL.js +65 -0
  66. package/dist/chunk-EZRVMSZK.js +67 -0
  67. package/dist/chunk-F2TRGINX.js +254 -0
  68. package/dist/chunk-F5JCIH3Q.js +642 -0
  69. package/dist/chunk-FGK3JKMN.js +909 -0
  70. package/dist/chunk-FQWPKSTD.js +1437 -0
  71. package/dist/chunk-FWHVLMCI.js +26 -0
  72. package/dist/chunk-GBEYQRO2.js +303 -0
  73. package/dist/chunk-GBLES3NK.js +248 -0
  74. package/dist/chunk-GQVGUQW6.js +1436 -0
  75. package/dist/chunk-HCV2T76T.js +457 -0
  76. package/dist/chunk-HS7ZKVPR.js +182 -0
  77. package/dist/chunk-HXDVV7HZ.js +909 -0
  78. package/dist/chunk-IB23VMO3.js +1746 -0
  79. package/dist/chunk-IEMZ7RTT.js +99 -0
  80. package/dist/chunk-IPGRSN42.js +1750 -0
  81. package/dist/chunk-IVMOK2QN.js +1750 -0
  82. package/dist/chunk-JCLGQO7T.js +443 -0
  83. package/dist/chunk-JDXL7KDB.js +1436 -0
  84. package/dist/chunk-JIIFW636.js +270 -0
  85. package/dist/chunk-JWGEEH7H.js +944 -0
  86. package/dist/chunk-K2BESAG7.js +1688 -0
  87. package/dist/chunk-K2U5YGF4.js +877 -0
  88. package/dist/chunk-K45FQ4Y4.js +175 -0
  89. package/dist/chunk-K7BPE427.js +1432 -0
  90. package/dist/chunk-KL3266RS.js +26 -0
  91. package/dist/chunk-KNN4P7DZ.js +84 -0
  92. package/dist/chunk-KP2DZH5Q.js +254 -0
  93. package/dist/chunk-KZHAJSQR.js +1636 -0
  94. package/dist/chunk-LBKGHMQV.js +1750 -0
  95. package/dist/chunk-LBTEPL7A.js +1731 -0
  96. package/dist/chunk-LEBBPTDB.js +1444 -0
  97. package/dist/chunk-LLH63WVQ.js +98 -0
  98. package/dist/chunk-LWVR2C4G.js +1711 -0
  99. package/dist/chunk-M3MDTVV2.js +896 -0
  100. package/dist/chunk-M5GNLDEO.js +303 -0
  101. package/dist/chunk-MFHVGKET.js +267 -0
  102. package/dist/chunk-MGWSG3PM.js +358 -0
  103. package/dist/chunk-MJNB47HB.js +19 -0
  104. package/dist/chunk-MLKGABMK.js +9 -0
  105. package/dist/chunk-MQWTY3JY.js +944 -0
  106. package/dist/chunk-MZZOQHNI.js +642 -0
  107. package/dist/chunk-NIHWGZS4.js +1426 -0
  108. package/dist/chunk-NSVVHQK5.js +41 -0
  109. package/dist/chunk-NVI2WE7D.js +443 -0
  110. package/dist/chunk-O7QBO3PH.js +58 -0
  111. package/dist/chunk-OAUPQBO2.js +270 -0
  112. package/dist/chunk-OB2LMD7C.js +297 -0
  113. package/dist/chunk-OHEYBWQU.js +58 -0
  114. package/dist/chunk-OI6OXUHJ.js +443 -0
  115. package/dist/chunk-OX2VMRMV.js +633 -0
  116. package/dist/chunk-P4FYE5TX.js +866 -0
  117. package/dist/chunk-P5KFWM4H.js +98 -0
  118. package/dist/chunk-PUMLE7RJ.js +1711 -0
  119. package/dist/chunk-Q4MFANBF.js +282 -0
  120. package/dist/chunk-QLEKZMMU.js +282 -0
  121. package/dist/chunk-RGGNGVO3.js +98 -0
  122. package/dist/chunk-RKJDRVV6.js +443 -0
  123. package/dist/chunk-S5BHU353.js +43 -0
  124. package/dist/chunk-SHQUSFH7.js +1426 -0
  125. package/dist/chunk-SMB4DBMD.js +182 -0
  126. package/dist/chunk-SNYHQP3D.js +743 -0
  127. package/dist/chunk-T24L3TBF.js +1717 -0
  128. package/dist/chunk-TAQNSOKT.js +692 -0
  129. package/dist/chunk-TDNY4SUA.js +41 -0
  130. package/dist/chunk-TNNF56IQ.js +1750 -0
  131. package/dist/chunk-TR7E6LYX.js +457 -0
  132. package/dist/chunk-URWUFH45.js +98 -0
  133. package/dist/chunk-UUSIH3XH.js +1429 -0
  134. package/dist/chunk-UYFNXLKR.js +1436 -0
  135. package/dist/chunk-V6F7KUWD.js +270 -0
  136. package/dist/chunk-VCZLXRMR.js +254 -0
  137. package/dist/chunk-VDHXSSBT.js +1426 -0
  138. package/dist/chunk-VM4QMKVK.js +254 -0
  139. package/dist/chunk-VWGYKYL2.js +737 -0
  140. package/dist/chunk-VX2OFBJN.js +1426 -0
  141. package/dist/chunk-VXVIE6DG.js +84 -0
  142. package/dist/chunk-W4OH7HG4.js +40 -0
  143. package/dist/chunk-WBVJX4GZ.js +98 -0
  144. package/dist/chunk-WDU2ZV4I.js +1426 -0
  145. package/dist/chunk-X6VUCICU.js +457 -0
  146. package/dist/chunk-XAY7FM7Y.js +618 -0
  147. package/dist/chunk-XJZ5Z2CM.js +642 -0
  148. package/dist/chunk-XKVFQTJJ.js +254 -0
  149. package/dist/chunk-XRLFASCY.js +22 -0
  150. package/dist/chunk-XYU6TZOW.js +182 -0
  151. package/dist/chunk-Y745CBVB.js +944 -0
  152. package/dist/chunk-YLBJSXYY.js +944 -0
  153. package/dist/chunk-YQJIKVPZ.js +1429 -0
  154. package/dist/chunk-YRM2VCZF.js +457 -0
  155. package/dist/chunk-YS33KBVJ.js +944 -0
  156. package/dist/chunk-Z27DZPDG.js +41 -0
  157. package/dist/chunk-ZXQ5NAEN.js +32 -0
  158. package/dist/contracts-B552GopR.d.cts +245 -0
  159. package/dist/contracts-B552GopR.d.ts +245 -0
  160. package/dist/contracts-Bg1ECISC.d.cts +245 -0
  161. package/dist/contracts-Bg1ECISC.d.ts +245 -0
  162. package/dist/contracts-CMriKJ6P.d.cts +245 -0
  163. package/dist/contracts-CMriKJ6P.d.ts +245 -0
  164. package/dist/contracts-DOrhwbke.d.cts +245 -0
  165. package/dist/contracts-DOrhwbke.d.ts +245 -0
  166. package/dist/data.cjs +1373 -0
  167. package/dist/data.d.cts +434 -0
  168. package/dist/data.d.ts +434 -0
  169. package/dist/data.js +945 -0
  170. package/dist/devtools.cjs +1357 -0
  171. package/dist/devtools.d.cts +473 -0
  172. package/dist/devtools.d.ts +473 -0
  173. package/dist/devtools.js +1084 -0
  174. package/dist/ecosystem.cjs +1046 -0
  175. package/dist/ecosystem.d.cts +247 -0
  176. package/dist/ecosystem.d.ts +247 -0
  177. package/dist/ecosystem.js +369 -0
  178. package/dist/extras.cjs +8457 -0
  179. package/dist/extras.d.cts +2356 -0
  180. package/dist/extras.d.ts +2356 -0
  181. package/dist/extras.js +5152 -0
  182. package/dist/index.cjs +2648 -0
  183. package/dist/index.d.cts +869 -0
  184. package/dist/index.d.ts +869 -0
  185. package/dist/index.js +386 -0
  186. package/dist/motion.cjs +604 -0
  187. package/dist/motion.d.cts +146 -0
  188. package/dist/motion.d.ts +146 -0
  189. package/dist/motion.js +346 -0
  190. package/dist/patterns.cjs +815 -0
  191. package/dist/patterns.d.cts +163 -0
  192. package/dist/patterns.d.ts +163 -0
  193. package/dist/patterns.js +296 -0
  194. package/dist/performance.cjs +927 -0
  195. package/dist/performance.d.cts +416 -0
  196. package/dist/performance.d.ts +416 -0
  197. package/dist/performance.js +654 -0
  198. package/dist/plugins.cjs +2487 -0
  199. package/dist/plugins.d.cts +393 -0
  200. package/dist/plugins.d.ts +393 -0
  201. package/dist/plugins.js +1504 -0
  202. package/dist/signal-BnWpq6WB.d.cts +5 -0
  203. package/dist/signal-BnWpq6WB.d.ts +5 -0
  204. package/dist/src/components/ErrorBoundary.d.ts +15 -0
  205. package/dist/src/components/ErrorBoundary.js +119 -0
  206. package/dist/src/core/catch.d.ts +11 -0
  207. package/dist/src/core/catch.js +28 -0
  208. package/dist/src/core/each.d.ts +13 -0
  209. package/dist/src/core/each.js +68 -0
  210. package/dist/src/core/for.d.ts +12 -0
  211. package/dist/src/core/for.js +67 -0
  212. package/dist/src/core/html.d.ts +137 -0
  213. package/dist/src/core/html.js +155 -0
  214. package/dist/src/core/htmlIf.d.ts +11 -0
  215. package/dist/src/core/htmlIf.js +18 -0
  216. package/dist/src/core/lazy.d.ts +7 -0
  217. package/dist/src/core/lazy.js +16 -0
  218. package/dist/src/core/mount.d.ts +7 -0
  219. package/dist/src/core/mount.js +12 -0
  220. package/dist/src/core/slots.d.ts +3 -0
  221. package/dist/src/core/slots.js +3 -0
  222. package/dist/src/core/suspense.d.ts +10 -0
  223. package/dist/src/core/suspense.js +33 -0
  224. package/dist/src/core/tagFactory.d.ts +13 -0
  225. package/dist/src/core/tagFactory.js +86 -0
  226. package/dist/src/core/test.d.ts +11 -0
  227. package/dist/src/core/test.js +28 -0
  228. package/dist/src/core/types.d.ts +2 -0
  229. package/dist/src/core/types.js +1 -0
  230. package/dist/src/core/useComputed.d.ts +6 -0
  231. package/dist/src/core/useComputed.js +30 -0
  232. package/dist/src/core/useEffect.d.ts +6 -0
  233. package/dist/src/core/useEffect.js +23 -0
  234. package/dist/src/core/useState.d.ts +10 -0
  235. package/dist/src/core/useState.js +34 -0
  236. package/dist/src/core/useStore.d.ts +19 -0
  237. package/dist/src/core/useStore.js +53 -0
  238. package/dist/src/core/useWatch.d.ts +8 -0
  239. package/dist/src/core/useWatch.js +23 -0
  240. package/dist/src/plugins/i18n.d.ts +6 -0
  241. package/dist/src/plugins/i18n.js +16 -0
  242. package/dist/src/plugins/router.d.ts +188 -0
  243. package/dist/src/plugins/router.js +1178 -0
  244. package/dist/src/reactivity/bindAttribute.d.ts +5 -0
  245. package/dist/src/reactivity/bindAttribute.js +31 -0
  246. package/dist/src/reactivity/bindChildNode.d.ts +10 -0
  247. package/dist/src/reactivity/bindChildNode.js +46 -0
  248. package/dist/src/reactivity/bindTextNode.d.ts +10 -0
  249. package/dist/src/reactivity/bindTextNode.js +27 -0
  250. package/dist/src/reactivity/signal.d.ts +3 -0
  251. package/dist/src/reactivity/signal.js +1 -0
  252. package/dist/src/reactivity/track.d.ts +18 -0
  253. package/dist/src/reactivity/track.js +73 -0
  254. package/dist/src/reactivity/useComputed.d.ts +6 -0
  255. package/dist/src/reactivity/useComputed.js +30 -0
  256. package/dist/src/reactivity/useEffect.d.ts +6 -0
  257. package/dist/src/reactivity/useEffect.js +23 -0
  258. package/dist/src/reactivity/useState.d.ts +10 -0
  259. package/dist/src/reactivity/useState.js +34 -0
  260. package/dist/src/reactivity/useStore.d.ts +19 -0
  261. package/dist/src/reactivity/useStore.js +53 -0
  262. package/dist/src/reactivity/useWatch.d.ts +8 -0
  263. package/dist/src/reactivity/useWatch.js +23 -0
  264. package/dist/src/utils/sanitize.d.ts +1 -0
  265. package/dist/src/utils/sanitize.js +8 -0
  266. package/dist/ssr-27FOM46T.js +35 -0
  267. package/dist/ssr-GFUTTSJD.js +22 -0
  268. package/dist/ssr-K7DCR6BZ.js +35 -0
  269. package/dist/ssr-O6LFMRFP.js +35 -0
  270. package/dist/ssr-QZEVGMMK.js +35 -0
  271. package/dist/ssr-SGVBCAGC.js +35 -0
  272. package/dist/ssr-UB2IXCYX.js +35 -0
  273. package/dist/ssr-XBZQNV4O.js +22 -0
  274. package/dist/ssr-Y76FSXDU.js +35 -0
  275. package/dist/ssr-YQJ4AYBD.js +35 -0
  276. package/dist/ssr.cjs +1757 -0
  277. package/dist/ssr.d.cts +478 -0
  278. package/dist/ssr.d.ts +478 -0
  279. package/dist/ssr.js +743 -0
  280. package/dist/tagFactory-CZPO4RXF.d.cts +34 -0
  281. package/dist/tagFactory-CZPO4RXF.d.ts +34 -0
  282. package/dist/tagFactory-CgImPVMY.d.cts +22 -0
  283. package/dist/tagFactory-CgImPVMY.d.ts +22 -0
  284. package/dist/tagFactory-Cw1iv5if.d.cts +22 -0
  285. package/dist/tagFactory-Cw1iv5if.d.ts +22 -0
  286. package/dist/tagFactory-DeAXq9ef.d.cts +30 -0
  287. package/dist/tagFactory-DeAXq9ef.d.ts +30 -0
  288. package/dist/tagFactory-SkY0a7L1.d.cts +22 -0
  289. package/dist/tagFactory-SkY0a7L1.d.ts +22 -0
  290. package/dist/testing.cjs +1919 -0
  291. package/dist/testing.d.cts +491 -0
  292. package/dist/testing.d.ts +491 -0
  293. package/dist/testing.js +1862 -0
  294. package/dist/ui.cjs +1497 -0
  295. package/dist/ui.d.cts +264 -0
  296. package/dist/ui.d.ts +264 -0
  297. package/dist/ui.js +900 -0
  298. package/dist/widgets.cjs +919 -0
  299. package/dist/widgets.d.cts +165 -0
  300. package/dist/widgets.d.ts +165 -0
  301. package/dist/widgets.js +545 -0
  302. package/package.json +134 -0
@@ -0,0 +1,944 @@
1
+ import {
2
+ createContext
3
+ } from "./chunk-D6JD4FDC.js";
4
+ import {
5
+ sbDerived
6
+ } from "./chunk-A65GFJBL.js";
7
+ import {
8
+ sbEffect
9
+ } from "./chunk-7JOLTGUH.js";
10
+ import {
11
+ sbBatch,
12
+ sbSignal
13
+ } from "./chunk-GBEYQRO2.js";
14
+
15
+ // src/core/data/retry.ts
16
+ function calculateDelay(attempt, strategy, baseDelay, maxDelay, jitter) {
17
+ let delay;
18
+ switch (strategy) {
19
+ case "exponential":
20
+ delay = baseDelay * 2 ** attempt;
21
+ break;
22
+ case "linear":
23
+ delay = baseDelay * (attempt + 1);
24
+ break;
25
+ case "fixed":
26
+ delay = baseDelay;
27
+ break;
28
+ }
29
+ delay = Math.min(delay, maxDelay);
30
+ if (jitter > 0) {
31
+ const jitterRange = delay * jitter;
32
+ delay += (Math.random() * 2 - 1) * jitterRange;
33
+ }
34
+ return Math.max(0, delay);
35
+ }
36
+ async function withRetry(fn, options, onRetry, signal) {
37
+ const maxRetries = options?.maxRetries ?? 3;
38
+ const strategy = options?.strategy ?? "exponential";
39
+ const baseDelay = options?.baseDelay ?? 1e3;
40
+ const maxDelay = options?.maxDelay ?? 3e4;
41
+ const jitter = options?.jitter ?? 0.1;
42
+ const shouldRetry = options?.shouldRetry ?? (() => true);
43
+ let lastError;
44
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
45
+ if (signal?.aborted) throw new DOMException("Aborted", "AbortError");
46
+ try {
47
+ return await fn();
48
+ } catch (error) {
49
+ lastError = error;
50
+ if (attempt >= maxRetries || !shouldRetry(error, attempt)) throw error;
51
+ const delay = calculateDelay(attempt, strategy, baseDelay, maxDelay, jitter);
52
+ onRetry?.(error, attempt, delay);
53
+ await new Promise((resolve, reject) => {
54
+ const timer = setTimeout(resolve, delay);
55
+ if (signal) {
56
+ const onAbort = () => {
57
+ clearTimeout(timer);
58
+ reject(new DOMException("Aborted", "AbortError"));
59
+ };
60
+ signal.addEventListener("abort", onAbort, { once: true });
61
+ }
62
+ });
63
+ }
64
+ }
65
+ throw lastError;
66
+ }
67
+
68
+ // src/core/data/sbQuery.ts
69
+ var queryCache = /* @__PURE__ */ new Map();
70
+ function getOrCreateEntry(key, initialData) {
71
+ let entry = queryCache.get(key);
72
+ if (!entry) {
73
+ entry = {
74
+ data: initialData,
75
+ error: void 0,
76
+ dataUpdatedAt: initialData !== void 0 ? Date.now() : 0,
77
+ subscribers: 0,
78
+ gcTimer: null,
79
+ promise: null,
80
+ listeners: /* @__PURE__ */ new Set(),
81
+ refetchers: /* @__PURE__ */ new Set()
82
+ };
83
+ queryCache.set(key, entry);
84
+ }
85
+ return entry;
86
+ }
87
+ function sbQuery(key, fetcher, options = {}) {
88
+ const {
89
+ staleTime = 0,
90
+ cacheTime = 3e5,
91
+ enabled = true,
92
+ retry: retryOptions,
93
+ initialData,
94
+ refetchInterval,
95
+ refetchOnWindowFocus = false,
96
+ refetchOnReconnect = false,
97
+ onSuccess,
98
+ onError,
99
+ onSettled
100
+ } = options;
101
+ const resolveKey = typeof key === "function" ? key : () => key;
102
+ const [data, setData] = sbSignal(initialData);
103
+ const [isFetching, setIsFetching] = sbSignal(false);
104
+ const [error, setError] = sbSignal(void 0);
105
+ let abortController = null;
106
+ let disposed = false;
107
+ let currentKey = null;
108
+ let intervalTimer = null;
109
+ const loading = sbDerived(() => isFetching() && data() === void 0);
110
+ const isStale = sbDerived(() => {
111
+ data();
112
+ if (!currentKey) return true;
113
+ const entry = queryCache.get(currentKey);
114
+ if (!entry || entry.dataUpdatedAt === 0) return true;
115
+ return Date.now() - entry.dataUpdatedAt >= staleTime;
116
+ });
117
+ async function doFetch() {
118
+ if (disposed || !currentKey || !enabled) return;
119
+ const key2 = currentKey;
120
+ const entry = queryCache.get(key2);
121
+ if (!entry) return;
122
+ if (entry.promise) {
123
+ setIsFetching(true);
124
+ return;
125
+ }
126
+ abortController?.abort();
127
+ abortController = new AbortController();
128
+ const signal = abortController.signal;
129
+ setIsFetching(true);
130
+ const promise = withRetry(() => fetcher({ signal, key: key2 }), retryOptions, void 0, signal);
131
+ entry.promise = promise;
132
+ try {
133
+ const result = await promise;
134
+ entry.promise = null;
135
+ if (disposed || currentKey !== key2) return;
136
+ entry.data = result;
137
+ entry.dataUpdatedAt = Date.now();
138
+ entry.error = void 0;
139
+ sbBatch(() => {
140
+ setData(result);
141
+ setIsFetching(false);
142
+ setError(void 0);
143
+ });
144
+ for (const listener of entry.listeners) listener();
145
+ onSuccess?.(result);
146
+ } catch (err) {
147
+ entry.promise = null;
148
+ if (disposed || currentKey !== key2) return;
149
+ if (err instanceof DOMException && err.name === "AbortError") return;
150
+ const errorObj = err instanceof Error ? err : new Error(String(err));
151
+ entry.error = errorObj;
152
+ sbBatch(() => {
153
+ setError(errorObj);
154
+ setIsFetching(false);
155
+ });
156
+ for (const listener of entry.listeners) listener();
157
+ onError?.(errorObj);
158
+ } finally {
159
+ if (!disposed && currentKey === key2) onSettled?.();
160
+ }
161
+ }
162
+ function onCacheUpdate() {
163
+ if (disposed || !currentKey) return;
164
+ const entry = queryCache.get(currentKey);
165
+ if (!entry) return;
166
+ sbBatch(() => {
167
+ if (entry.data !== void 0) setData(entry.data);
168
+ setError(entry.error);
169
+ if (!entry.promise) setIsFetching(false);
170
+ });
171
+ }
172
+ const effectCleanup = sbEffect(() => {
173
+ const key2 = resolveKey();
174
+ if (currentKey !== null && currentKey !== key2) {
175
+ const oldEntry = queryCache.get(currentKey);
176
+ if (oldEntry) {
177
+ oldEntry.listeners.delete(onCacheUpdate);
178
+ oldEntry.refetchers.delete(doFetch);
179
+ oldEntry.subscribers--;
180
+ if (oldEntry.subscribers <= 0 && cacheTime >= 0) {
181
+ const oldKey = currentKey;
182
+ oldEntry.gcTimer = setTimeout(() => queryCache.delete(oldKey), cacheTime);
183
+ }
184
+ }
185
+ }
186
+ currentKey = key2;
187
+ const entry = getOrCreateEntry(key2, initialData);
188
+ entry.subscribers++;
189
+ if (entry.gcTimer !== null) {
190
+ clearTimeout(entry.gcTimer);
191
+ entry.gcTimer = null;
192
+ }
193
+ entry.listeners.add(onCacheUpdate);
194
+ entry.refetchers.add(doFetch);
195
+ if (entry.data !== void 0) {
196
+ sbBatch(() => {
197
+ setData(entry.data);
198
+ setError(entry.error);
199
+ });
200
+ }
201
+ const isDataStale = entry.dataUpdatedAt === 0 || Date.now() - entry.dataUpdatedAt >= staleTime;
202
+ if (enabled && (entry.data === void 0 || isDataStale)) {
203
+ doFetch();
204
+ }
205
+ });
206
+ if (refetchInterval && refetchInterval > 0) {
207
+ intervalTimer = setInterval(() => {
208
+ if (!disposed && currentKey && enabled) doFetch();
209
+ }, refetchInterval);
210
+ }
211
+ let focusHandler = null;
212
+ let onlineHandler = null;
213
+ if (typeof globalThis !== "undefined" && typeof globalThis.addEventListener === "function") {
214
+ if (refetchOnWindowFocus) {
215
+ focusHandler = () => {
216
+ if (!disposed && currentKey && enabled) doFetch();
217
+ };
218
+ globalThis.addEventListener("focus", focusHandler);
219
+ }
220
+ if (refetchOnReconnect) {
221
+ onlineHandler = () => {
222
+ if (!disposed && currentKey && enabled) doFetch();
223
+ };
224
+ globalThis.addEventListener("online", onlineHandler);
225
+ }
226
+ }
227
+ function dispose() {
228
+ disposed = true;
229
+ abortController?.abort();
230
+ effectCleanup();
231
+ if (intervalTimer) clearInterval(intervalTimer);
232
+ if (currentKey) {
233
+ const entry = queryCache.get(currentKey);
234
+ if (entry) {
235
+ entry.listeners.delete(onCacheUpdate);
236
+ entry.refetchers.delete(doFetch);
237
+ entry.subscribers--;
238
+ if (entry.subscribers <= 0 && cacheTime >= 0) {
239
+ const key2 = currentKey;
240
+ entry.gcTimer = setTimeout(() => queryCache.delete(key2), cacheTime);
241
+ }
242
+ }
243
+ }
244
+ if (focusHandler) globalThis.removeEventListener("focus", focusHandler);
245
+ if (onlineHandler) globalThis.removeEventListener("online", onlineHandler);
246
+ }
247
+ return {
248
+ data,
249
+ loading,
250
+ fetching: isFetching,
251
+ error,
252
+ isStale,
253
+ refetch: doFetch,
254
+ dispose
255
+ };
256
+ }
257
+ function invalidateQueries(keyOrPredicate) {
258
+ const predicate = typeof keyOrPredicate === "function" ? keyOrPredicate : (k) => k === keyOrPredicate;
259
+ for (const [key, entry] of queryCache.entries()) {
260
+ if (predicate(key)) {
261
+ entry.dataUpdatedAt = 0;
262
+ for (const refetcher of entry.refetchers) refetcher();
263
+ }
264
+ }
265
+ }
266
+ function getQueryData(key) {
267
+ return queryCache.get(key)?.data;
268
+ }
269
+ function setQueryData(key, data) {
270
+ const entry = queryCache.get(key);
271
+ if (!entry) return;
272
+ const newData = typeof data === "function" ? data(entry.data) : data;
273
+ entry.data = newData;
274
+ entry.dataUpdatedAt = Date.now();
275
+ for (const listener of entry.listeners) listener();
276
+ }
277
+ function clearQueryCache() {
278
+ for (const entry of queryCache.values()) {
279
+ if (entry.gcTimer) clearTimeout(entry.gcTimer);
280
+ }
281
+ queryCache.clear();
282
+ }
283
+
284
+ // src/core/data/sbMutation.ts
285
+ function sbMutation(mutationFn, options = {}) {
286
+ const [data, setData] = sbSignal(void 0);
287
+ const [loading, setLoading] = sbSignal(false);
288
+ const [error, setError] = sbSignal(void 0);
289
+ const [status, setStatus] = sbSignal("idle");
290
+ const isSuccess = sbDerived(() => status() === "success");
291
+ const isIdle = sbDerived(() => status() === "idle");
292
+ async function execute(variables) {
293
+ let context;
294
+ sbBatch(() => {
295
+ setLoading(true);
296
+ setError(void 0);
297
+ setStatus("loading");
298
+ });
299
+ try {
300
+ if (options.onMutate) {
301
+ context = await options.onMutate(variables);
302
+ }
303
+ const result = await withRetry(() => mutationFn(variables), options.retry);
304
+ sbBatch(() => {
305
+ setData(result);
306
+ setLoading(false);
307
+ setStatus("success");
308
+ });
309
+ options.onSuccess?.(result, variables, context);
310
+ options.onSettled?.(result, void 0, variables, context);
311
+ return result;
312
+ } catch (err) {
313
+ const errorObj = err instanceof Error ? err : new Error(String(err));
314
+ sbBatch(() => {
315
+ setError(errorObj);
316
+ setLoading(false);
317
+ setStatus("error");
318
+ });
319
+ options.onError?.(errorObj, variables, context);
320
+ options.onSettled?.(void 0, errorObj, variables, context);
321
+ throw errorObj;
322
+ }
323
+ }
324
+ function reset() {
325
+ sbBatch(() => {
326
+ setData(void 0);
327
+ setError(void 0);
328
+ setLoading(false);
329
+ setStatus("idle");
330
+ });
331
+ }
332
+ return {
333
+ data,
334
+ loading,
335
+ error,
336
+ isSuccess,
337
+ isIdle,
338
+ mutate: (variables) => {
339
+ execute(variables).catch(() => {
340
+ });
341
+ },
342
+ mutateAsync: execute,
343
+ reset
344
+ };
345
+ }
346
+
347
+ // src/core/data/sbInfiniteQuery.ts
348
+ function sbInfiniteQuery(key, fetcher, options) {
349
+ const {
350
+ getNextPageParam,
351
+ getPreviousPageParam,
352
+ initialPageParam = 0,
353
+ enabled = true,
354
+ retry: retryOptions,
355
+ onSuccess,
356
+ onError
357
+ } = options;
358
+ const resolveKey = typeof key === "function" ? key : () => key;
359
+ const [pages, setPages] = sbSignal([]);
360
+ const [isFetching, setIsFetching] = sbSignal(false);
361
+ const [isFetchingNext, setIsFetchingNext] = sbSignal(false);
362
+ const [isFetchingPrev, setIsFetchingPrev] = sbSignal(false);
363
+ const [error, setError] = sbSignal(void 0);
364
+ const [nextPageParam, setNextPageParam] = sbSignal(initialPageParam);
365
+ const [prevPageParam, setPrevPageParam] = sbSignal(void 0);
366
+ const data = sbDerived(() => {
367
+ const p = pages();
368
+ return p.length > 0 ? p : void 0;
369
+ });
370
+ const loading = sbDerived(() => isFetching() && pages().length === 0);
371
+ const hasNextPage = sbDerived(() => nextPageParam() !== void 0);
372
+ const hasPreviousPage = sbDerived(() => prevPageParam() !== void 0);
373
+ let abortController = null;
374
+ let disposed = false;
375
+ async function fetchPage(pageParam, direction) {
376
+ if (disposed) return;
377
+ abortController?.abort();
378
+ abortController = new AbortController();
379
+ const signal = abortController.signal;
380
+ sbBatch(() => {
381
+ setIsFetching(true);
382
+ if (direction === "next") setIsFetchingNext(true);
383
+ if (direction === "prev") setIsFetchingPrev(true);
384
+ setError(void 0);
385
+ });
386
+ try {
387
+ const page = await withRetry(() => fetcher({ signal, pageParam }), retryOptions, void 0, signal);
388
+ if (disposed) return;
389
+ const currentPages = pages();
390
+ let newPages;
391
+ if (direction === "prev") {
392
+ newPages = [page, ...currentPages];
393
+ } else {
394
+ newPages = [...currentPages, page];
395
+ }
396
+ const nextParam = getNextPageParam(page, newPages);
397
+ const prevParam = getPreviousPageParam?.(newPages[0], newPages);
398
+ sbBatch(() => {
399
+ setPages(newPages);
400
+ setNextPageParam(nextParam);
401
+ setPrevPageParam(prevParam);
402
+ setIsFetching(false);
403
+ setIsFetchingNext(false);
404
+ setIsFetchingPrev(false);
405
+ });
406
+ onSuccess?.(newPages);
407
+ } catch (err) {
408
+ if (disposed) return;
409
+ if (err instanceof DOMException && err.name === "AbortError") return;
410
+ const errorObj = err instanceof Error ? err : new Error(String(err));
411
+ sbBatch(() => {
412
+ setError(errorObj);
413
+ setIsFetching(false);
414
+ setIsFetchingNext(false);
415
+ setIsFetchingPrev(false);
416
+ });
417
+ onError?.(errorObj);
418
+ }
419
+ }
420
+ const effectCleanup = sbEffect(() => {
421
+ resolveKey();
422
+ if (enabled) {
423
+ setPages([]);
424
+ setNextPageParam(initialPageParam);
425
+ setPrevPageParam(void 0);
426
+ fetchPage(initialPageParam, "initial");
427
+ }
428
+ });
429
+ function fetchNextPage() {
430
+ const param = nextPageParam();
431
+ if (param === void 0) return Promise.resolve();
432
+ return fetchPage(param, "next");
433
+ }
434
+ function fetchPreviousPage() {
435
+ const param = prevPageParam();
436
+ if (param === void 0) return Promise.resolve();
437
+ return fetchPage(param, "prev");
438
+ }
439
+ async function refetch() {
440
+ sbBatch(() => {
441
+ setPages([]);
442
+ setNextPageParam(initialPageParam);
443
+ setPrevPageParam(void 0);
444
+ });
445
+ await fetchPage(initialPageParam, "initial");
446
+ }
447
+ function dispose() {
448
+ disposed = true;
449
+ abortController?.abort();
450
+ effectCleanup();
451
+ }
452
+ return {
453
+ data,
454
+ pages,
455
+ loading,
456
+ fetching: isFetching,
457
+ fetchingNextPage: isFetchingNext,
458
+ fetchingPreviousPage: isFetchingPrev,
459
+ error,
460
+ hasNextPage,
461
+ hasPreviousPage,
462
+ fetchNextPage,
463
+ fetchPreviousPage,
464
+ refetch,
465
+ dispose
466
+ };
467
+ }
468
+
469
+ // src/core/data/sbPrevious.ts
470
+ function sbPrevious(getter) {
471
+ const [previous, setPrevious] = sbSignal(void 0);
472
+ let current = getter();
473
+ sbEffect(() => {
474
+ const next = getter();
475
+ if (!Object.is(next, current)) {
476
+ setPrevious(current);
477
+ current = next;
478
+ }
479
+ });
480
+ return previous;
481
+ }
482
+
483
+ // src/core/data/sbDebounce.ts
484
+ function sbDebounce(getter, delay) {
485
+ const [debounced, setDebounced] = sbSignal(getter());
486
+ let timer = null;
487
+ sbEffect(() => {
488
+ const value = getter();
489
+ if (timer !== null) clearTimeout(timer);
490
+ timer = setTimeout(() => {
491
+ setDebounced(value);
492
+ timer = null;
493
+ }, delay);
494
+ });
495
+ return debounced;
496
+ }
497
+
498
+ // src/core/data/sbThrottle.ts
499
+ function sbThrottle(getter, interval) {
500
+ const [throttled, setThrottled] = sbSignal(getter());
501
+ let cooldown = false;
502
+ let pending = null;
503
+ let lastEmitted = getter();
504
+ sbEffect(() => {
505
+ const value = getter();
506
+ if (!cooldown) {
507
+ if (!Object.is(value, lastEmitted)) {
508
+ setThrottled(value);
509
+ lastEmitted = value;
510
+ cooldown = true;
511
+ pending = null;
512
+ setTimeout(() => {
513
+ cooldown = false;
514
+ if (pending !== null) {
515
+ const trailingValue = pending.value;
516
+ pending = null;
517
+ setThrottled(trailingValue);
518
+ lastEmitted = trailingValue;
519
+ }
520
+ }, interval);
521
+ }
522
+ } else {
523
+ pending = { value };
524
+ }
525
+ });
526
+ return throttled;
527
+ }
528
+
529
+ // src/core/data/createResource.ts
530
+ function createResource(sourceOrFetcher, fetcherOrOptions, maybeOptions) {
531
+ let source = null;
532
+ let fetcher;
533
+ let options;
534
+ if (typeof fetcherOrOptions === "function") {
535
+ source = sourceOrFetcher;
536
+ fetcher = fetcherOrOptions;
537
+ options = maybeOptions ?? {};
538
+ } else {
539
+ const rawFetcher = sourceOrFetcher;
540
+ fetcher = (_source, info) => rawFetcher(info);
541
+ options = fetcherOrOptions ?? {};
542
+ }
543
+ const [data, setData] = sbSignal(options.initialValue);
544
+ const [loading, setLoading] = sbSignal(false);
545
+ const [error, setError] = sbSignal(void 0);
546
+ let currentData = options.initialValue;
547
+ let abortController = null;
548
+ let disposed = false;
549
+ let effectCleanup = null;
550
+ let fetchVersion = 0;
551
+ async function doFetch(sourceValue) {
552
+ if (disposed) return;
553
+ abortController?.abort();
554
+ abortController = new AbortController();
555
+ const version = ++fetchVersion;
556
+ const signal = abortController.signal;
557
+ const prev = currentData;
558
+ sbBatch(() => {
559
+ setLoading(true);
560
+ setError(void 0);
561
+ });
562
+ options.onStart?.();
563
+ try {
564
+ const result = await withRetry(() => fetcher(sourceValue, { signal, prev }), options.retry, void 0, signal);
565
+ if (version !== fetchVersion || disposed) return;
566
+ currentData = result;
567
+ sbBatch(() => {
568
+ setData(result);
569
+ setLoading(false);
570
+ });
571
+ options.onSuccess?.(result);
572
+ } catch (err) {
573
+ if (version !== fetchVersion || disposed) return;
574
+ if (err instanceof DOMException && err.name === "AbortError") return;
575
+ const errorObj = err instanceof Error ? err : new Error(String(err));
576
+ sbBatch(() => {
577
+ setError(errorObj);
578
+ setLoading(false);
579
+ });
580
+ options.onError?.(errorObj);
581
+ } finally {
582
+ if (version === fetchVersion) {
583
+ options.onSettled?.();
584
+ }
585
+ }
586
+ }
587
+ if (source) {
588
+ effectCleanup = sbEffect(() => {
589
+ const sourceValue = source();
590
+ doFetch(sourceValue);
591
+ });
592
+ } else if (options.immediate !== false) {
593
+ doFetch(void 0);
594
+ }
595
+ return {
596
+ data,
597
+ loading,
598
+ error,
599
+ refetch: () => doFetch(source ? source() : void 0),
600
+ mutate: (value) => {
601
+ const newValue = typeof value === "function" ? value(currentData) : value;
602
+ currentData = newValue;
603
+ setData(newValue);
604
+ },
605
+ abort: () => abortController?.abort(),
606
+ dispose: () => {
607
+ disposed = true;
608
+ abortController?.abort();
609
+ effectCleanup?.();
610
+ }
611
+ };
612
+ }
613
+
614
+ // src/core/data/offlineStore.ts
615
+ function openDB(name, version, keyPath) {
616
+ return new Promise((resolve, reject) => {
617
+ const request = indexedDB.open(name, version);
618
+ request.onerror = () => reject(request.error);
619
+ request.onsuccess = () => resolve(request.result);
620
+ request.onupgradeneeded = () => {
621
+ const db = request.result;
622
+ if (!db.objectStoreNames.contains("items")) {
623
+ db.createObjectStore("items", { keyPath });
624
+ }
625
+ if (!db.objectStoreNames.contains("_changes")) {
626
+ const changeStore = db.createObjectStore("_changes", { autoIncrement: true });
627
+ changeStore.createIndex("timestamp", "timestamp");
628
+ }
629
+ if (!db.objectStoreNames.contains("_meta")) {
630
+ db.createObjectStore("_meta");
631
+ }
632
+ };
633
+ });
634
+ }
635
+ function idbGetAll(db, store) {
636
+ return new Promise((resolve, reject) => {
637
+ const tx = db.transaction(store, "readonly");
638
+ const req = tx.objectStore(store).getAll();
639
+ req.onsuccess = () => resolve(req.result);
640
+ req.onerror = () => reject(req.error);
641
+ });
642
+ }
643
+ function idbGet(db, store, key) {
644
+ return new Promise((resolve, reject) => {
645
+ const tx = db.transaction(store, "readonly");
646
+ const req = tx.objectStore(store).get(key);
647
+ req.onsuccess = () => resolve(req.result);
648
+ req.onerror = () => reject(req.error);
649
+ });
650
+ }
651
+ function idbPut(db, store, item) {
652
+ return new Promise((resolve, reject) => {
653
+ const tx = db.transaction(store, "readwrite");
654
+ tx.objectStore(store).put(item);
655
+ tx.oncomplete = () => resolve();
656
+ tx.onerror = () => reject(tx.error);
657
+ });
658
+ }
659
+ function idbDelete(db, store, key) {
660
+ return new Promise((resolve, reject) => {
661
+ const tx = db.transaction(store, "readwrite");
662
+ tx.objectStore(store).delete(key);
663
+ tx.oncomplete = () => resolve();
664
+ tx.onerror = () => reject(tx.error);
665
+ });
666
+ }
667
+ function idbClear(db, store) {
668
+ return new Promise((resolve, reject) => {
669
+ const tx = db.transaction(store, "readwrite");
670
+ tx.objectStore(store).clear();
671
+ tx.oncomplete = () => resolve();
672
+ tx.onerror = () => reject(tx.error);
673
+ });
674
+ }
675
+ async function sbOfflineStore(options) {
676
+ const { name, version = 1, keyPath = "id", autoSync = true } = options;
677
+ const db = await openDB(name, version, keyPath);
678
+ const initialData = await idbGetAll(db, "items");
679
+ const initialChanges = await idbGetAll(db, "_changes");
680
+ const savedLastSync = await idbGet(db, "_meta", "lastSynced");
681
+ const [data, setData] = sbSignal(initialData);
682
+ const [isSyncing, setIsSyncing] = sbSignal(false);
683
+ const [lastSynced, setLastSynced] = sbSignal(savedLastSync ?? null);
684
+ const [pendingCount, setPendingCount] = sbSignal(initialChanges.length);
685
+ let adapter = options.adapter;
686
+ async function refreshData() {
687
+ const items = await idbGetAll(db, "items");
688
+ setData(items);
689
+ const changes = await idbGetAll(db, "_changes");
690
+ setPendingCount(changes.length);
691
+ }
692
+ async function put(item) {
693
+ await idbPut(db, "items", item);
694
+ await idbPut(db, "_changes", { type: "put", item, timestamp: Date.now() });
695
+ await refreshData();
696
+ }
697
+ async function remove(key) {
698
+ const existing = await idbGet(db, "items", key);
699
+ if (existing) {
700
+ await idbDelete(db, "items", key);
701
+ await idbPut(db, "_changes", { type: "delete", item: existing, timestamp: Date.now() });
702
+ await refreshData();
703
+ }
704
+ }
705
+ async function get(key) {
706
+ return idbGet(db, "items", key);
707
+ }
708
+ function query(filter) {
709
+ return data().filter(filter);
710
+ }
711
+ async function sync() {
712
+ if (!adapter || isSyncing()) return;
713
+ setIsSyncing(true);
714
+ try {
715
+ const changes = await idbGetAll(db, "_changes");
716
+ if (changes.length > 0) {
717
+ const result = await adapter.push(changes);
718
+ if (result.ok) {
719
+ await idbClear(db, "_changes");
720
+ }
721
+ }
722
+ const remoteItems = await adapter.pull(lastSynced());
723
+ for (const item of remoteItems) {
724
+ await idbPut(db, "items", item);
725
+ }
726
+ const now = Date.now();
727
+ await idbPut(db, "_meta", now);
728
+ setLastSynced(now);
729
+ await refreshData();
730
+ } catch {
731
+ } finally {
732
+ setIsSyncing(false);
733
+ }
734
+ }
735
+ function attach(newAdapter) {
736
+ adapter = newAdapter;
737
+ }
738
+ function close() {
739
+ db.close();
740
+ }
741
+ if (autoSync && typeof window !== "undefined") {
742
+ window.addEventListener("online", () => {
743
+ sync();
744
+ });
745
+ }
746
+ return {
747
+ data,
748
+ get,
749
+ put,
750
+ remove,
751
+ query,
752
+ isSyncing,
753
+ lastSynced,
754
+ sync,
755
+ attach,
756
+ pendingCount,
757
+ close
758
+ };
759
+ }
760
+ function createSyncAdapter(config) {
761
+ return config;
762
+ }
763
+
764
+ // src/core/data/routeLoader.ts
765
+ var LoaderContext = createContext(null);
766
+ function executeLoader(loader, context, options) {
767
+ const resource = createResource(({ signal }) => loader(context, { signal }), options);
768
+ LoaderContext.provide(resource);
769
+ return resource;
770
+ }
771
+ function sbLoaderData() {
772
+ const resource = LoaderContext.get();
773
+ if (!resource) {
774
+ throw new Error("sbLoaderData must be used inside a route with a loader");
775
+ }
776
+ return {
777
+ data: resource.data,
778
+ loading: resource.loading,
779
+ error: resource.error
780
+ };
781
+ }
782
+ async function preloadRoute(route, context) {
783
+ if (!route.loader) return void 0;
784
+ const controller = new AbortController();
785
+ return route.loader(context, { signal: controller.signal });
786
+ }
787
+
788
+ // src/core/ui/sbSocket.ts
789
+ function sbSocket(url, options) {
790
+ const autoReconnect = options?.autoReconnect ?? false;
791
+ const reconnectDelay = options?.reconnectDelay ?? 1e3;
792
+ const maxReconnects = options?.maxReconnects ?? Infinity;
793
+ const heartbeat = options?.heartbeat;
794
+ const protocols = options?.protocols;
795
+ const [data, setData] = sbSignal(null);
796
+ const [status, setStatus] = sbSignal("closed");
797
+ let ws = null;
798
+ let reconnectCount = 0;
799
+ let reconnectTimer = null;
800
+ let heartbeatTimer = null;
801
+ let disposed = false;
802
+ function getUrl() {
803
+ return typeof url === "function" ? url() : url;
804
+ }
805
+ function connect() {
806
+ if (disposed) return;
807
+ setStatus("connecting");
808
+ ws = new WebSocket(getUrl(), protocols);
809
+ ws.onopen = () => {
810
+ setStatus("open");
811
+ reconnectCount = 0;
812
+ startHeartbeat();
813
+ };
814
+ ws.onmessage = (event) => {
815
+ setData(event.data);
816
+ };
817
+ ws.onclose = () => {
818
+ setStatus("closed");
819
+ stopHeartbeat();
820
+ if (autoReconnect && !disposed && reconnectCount < maxReconnects) {
821
+ reconnectCount++;
822
+ reconnectTimer = setTimeout(() => {
823
+ connect();
824
+ }, reconnectDelay);
825
+ }
826
+ };
827
+ ws.onerror = () => {
828
+ };
829
+ }
830
+ function startHeartbeat() {
831
+ if (!heartbeat) return;
832
+ stopHeartbeat();
833
+ heartbeatTimer = setInterval(() => {
834
+ if (ws && ws.readyState === WebSocket.OPEN) {
835
+ ws.send(heartbeat.message);
836
+ }
837
+ }, heartbeat.interval);
838
+ }
839
+ function stopHeartbeat() {
840
+ if (heartbeatTimer !== null) {
841
+ clearInterval(heartbeatTimer);
842
+ heartbeatTimer = null;
843
+ }
844
+ }
845
+ function send(msg) {
846
+ if (ws && ws.readyState === WebSocket.OPEN) {
847
+ ws.send(msg);
848
+ }
849
+ }
850
+ function close() {
851
+ if (reconnectTimer !== null) {
852
+ clearTimeout(reconnectTimer);
853
+ reconnectTimer = null;
854
+ }
855
+ stopHeartbeat();
856
+ if (ws) {
857
+ setStatus("closing");
858
+ ws.close();
859
+ }
860
+ }
861
+ function dispose() {
862
+ disposed = true;
863
+ close();
864
+ }
865
+ connect();
866
+ return { data, status, send, close, dispose };
867
+ }
868
+
869
+ // src/core/ui/sbStream.ts
870
+ function sbStream(url, options) {
871
+ const autoReconnect = options?.autoReconnect ?? false;
872
+ const [data, setData] = sbSignal(null);
873
+ const [event, setEvent] = sbSignal(null);
874
+ const [status, setStatus] = sbSignal("connecting");
875
+ let source = null;
876
+ let disposed = false;
877
+ let reconnectTimer = null;
878
+ function connect() {
879
+ if (disposed) return;
880
+ setStatus("connecting");
881
+ source = new EventSource(url, {
882
+ withCredentials: options?.withCredentials ?? false
883
+ });
884
+ source.onopen = () => {
885
+ setStatus("open");
886
+ };
887
+ source.onmessage = (evt) => {
888
+ setData(evt.data);
889
+ setEvent(evt.type);
890
+ };
891
+ source.onerror = () => {
892
+ if (source && source.readyState === EventSource.CLOSED) {
893
+ setStatus("closed");
894
+ source = null;
895
+ if (autoReconnect && !disposed) {
896
+ reconnectTimer = setTimeout(() => {
897
+ reconnectTimer = null;
898
+ connect();
899
+ }, 1e3);
900
+ }
901
+ }
902
+ };
903
+ }
904
+ function close() {
905
+ if (reconnectTimer !== null) {
906
+ clearTimeout(reconnectTimer);
907
+ reconnectTimer = null;
908
+ }
909
+ if (source) {
910
+ source.close();
911
+ setStatus("closed");
912
+ source = null;
913
+ }
914
+ }
915
+ function dispose() {
916
+ disposed = true;
917
+ close();
918
+ }
919
+ connect();
920
+ return { data, event, status, close, dispose };
921
+ }
922
+
923
+ export {
924
+ calculateDelay,
925
+ withRetry,
926
+ sbQuery,
927
+ invalidateQueries,
928
+ getQueryData,
929
+ setQueryData,
930
+ clearQueryCache,
931
+ sbMutation,
932
+ sbInfiniteQuery,
933
+ sbPrevious,
934
+ sbDebounce,
935
+ sbThrottle,
936
+ createResource,
937
+ sbOfflineStore,
938
+ createSyncAdapter,
939
+ executeLoader,
940
+ sbLoaderData,
941
+ preloadRoute,
942
+ sbSocket,
943
+ sbStream
944
+ };