keits 0.0.3 → 0.0.4

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 (37) hide show
  1. package/dist/dev/api.js +270 -0
  2. package/dist/dev/forms.js +328 -0
  3. package/dist/dev/index.js +8 -299
  4. package/dist/dev/router.js +3 -1
  5. package/dist/dev/shared/computed-3UO-KiH1.js +40 -0
  6. package/dist/dev/shared/{element-C4DfKtZW.js → element-CQPqagu1.js} +5 -38
  7. package/dist/dev/shared/owner-Dmge21OW.js +62 -0
  8. package/dist/dev/shared/render-effect-kOGnLNbL.js +26 -0
  9. package/dist/dev/shared/resource-CbXVATvq.js +154 -0
  10. package/dist/dev/shared/signal-BOXkY-Iv.js +24 -0
  11. package/dist/dev/shared/store-BzUN1qE5.js +148 -0
  12. package/dist/dev/svg.js +1 -1
  13. package/dist/prod/api.js +210 -0
  14. package/dist/prod/forms.js +237 -0
  15. package/dist/prod/index.js +107 -316
  16. package/dist/prod/router.js +24 -22
  17. package/dist/prod/shared/computed-CAPpOLCY.js +33 -0
  18. package/dist/prod/shared/element-DGyQ01Vs.js +164 -0
  19. package/dist/prod/shared/owner-6BFHIPR9.js +59 -0
  20. package/dist/prod/shared/render-effect-DjaUT3Cn.js +23 -0
  21. package/dist/prod/shared/resource-CNC2DBO3.js +109 -0
  22. package/dist/prod/shared/signal-DGrl3r9T.js +16 -0
  23. package/dist/prod/shared/store-Dcscpk_h.js +109 -0
  24. package/dist/prod/svg.js +1 -1
  25. package/dist/types/api/cache.d.ts +16 -0
  26. package/dist/types/api/client.d.ts +42 -0
  27. package/dist/types/api/error.d.ts +16 -0
  28. package/dist/types/api/index.d.ts +3 -0
  29. package/dist/types/api/url.d.ts +5 -0
  30. package/dist/types/forms/form.d.ts +55 -0
  31. package/dist/types/forms/index.d.ts +2 -0
  32. package/dist/types/forms/paths.d.ts +6 -0
  33. package/dist/types/forms/types.d.ts +38 -0
  34. package/package.json +16 -2
  35. package/dist/dev/shared/owner-DlUsXKXE.js +0 -122
  36. package/dist/prod/shared/element-DUtXb3px.js +0 -187
  37. package/dist/prod/shared/owner-DgJNPM0P.js +0 -104
package/dist/dev/index.js CHANGED
@@ -1,180 +1,13 @@
1
- import { a as onMount, c as computed, i as onCleanup, l as signal, n as createRoot, o as renderStatic, r as getOwner, s as runWithOwner, t as catchError } from "./shared/owner-DlUsXKXE.js";
2
- import { a as renderEffect, i as insert, n as html, o as hooks, s as effect } from "./shared/element-C4DfKtZW.js";
3
- import { a as deferMounts, c as handleError, d as notify, h as untrack, i as batch, l as isStaticRender, m as track, p as runWithOwner$1, r as adopt, s as getOwner$1, t as addCleanup, u as isTracking } from "./shared/graph-D6-x_CWC.js";
1
+ import { t as signal } from "./shared/signal-BOXkY-Iv.js";
2
+ import { n as effect, t as renderEffect } from "./shared/render-effect-kOGnLNbL.js";
3
+ import { t as computed } from "./shared/computed-3UO-KiH1.js";
4
+ import { a as deferMounts, h as untrack, i as batch, l as isStaticRender, t as addCleanup } from "./shared/graph-D6-x_CWC.js";
5
+ import { a as onMount, i as onCleanup, n as createRoot, o as renderStatic, r as getOwner, s as runWithOwner, t as catchError } from "./shared/owner-Dmge21OW.js";
6
+ import { a as useContext, i as provide, n as SuspenseContext, r as createContext, t as resource } from "./shared/resource-CbXVATvq.js";
7
+ import { n as store, r as unwrap, t as reconcile } from "./shared/store-BzUN1qE5.js";
8
+ import { a as hooks, i as insert, n as html } from "./shared/element-CQPqagu1.js";
4
9
  // src/core/env.ts
5
10
  var isServer = typeof window === "undefined";
6
- // src/core/context.ts
7
- function createContext(defaultValue, options) {
8
- const context = { defaultValue };
9
- if (options?.name !== void 0) context.name = options.name;
10
- return context;
11
- }
12
- function provide(context, value, fn) {
13
- const provider = {
14
- kind: 3,
15
- owner: null,
16
- children: null,
17
- cleanups: null,
18
- disposed: false,
19
- handler: null,
20
- context: {
21
- key: context,
22
- value
23
- }
24
- };
25
- adopt(provider);
26
- return runWithOwner$1(provider, fn);
27
- }
28
- function useContext(context) {
29
- for (let o = getOwner$1(); o !== null; o = o.owner) if (o.kind === 3 && o.context?.key === context) return o.context.value;
30
- return context.defaultValue;
31
- }
32
- // src/core/store.ts
33
- var RAW = Symbol("raw");
34
- var SHAPE = Symbol("shape");
35
- var proxies = /* @__PURE__ */ new WeakMap();
36
- var nodeMaps = /* @__PURE__ */ new WeakMap();
37
- var writing = 0;
38
- function store(initial, options) {
39
- const state = wrap(initial);
40
- const name = options?.name;
41
- const setState = (fn) => {
42
- writing++;
43
- try {
44
- untrack(() => batch(() => fn(state)));
45
- } finally {
46
- writing--;
47
- }
48
- };
49
- if (name !== void 0) storeNames.set(unwrap(state), name);
50
- return [state, setState];
51
- }
52
- var storeNames = /* @__PURE__ */ new WeakMap();
53
- function unwrap(value) {
54
- if (value !== null && typeof value === "object") {
55
- const raw = value[RAW];
56
- if (raw !== void 0) return raw;
57
- }
58
- return value;
59
- }
60
- function isWrappable(value) {
61
- if (value === null || typeof value !== "object") return false;
62
- if (Array.isArray(value)) return true;
63
- const proto = Object.getPrototypeOf(value);
64
- return proto === Object.prototype || proto === null;
65
- }
66
- function wrap(value) {
67
- if (!isWrappable(value)) return value;
68
- let proxy = proxies.get(value);
69
- if (proxy === void 0) {
70
- proxy = new Proxy(value, handler);
71
- proxies.set(value, proxy);
72
- }
73
- return proxy;
74
- }
75
- function getNode(target, key) {
76
- let nodes = nodeMaps.get(target);
77
- if (nodes === void 0) nodeMaps.set(target, nodes = /* @__PURE__ */ new Map());
78
- let node = nodes.get(key);
79
- if (node === void 0) nodes.set(key, node = {
80
- kind: 0,
81
- observers: [],
82
- seenBy: 0
83
- });
84
- return node;
85
- }
86
- function notifyKey(target, key) {
87
- const node = nodeMaps.get(target)?.get(key);
88
- if (node !== void 0) notify(node);
89
- }
90
- function readOnlyError(target) {
91
- const name = storeNames.get(target);
92
- return /* @__PURE__ */ new Error(`[keits] Store${name ? ` "${name}"` : ""} is read-only here. Change it inside setState(draft => { ... }).`);
93
- }
94
- var handler = {
95
- get(target, key) {
96
- if (key === RAW) return target;
97
- const value = target[key];
98
- if (isTracking() && (Object.hasOwn(target, key) || !(key in target))) track(getNode(target, key));
99
- return wrap(value);
100
- },
101
- has(target, key) {
102
- if (isTracking()) track(getNode(target, key));
103
- return key in target;
104
- },
105
- ownKeys(target) {
106
- if (isTracking()) track(getNode(target, SHAPE));
107
- return Reflect.ownKeys(target);
108
- },
109
- set(target, key, value) {
110
- if (writing === 0) {
111
- const error = readOnlyError(target);
112
- if (error) throw error;
113
- return false;
114
- }
115
- const record = target;
116
- const next = unwrap(value);
117
- const hadKey = Object.hasOwn(target, key);
118
- const prev = record[key];
119
- const isArray = Array.isArray(target);
120
- const prevLength = isArray ? target.length : 0;
121
- if (hadKey && Object.is(prev, next)) return true;
122
- record[key] = next;
123
- notifyKey(target, key);
124
- if (!hadKey) notifyKey(target, SHAPE);
125
- if (isArray) {
126
- const length = target.length;
127
- if (length !== prevLength) {
128
- if (key !== "length") notifyKey(target, "length");
129
- notifyKey(target, SHAPE);
130
- for (let i = length; i < prevLength; i++) notifyKey(target, String(i));
131
- }
132
- }
133
- return true;
134
- },
135
- deleteProperty(target, key) {
136
- if (writing === 0) {
137
- const error = readOnlyError(target);
138
- if (error) throw error;
139
- return false;
140
- }
141
- if (!Object.hasOwn(target, key)) return true;
142
- delete target[key];
143
- notifyKey(target, key);
144
- notifyKey(target, SHAPE);
145
- return true;
146
- }
147
- };
148
- function reconcile(target, source, options = {}) {
149
- if (writing === 0) throw new Error("[keits] reconcile() must be called inside setState.");
150
- reconcileInto(wrap(unwrap(target)), unwrap(source), options.key);
151
- }
152
- function reconcileInto(target, source, key) {
153
- const raw = unwrap(target);
154
- const draft = target;
155
- if (Array.isArray(raw) && Array.isArray(source)) {
156
- const byKey = /* @__PURE__ */ new Map();
157
- if (key !== void 0) {
158
- for (const item of raw) if (isWrappable(item)) byKey.set(item[key], item);
159
- }
160
- for (let i = 0; i < source.length; i++) {
161
- const next = source[i];
162
- const existing = key !== void 0 && isWrappable(next) ? byKey.get(next[key]) : raw[i];
163
- draft[i] = matchAndUpdate(existing, next, key);
164
- }
165
- if (raw.length !== source.length) draft.length = source.length;
166
- return;
167
- }
168
- for (const k of Object.keys(source)) draft[k] = matchAndUpdate(raw[k], source[k], key);
169
- for (const k of Object.keys(raw)) if (!Object.hasOwn(source, k)) delete draft[k];
170
- }
171
- function matchAndUpdate(existing, next, key) {
172
- if (isWrappable(existing) && isWrappable(next) && Array.isArray(existing) === Array.isArray(next)) {
173
- reconcileInto(wrap(existing), next, key);
174
- return existing;
175
- }
176
- return next;
177
- }
178
11
  // src/dom/tags.ts
179
12
  var $a = /* @__PURE__ */ html("a");
180
13
  var $abbr = /* @__PURE__ */ html("abbr");
@@ -525,8 +358,6 @@ function $portal(children, target) {
525
358
  });
526
359
  return null;
527
360
  }
528
- // src/async/suspense-context.ts
529
- var SuspenseContext = createContext(null);
530
361
  // src/flow/suspense.ts
531
362
  function $suspense(children, fallback) {
532
363
  const pendingCount = signal(0);
@@ -579,128 +410,6 @@ function $suspense(children, fallback) {
579
410
  });
580
411
  return container;
581
412
  }
582
- // src/async/resource.ts
583
- function resource(first, second, third) {
584
- const hasSource = typeof second === "function";
585
- const source = hasSource ? first : () => true;
586
- const fetcher = hasSource ? second : (_, info) => first(info);
587
- const options = hasSource ? third : second;
588
- const owner = getOwner$1();
589
- const hasInitial = options !== void 0 && "initialValue" in options;
590
- const value = signal(options?.initialValue);
591
- const state = signal(hasInitial ? "ready" : "unresolved");
592
- const failure = signal(null);
593
- let controller = null;
594
- let version = 0;
595
- let waiters = [];
596
- let currentSource = null;
597
- const settleWaiters = () => {
598
- const list = waiters;
599
- waiters = [];
600
- for (const done of list) done();
601
- };
602
- function load(src, refetching) {
603
- controller?.abort();
604
- controller = null;
605
- const id = ++version;
606
- if (src === null || src === void 0 || src === false) {
607
- currentSource = null;
608
- state.set(untrack(value) === void 0 ? "unresolved" : "ready");
609
- settleWaiters();
610
- return Promise.resolve();
611
- }
612
- currentSource = src;
613
- const abort = new AbortController();
614
- controller = abort;
615
- let result;
616
- try {
617
- result = fetcher(src, {
618
- signal: abort.signal,
619
- refetching,
620
- value: untrack(value)
621
- });
622
- } catch (error) {
623
- fail(error);
624
- return Promise.resolve();
625
- }
626
- if (!isPromiseLike(result)) {
627
- succeed(result);
628
- return Promise.resolve();
629
- }
630
- state.set(untrack(value) === void 0 && untrack(state) !== "ready" ? "pending" : "refreshing");
631
- return Promise.resolve(result).then((data) => {
632
- if (id === version) succeed(data);
633
- }, (error) => {
634
- if (id === version) fail(error);
635
- });
636
- }
637
- function succeed(data) {
638
- controller = null;
639
- batch(() => {
640
- value.set(() => data);
641
- failure.set(null);
642
- state.set("ready");
643
- });
644
- settleWaiters();
645
- }
646
- function fail(error) {
647
- controller = null;
648
- batch(() => {
649
- failure.set({ error });
650
- state.set("errored");
651
- });
652
- settleWaiters();
653
- }
654
- renderEffect(() => {
655
- const src = source();
656
- untrack(() => load(src, false));
657
- });
658
- addCleanup(() => {
659
- version++;
660
- controller?.abort();
661
- controller = null;
662
- });
663
- const read = () => {
664
- const current = state();
665
- if (current === "errored") throw failure().error;
666
- if (current === "pending") {
667
- const boundary = useContext(SuspenseContext);
668
- if (boundary !== null) boundary.wait(resourceApi, (done) => waiters.push(done));
669
- }
670
- return value();
671
- };
672
- const resourceApi = Object.assign(read, {
673
- state: () => state(),
674
- loading: () => {
675
- const current = state();
676
- return current === "pending" || current === "refreshing";
677
- },
678
- error: () => failure()?.error,
679
- refetch: () => untrack(() => load(currentSource ?? source(), true)),
680
- mutate: (next) => batch(() => {
681
- value.set(() => next);
682
- failure.set(null);
683
- state.set(next === void 0 ? "unresolved" : "ready");
684
- }),
685
- optimistic: async (update, action) => {
686
- const previous = untrack(value);
687
- const next = update(previous);
688
- value.set(() => next);
689
- try {
690
- await action();
691
- return true;
692
- } catch (error) {
693
- if (untrack(value) === next) value.set(() => previous);
694
- batch(() => handleError(error, owner));
695
- return false;
696
- }
697
- }
698
- });
699
- return resourceApi;
700
- }
701
- function isPromiseLike(value) {
702
- return typeof value?.then === "function";
703
- }
704
413
  // src/async/lazy.ts
705
414
  function lazy(load) {
706
415
  let loaded;
@@ -1,5 +1,7 @@
1
- import { c as computed, i as onCleanup, l as signal } from "./shared/owner-DlUsXKXE.js";
1
+ import { t as computed } from "./shared/computed-3UO-KiH1.js";
2
+ import { t as signal } from "./shared/signal-BOXkY-Iv.js";
2
3
  import { c as handleError, h as untrack, i as batch, n as addMount, p as runWithOwner, s as getOwner } from "./shared/graph-D6-x_CWC.js";
4
+ import { i as onCleanup } from "./shared/owner-Dmge21OW.js";
3
5
  // src/router/history.ts
4
6
  var newKey = () => Math.random().toString(36).slice(2, 10);
5
7
  var stateKey = () => window.history.state?.keitsKey ?? "start";
@@ -0,0 +1,40 @@
1
+ import { g as updateIfNeeded, m as track, r as adopt } from "./graph-D6-x_CWC.js";
2
+ // src/core/computed.ts
3
+ function computed(fn, options) {
4
+ const node = {
5
+ kind: 2,
6
+ fn,
7
+ value: void 0,
8
+ equals: options?.equals ?? Object.is,
9
+ hasValue: false,
10
+ hasError: false,
11
+ error: void 0,
12
+ computing: false,
13
+ state: 2,
14
+ sources: [],
15
+ observers: [],
16
+ seenBy: 0,
17
+ flushId: 0,
18
+ flushRuns: 0,
19
+ owner: null,
20
+ children: null,
21
+ cleanups: null,
22
+ disposed: false
23
+ };
24
+ if (options?.name !== void 0) node.name = options.name;
25
+ adopt(node);
26
+ return () => {
27
+ if (node.computing) throw cycleError(node);
28
+ if (node.state !== 0) updateIfNeeded(node);
29
+ track(node);
30
+ if (node.hasError) throw node.error;
31
+ return node.value;
32
+ };
33
+ }
34
+ function cycleError(node) {
35
+ {
36
+ const who = node.name ? `"${node.name}"` : "a computed";
37
+ return /* @__PURE__ */ new Error(`[keits] Cycle detected: ${who} reads itself (directly or through other computeds).`);
38
+ }
39
+ }
40
+ export { computed as t };
@@ -1,33 +1,10 @@
1
- import { c as handleError, f as runEffect, i as batch, l as isStaticRender, p as runWithOwner, r as adopt, s as getOwner } from "./graph-D6-x_CWC.js";
2
- // src/core/effect.ts
3
- function effect(fn, options) {
4
- if (isStaticRender()) return;
5
- const node = {
6
- kind: 1,
7
- fn,
8
- state: 0,
9
- sources: [],
10
- flushId: 0,
11
- flushRuns: 0,
12
- owner: null,
13
- children: null,
14
- cleanups: null,
15
- disposed: false
16
- };
17
- if (options?.name !== void 0) node.name = options.name;
18
- adopt(node);
19
- batch(() => runEffect(node));
20
- }
1
+ import { t as renderEffect } from "./render-effect-kOGnLNbL.js";
2
+ import { c as handleError, i as batch, l as isStaticRender, p as runWithOwner, s as getOwner } from "./graph-D6-x_CWC.js";
21
3
  var hooks = {
22
4
  element: (tag, namespace) => namespace === null ? document.createElement(tag) : document.createElementNS(namespace, tag),
23
5
  text: (value) => document.createTextNode(value),
24
6
  marker: () => document.createComment("")
25
7
  };
26
- // src/dom/render-effect.ts
27
- function renderEffect(fn) {
28
- if (isStaticRender()) fn();
29
- else effect(fn);
30
- }
31
8
  // src/dom/insert.ts
32
9
  function insert(parent, child, before = null) {
33
10
  if (child === null || child === void 0 || typeof child === "boolean") return;
@@ -225,24 +202,14 @@ function writeStyle(style, name, value) {
225
202
  }
226
203
  // src/dom/element.ts
227
204
  function html(tag) {
228
- const factory = (first, children) => {
229
- const element = hooks.element(tag, null);
230
- if (isProps(first)) {
231
- applyProps(element, first);
232
- insert(element, children);
233
- if (typeof first.ref === "function") first.ref(element);
234
- } else if (first === null || first === void 0) insert(element, children);
235
- else insert(element, first);
236
- return element;
237
- };
238
- return factory;
205
+ return createFactory(tag, null);
239
206
  }
240
207
  function createFactory(tag, namespace) {
241
208
  return (first, children) => {
242
209
  const element = hooks.element(tag, namespace);
243
210
  if (isProps(first)) {
244
- applyProps(element, first);
245
211
  insert(element, children);
212
+ applyProps(element, first);
246
213
  if (typeof first.ref === "function") first.ref(element);
247
214
  } else if (first === null || first === void 0) insert(element, children);
248
215
  else insert(element, first);
@@ -252,4 +219,4 @@ function createFactory(tag, namespace) {
252
219
  function isProps(value) {
253
220
  return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.nodeType !== "number";
254
221
  }
255
- export { renderEffect as a, insert as i, html as n, hooks as o, SVG_NS as r, effect as s, createFactory as t };
222
+ export { hooks as a, insert as i, html as n, SVG_NS as r, createFactory as t };
@@ -0,0 +1,62 @@
1
+ import { _ as withStaticRender, c as handleError, i as batch, n as addMount, o as dispose, p as runWithOwner$1, r as adopt, s as getOwner$1, t as addCleanup } from "./graph-D6-x_CWC.js";
2
+ // src/core/owner.ts
3
+ function getOwner() {
4
+ return getOwner$1();
5
+ }
6
+ function runWithOwner(owner, fn) {
7
+ return runWithOwner$1(owner, fn);
8
+ }
9
+ function createRoot(fn) {
10
+ const root = {
11
+ kind: 3,
12
+ owner: getOwner$1(),
13
+ children: null,
14
+ cleanups: null,
15
+ disposed: false,
16
+ handler: null
17
+ };
18
+ const disposeRoot = () => batch(() => dispose(root));
19
+ return batch(() => {
20
+ try {
21
+ return runWithOwner$1(root, () => fn(disposeRoot));
22
+ } catch (error) {
23
+ dispose(root);
24
+ throw error;
25
+ }
26
+ });
27
+ }
28
+ function onCleanup(fn) {
29
+ if (!addCleanup(fn) && true) console.warn("[keits] onCleanup was called outside an owner, so it will never run.");
30
+ }
31
+ function onMount(fn) {
32
+ addMount(fn);
33
+ }
34
+ function catchError(fn, handler) {
35
+ const boundary = {
36
+ kind: 3,
37
+ owner: null,
38
+ children: null,
39
+ cleanups: null,
40
+ disposed: false,
41
+ handler
42
+ };
43
+ adopt(boundary);
44
+ return batch(() => {
45
+ try {
46
+ return runWithOwner$1(boundary, fn);
47
+ } catch (error) {
48
+ handleError(error, boundary);
49
+ return;
50
+ }
51
+ });
52
+ }
53
+ function renderStatic(fn) {
54
+ return withStaticRender(() => createRoot((dispose) => {
55
+ try {
56
+ return fn();
57
+ } finally {
58
+ dispose();
59
+ }
60
+ }));
61
+ }
62
+ export { onMount as a, onCleanup as i, createRoot as n, renderStatic as o, getOwner as r, runWithOwner as s, catchError as t };
@@ -0,0 +1,26 @@
1
+ import { f as runEffect, i as batch, l as isStaticRender, r as adopt } from "./graph-D6-x_CWC.js";
2
+ // src/core/effect.ts
3
+ function effect(fn, options) {
4
+ if (isStaticRender()) return;
5
+ const node = {
6
+ kind: 1,
7
+ fn,
8
+ state: 0,
9
+ sources: [],
10
+ flushId: 0,
11
+ flushRuns: 0,
12
+ owner: null,
13
+ children: null,
14
+ cleanups: null,
15
+ disposed: false
16
+ };
17
+ if (options?.name !== void 0) node.name = options.name;
18
+ adopt(node);
19
+ batch(() => runEffect(node));
20
+ }
21
+ // src/dom/render-effect.ts
22
+ function renderEffect(fn) {
23
+ if (isStaticRender()) fn();
24
+ else effect(fn);
25
+ }
26
+ export { effect as n, renderEffect as t };
@@ -0,0 +1,154 @@
1
+ import { c as handleError, h as untrack, i as batch, p as runWithOwner, r as adopt, s as getOwner, t as addCleanup } from "./graph-D6-x_CWC.js";
2
+ import { t as signal } from "./signal-BOXkY-Iv.js";
3
+ import { t as renderEffect } from "./render-effect-kOGnLNbL.js";
4
+ // src/core/context.ts
5
+ function createContext(defaultValue, options) {
6
+ const context = { defaultValue };
7
+ if (options?.name !== void 0) context.name = options.name;
8
+ return context;
9
+ }
10
+ function provide(context, value, fn) {
11
+ const provider = {
12
+ kind: 3,
13
+ owner: null,
14
+ children: null,
15
+ cleanups: null,
16
+ disposed: false,
17
+ handler: null,
18
+ context: {
19
+ key: context,
20
+ value
21
+ }
22
+ };
23
+ adopt(provider);
24
+ return runWithOwner(provider, fn);
25
+ }
26
+ function useContext(context) {
27
+ for (let o = getOwner(); o !== null; o = o.owner) if (o.kind === 3 && o.context?.key === context) return o.context.value;
28
+ return context.defaultValue;
29
+ }
30
+ // src/async/suspense-context.ts
31
+ var SuspenseContext = createContext(null);
32
+ // src/async/resource.ts
33
+ function resource(first, second, third) {
34
+ const hasSource = typeof second === "function";
35
+ const source = hasSource ? first : () => true;
36
+ const fetcher = hasSource ? second : (_, info) => first(info);
37
+ const options = hasSource ? third : second;
38
+ const owner = getOwner();
39
+ const hasInitial = options !== void 0 && "initialValue" in options;
40
+ const value = signal(options?.initialValue);
41
+ const state = signal(hasInitial ? "ready" : "unresolved");
42
+ const failure = signal(null);
43
+ let controller = null;
44
+ let version = 0;
45
+ let waiters = [];
46
+ let currentSource = null;
47
+ const settleWaiters = () => {
48
+ const list = waiters;
49
+ waiters = [];
50
+ for (const done of list) done();
51
+ };
52
+ function load(src, refetching) {
53
+ controller?.abort();
54
+ controller = null;
55
+ const id = ++version;
56
+ if (src === null || src === void 0 || src === false) {
57
+ currentSource = null;
58
+ state.set(untrack(value) === void 0 ? "unresolved" : "ready");
59
+ settleWaiters();
60
+ return Promise.resolve();
61
+ }
62
+ currentSource = src;
63
+ const abort = new AbortController();
64
+ controller = abort;
65
+ let result;
66
+ try {
67
+ result = fetcher(src, {
68
+ signal: abort.signal,
69
+ refetching,
70
+ value: untrack(value)
71
+ });
72
+ } catch (error) {
73
+ fail(error);
74
+ return Promise.resolve();
75
+ }
76
+ if (!isPromiseLike(result)) {
77
+ succeed(result);
78
+ return Promise.resolve();
79
+ }
80
+ state.set(untrack(value) === void 0 && untrack(state) !== "ready" ? "pending" : "refreshing");
81
+ return Promise.resolve(result).then((data) => {
82
+ if (id === version) succeed(data);
83
+ }, (error) => {
84
+ if (id === version) fail(error);
85
+ });
86
+ }
87
+ function succeed(data) {
88
+ controller = null;
89
+ batch(() => {
90
+ value.set(() => data);
91
+ failure.set(null);
92
+ state.set("ready");
93
+ });
94
+ settleWaiters();
95
+ }
96
+ function fail(error) {
97
+ controller = null;
98
+ batch(() => {
99
+ failure.set({ error });
100
+ state.set("errored");
101
+ });
102
+ settleWaiters();
103
+ }
104
+ renderEffect(() => {
105
+ const src = source();
106
+ untrack(() => load(src, false));
107
+ });
108
+ addCleanup(() => {
109
+ version++;
110
+ controller?.abort();
111
+ controller = null;
112
+ });
113
+ const read = () => {
114
+ const current = state();
115
+ if (current === "errored") throw failure().error;
116
+ if (current === "pending") {
117
+ const boundary = useContext(SuspenseContext);
118
+ if (boundary !== null) boundary.wait(resourceApi, (done) => waiters.push(done));
119
+ }
120
+ return value();
121
+ };
122
+ const resourceApi = Object.assign(read, {
123
+ state: () => state(),
124
+ loading: () => {
125
+ const current = state();
126
+ return current === "pending" || current === "refreshing";
127
+ },
128
+ error: () => failure()?.error,
129
+ refetch: () => untrack(() => load(currentSource ?? source(), true)),
130
+ mutate: (next) => batch(() => {
131
+ value.set(() => next);
132
+ failure.set(null);
133
+ state.set(next === void 0 ? "unresolved" : "ready");
134
+ }),
135
+ optimistic: async (update, action) => {
136
+ const previous = untrack(value);
137
+ const next = update(previous);
138
+ value.set(() => next);
139
+ try {
140
+ await action();
141
+ return true;
142
+ } catch (error) {
143
+ if (untrack(value) === next) value.set(() => previous);
144
+ batch(() => handleError(error, owner));
145
+ return false;
146
+ }
147
+ }
148
+ });
149
+ return resourceApi;
150
+ }
151
+ function isPromiseLike(value) {
152
+ return typeof value?.then === "function";
153
+ }
154
+ export { useContext as a, provide as i, SuspenseContext as n, createContext as r, resource as t };