sibujs 1.2.0 → 1.4.0

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 (95) hide show
  1. package/README.md +29 -25
  2. package/dist/browser.cjs +804 -2
  3. package/dist/browser.d.cts +591 -1
  4. package/dist/browser.d.ts +591 -1
  5. package/dist/browser.js +50 -8
  6. package/dist/build.cjs +655 -237
  7. package/dist/build.js +15 -93
  8. package/dist/cdn.global.js +188 -7
  9. package/dist/chunk-2BYQDGN3.js +742 -0
  10. package/dist/chunk-32DY64NT.js +282 -0
  11. package/dist/chunk-3AIRKM3B.js +1263 -0
  12. package/dist/chunk-3X2YG6YM.js +505 -0
  13. package/dist/chunk-5X6PP2UK.js +28 -0
  14. package/dist/chunk-77L6NL3X.js +1097 -0
  15. package/dist/chunk-BGN5ZMP4.js +26 -0
  16. package/dist/chunk-BTU3TJDS.js +365 -0
  17. package/dist/chunk-CHF5OHIA.js +61 -0
  18. package/dist/chunk-CMBFNA7L.js +27 -0
  19. package/dist/chunk-CNZ35WI2.js +178 -0
  20. package/dist/chunk-DAHRH4ON.js +331 -0
  21. package/dist/chunk-EBGIRKQY.js +616 -0
  22. package/dist/chunk-EUZND3CB.js +27 -0
  23. package/dist/chunk-F3FA4F32.js +292 -0
  24. package/dist/chunk-JAKHTMQU.js +1000 -0
  25. package/dist/chunk-JCI5M6U6.js +956 -0
  26. package/dist/chunk-KQPDEVVS.js +398 -0
  27. package/dist/chunk-M4NLBH4I.js +725 -0
  28. package/dist/chunk-NEKUBFPT.js +60 -0
  29. package/dist/chunk-NYVAC6P5.js +37 -0
  30. package/dist/chunk-PTQJDMRT.js +146 -0
  31. package/dist/chunk-QWZG56ET.js +2744 -0
  32. package/dist/chunk-TSOKIX5Z.js +654 -0
  33. package/dist/chunk-UHNL42EF.js +2730 -0
  34. package/dist/chunk-VRW3FULF.js +725 -0
  35. package/dist/chunk-WZSPOOER.js +84 -0
  36. package/dist/chunk-YT6HQ6AM.js +14 -0
  37. package/dist/chunk-ZD6OAMTH.js +277 -0
  38. package/dist/chunk-ZWKZCBO6.js +317 -0
  39. package/dist/contracts-DDrwxvJ-.d.cts +245 -0
  40. package/dist/contracts-DDrwxvJ-.d.ts +245 -0
  41. package/dist/contracts-xo5ckdRP.d.cts +240 -0
  42. package/dist/contracts-xo5ckdRP.d.ts +240 -0
  43. package/dist/data.cjs +35 -2
  44. package/dist/data.d.cts +7 -0
  45. package/dist/data.d.ts +7 -0
  46. package/dist/data.js +9 -8
  47. package/dist/devtools.cjs +122 -0
  48. package/dist/devtools.d.cts +69 -461
  49. package/dist/devtools.d.ts +69 -461
  50. package/dist/devtools.js +127 -6
  51. package/dist/ecosystem.cjs +23 -6
  52. package/dist/ecosystem.d.cts +1 -1
  53. package/dist/ecosystem.d.ts +1 -1
  54. package/dist/ecosystem.js +10 -9
  55. package/dist/extras.cjs +1208 -88
  56. package/dist/extras.d.cts +6 -6
  57. package/dist/extras.d.ts +6 -6
  58. package/dist/extras.js +70 -33
  59. package/dist/index.cjs +663 -158
  60. package/dist/index.d.cts +398 -40
  61. package/dist/index.d.ts +398 -40
  62. package/dist/index.js +39 -21
  63. package/dist/introspect-BumjnBKr.d.cts +477 -0
  64. package/dist/introspect-CZrlcaYy.d.ts +477 -0
  65. package/dist/introspect-Cb0zgpi2.d.cts +477 -0
  66. package/dist/introspect-Y2xNXGSf.d.ts +477 -0
  67. package/dist/motion.js +4 -4
  68. package/dist/patterns.cjs +51 -24
  69. package/dist/patterns.d.cts +19 -57
  70. package/dist/patterns.d.ts +19 -57
  71. package/dist/patterns.js +8 -16
  72. package/dist/performance.js +4 -4
  73. package/dist/plugins.cjs +429 -82
  74. package/dist/plugins.d.cts +27 -4
  75. package/dist/plugins.d.ts +27 -4
  76. package/dist/plugins.js +156 -37
  77. package/dist/ssr-4PBXAOO3.js +40 -0
  78. package/dist/ssr-Do_SiVoL.d.cts +201 -0
  79. package/dist/ssr-Do_SiVoL.d.ts +201 -0
  80. package/dist/ssr.cjs +312 -60
  81. package/dist/ssr.d.cts +10 -1
  82. package/dist/ssr.d.ts +10 -1
  83. package/dist/ssr.js +13 -10
  84. package/dist/tagFactory-DaJ0YWX6.d.cts +47 -0
  85. package/dist/tagFactory-DaJ0YWX6.d.ts +47 -0
  86. package/dist/testing.cjs +233 -2
  87. package/dist/testing.d.cts +42 -1
  88. package/dist/testing.d.ts +42 -1
  89. package/dist/testing.js +129 -2
  90. package/dist/ui.cjs +374 -8
  91. package/dist/ui.d.cts +252 -2
  92. package/dist/ui.d.ts +252 -2
  93. package/dist/ui.js +329 -11
  94. package/dist/widgets.js +7 -7
  95. package/package.json +1 -1
@@ -0,0 +1,84 @@
1
+ import {
2
+ drainNotificationQueue,
3
+ notifySubscribers,
4
+ queueSignalNotification,
5
+ recordDependency
6
+ } from "./chunk-ZD6OAMTH.js";
7
+ import {
8
+ isDev
9
+ } from "./chunk-5X6PP2UK.js";
10
+
11
+ // src/reactivity/batch.ts
12
+ var batchDepth = 0;
13
+ var pendingSignals = /* @__PURE__ */ new Set();
14
+ function batch(fn) {
15
+ batchDepth++;
16
+ try {
17
+ return fn();
18
+ } finally {
19
+ batchDepth--;
20
+ if (batchDepth === 0) {
21
+ flushBatch();
22
+ }
23
+ }
24
+ }
25
+ function enqueueBatchedSignal(signal2) {
26
+ if (batchDepth === 0) return false;
27
+ pendingSignals.add(signal2);
28
+ return true;
29
+ }
30
+ function isBatching() {
31
+ return batchDepth > 0;
32
+ }
33
+ function flushBatch() {
34
+ for (const signal2 of pendingSignals) {
35
+ queueSignalNotification(signal2);
36
+ }
37
+ pendingSignals.clear();
38
+ drainNotificationQueue();
39
+ }
40
+
41
+ // src/core/signals/signal.ts
42
+ var _g = globalThis;
43
+ var _isDev = isDev();
44
+ function signal(initial, options) {
45
+ const state = { value: initial };
46
+ const debugName = _isDev ? options?.name : void 0;
47
+ const equalsFn = options?.equals;
48
+ if (debugName) {
49
+ state.__name = debugName;
50
+ }
51
+ function get() {
52
+ recordDependency(state);
53
+ return state.value;
54
+ }
55
+ get.__signal = state;
56
+ if (debugName) get.__name = debugName;
57
+ function set(next) {
58
+ const newValue = typeof next === "function" ? next(state.value) : next;
59
+ if (equalsFn ? equalsFn(state.value, newValue) : Object.is(newValue, state.value)) return;
60
+ if (_isDev) {
61
+ const oldValue = state.value;
62
+ state.value = newValue;
63
+ const hook = _g.__SIBU_DEVTOOLS_GLOBAL_HOOK__;
64
+ if (hook) hook.emit("signal:update", { signal: state, name: debugName, oldValue, newValue });
65
+ } else {
66
+ state.value = newValue;
67
+ }
68
+ if (!enqueueBatchedSignal(state)) {
69
+ notifySubscribers(state);
70
+ }
71
+ }
72
+ if (_isDev) {
73
+ const hook = _g.__SIBU_DEVTOOLS_GLOBAL_HOOK__;
74
+ if (hook) hook.emit("signal:create", { signal: state, name: debugName, getter: get, initial });
75
+ }
76
+ return [get, set];
77
+ }
78
+
79
+ export {
80
+ batch,
81
+ enqueueBatchedSignal,
82
+ isBatching,
83
+ signal
84
+ };
@@ -0,0 +1,14 @@
1
+ // src/core/rendering/createId.ts
2
+ var idCounter = 0;
3
+ function createId(prefix = "sibu") {
4
+ idCounter++;
5
+ return `${prefix}-${idCounter}`;
6
+ }
7
+ function __resetIdCounter() {
8
+ idCounter = 0;
9
+ }
10
+
11
+ export {
12
+ createId,
13
+ __resetIdCounter
14
+ };
@@ -0,0 +1,277 @@
1
+ import {
2
+ devWarn,
3
+ isDev
4
+ } from "./chunk-5X6PP2UK.js";
5
+
6
+ // src/reactivity/track.ts
7
+ var _isDev = isDev();
8
+ var subscriberStack = new Array(32);
9
+ var stackCapacity = 32;
10
+ var stackTop = -1;
11
+ var currentSubscriber = null;
12
+ var signalSubscribers = /* @__PURE__ */ new WeakMap();
13
+ var SUBS = "__s";
14
+ var notifyDepth = 0;
15
+ var pendingQueue = [];
16
+ var pendingSet = /* @__PURE__ */ new Set();
17
+ function safeInvoke(sub) {
18
+ try {
19
+ sub();
20
+ } catch (err) {
21
+ if (_isDev) devWarn(`Subscriber threw during notification: ${err instanceof Error ? err.message : String(err)}`);
22
+ }
23
+ }
24
+ var suspendDepth = 0;
25
+ var trackingSuspended = false;
26
+ function track(effectFn, subscriber) {
27
+ if (!subscriber) subscriber = effectFn;
28
+ cleanup(subscriber);
29
+ ++stackTop;
30
+ if (stackTop >= stackCapacity) {
31
+ stackCapacity *= 2;
32
+ subscriberStack.length = stackCapacity;
33
+ }
34
+ subscriberStack[stackTop] = subscriber;
35
+ currentSubscriber = subscriber;
36
+ try {
37
+ effectFn();
38
+ } finally {
39
+ stackTop--;
40
+ currentSubscriber = stackTop >= 0 ? subscriberStack[stackTop] : null;
41
+ }
42
+ return () => cleanup(subscriber);
43
+ }
44
+ function suspendTracking() {
45
+ if (suspendDepth === 0) {
46
+ ++stackTop;
47
+ if (stackTop >= stackCapacity) {
48
+ stackCapacity *= 2;
49
+ subscriberStack.length = stackCapacity;
50
+ }
51
+ subscriberStack[stackTop] = null;
52
+ currentSubscriber = null;
53
+ trackingSuspended = true;
54
+ }
55
+ suspendDepth++;
56
+ }
57
+ function resumeTracking() {
58
+ suspendDepth--;
59
+ if (suspendDepth === 0) {
60
+ stackTop--;
61
+ currentSubscriber = stackTop >= 0 ? subscriberStack[stackTop] : null;
62
+ trackingSuspended = false;
63
+ }
64
+ }
65
+ function untracked(fn) {
66
+ suspendTracking();
67
+ try {
68
+ return fn();
69
+ } finally {
70
+ resumeTracking();
71
+ }
72
+ }
73
+ function recordDependency(signal) {
74
+ if (!currentSubscriber) return;
75
+ const sub = currentSubscriber;
76
+ if (sub._dep === signal) return;
77
+ const deps = sub._deps;
78
+ if (deps) {
79
+ if (deps.has(signal)) return;
80
+ deps.add(signal);
81
+ } else if (sub._dep !== void 0) {
82
+ const set = /* @__PURE__ */ new Set();
83
+ set.add(sub._dep);
84
+ set.add(signal);
85
+ sub._deps = set;
86
+ sub._dep = void 0;
87
+ } else {
88
+ sub._dep = signal;
89
+ }
90
+ let subs = signal[SUBS];
91
+ if (!subs) {
92
+ subs = /* @__PURE__ */ new Set();
93
+ signalSubscribers.set(signal, subs);
94
+ signal[SUBS] = subs;
95
+ }
96
+ subs.add(currentSubscriber);
97
+ if (subs.size === 1) {
98
+ signal.__f = currentSubscriber;
99
+ } else if (signal.__f !== void 0) {
100
+ signal.__f = void 0;
101
+ }
102
+ }
103
+ function queueSignalNotification(signal) {
104
+ const subs = signal[SUBS];
105
+ if (!subs) return;
106
+ for (const sub of subs) {
107
+ if (sub._c) {
108
+ propagateDirty(sub);
109
+ } else if (!pendingSet.has(sub)) {
110
+ pendingSet.add(sub);
111
+ pendingQueue.push(sub);
112
+ }
113
+ }
114
+ }
115
+ function drainNotificationQueue() {
116
+ if (notifyDepth > 0) return;
117
+ notifyDepth++;
118
+ try {
119
+ let i = 0;
120
+ while (i < pendingQueue.length) {
121
+ safeInvoke(pendingQueue[i]);
122
+ i++;
123
+ }
124
+ } finally {
125
+ pendingQueue.length = 0;
126
+ pendingSet.clear();
127
+ notifyDepth--;
128
+ }
129
+ }
130
+ function propagateDirty(sub) {
131
+ sub();
132
+ let sig = sub._sig;
133
+ while (sig) {
134
+ const first = sig.__f;
135
+ if (first) {
136
+ if (first._c) {
137
+ const nSig = first._sig;
138
+ nSig._d = true;
139
+ sig = nSig;
140
+ continue;
141
+ }
142
+ if (!pendingSet.has(first)) {
143
+ pendingSet.add(first);
144
+ pendingQueue.push(first);
145
+ }
146
+ break;
147
+ }
148
+ const subs = sig[SUBS];
149
+ if (!subs) break;
150
+ let nextSig;
151
+ for (const s of subs) {
152
+ if (s._c) {
153
+ s();
154
+ const nSig = s._sig;
155
+ if (nSig && !nextSig) {
156
+ nextSig = nSig;
157
+ } else if (nSig) {
158
+ propagateDirty(s);
159
+ }
160
+ } else if (!pendingSet.has(s)) {
161
+ pendingSet.add(s);
162
+ pendingQueue.push(s);
163
+ }
164
+ }
165
+ sig = nextSig;
166
+ }
167
+ }
168
+ function notifySubscribers(signal) {
169
+ const first = signal.__f;
170
+ if (first) {
171
+ if (notifyDepth > 0) {
172
+ if (first._c) {
173
+ propagateDirty(first);
174
+ } else if (!pendingSet.has(first)) {
175
+ pendingSet.add(first);
176
+ pendingQueue.push(first);
177
+ }
178
+ return;
179
+ }
180
+ notifyDepth++;
181
+ try {
182
+ if (first._c) {
183
+ propagateDirty(first);
184
+ } else {
185
+ safeInvoke(first);
186
+ }
187
+ let i = 0;
188
+ while (i < pendingQueue.length) {
189
+ safeInvoke(pendingQueue[i]);
190
+ i++;
191
+ }
192
+ } finally {
193
+ pendingQueue.length = 0;
194
+ pendingSet.clear();
195
+ notifyDepth--;
196
+ }
197
+ return;
198
+ }
199
+ const subs = signal[SUBS];
200
+ if (!subs || subs.size === 0) return;
201
+ if (notifyDepth > 0) {
202
+ for (const sub of subs) {
203
+ if (sub._c) {
204
+ propagateDirty(sub);
205
+ } else if (!pendingSet.has(sub)) {
206
+ pendingSet.add(sub);
207
+ pendingQueue.push(sub);
208
+ }
209
+ }
210
+ return;
211
+ }
212
+ notifyDepth++;
213
+ try {
214
+ let directCount = 0;
215
+ for (const sub of subs) {
216
+ pendingQueue[directCount++] = sub;
217
+ }
218
+ for (let i2 = 0; i2 < directCount; i2++) {
219
+ if (pendingQueue[i2]._c) {
220
+ propagateDirty(pendingQueue[i2]);
221
+ }
222
+ }
223
+ for (let i2 = 0; i2 < directCount; i2++) {
224
+ if (!pendingQueue[i2]._c) {
225
+ if (!pendingSet.has(pendingQueue[i2])) {
226
+ safeInvoke(pendingQueue[i2]);
227
+ }
228
+ }
229
+ }
230
+ let i = directCount;
231
+ while (i < pendingQueue.length) {
232
+ safeInvoke(pendingQueue[i]);
233
+ i++;
234
+ }
235
+ } finally {
236
+ pendingQueue.length = 0;
237
+ pendingSet.clear();
238
+ notifyDepth--;
239
+ }
240
+ }
241
+ function cleanup(subscriber) {
242
+ const sub = subscriber;
243
+ const singleDep = sub._dep;
244
+ if (singleDep !== void 0) {
245
+ const subs = singleDep[SUBS];
246
+ if (subs) {
247
+ subs.delete(subscriber);
248
+ if (singleDep.__f === subscriber) {
249
+ singleDep.__f = void 0;
250
+ }
251
+ }
252
+ sub._dep = void 0;
253
+ return;
254
+ }
255
+ const deps = sub._deps;
256
+ if (!deps || deps.size === 0) return;
257
+ for (const signal of deps) {
258
+ const subs = signal[SUBS];
259
+ if (subs) {
260
+ subs.delete(subscriber);
261
+ if (signal.__f === subscriber) {
262
+ signal.__f = void 0;
263
+ }
264
+ }
265
+ }
266
+ deps.clear();
267
+ }
268
+
269
+ export {
270
+ trackingSuspended,
271
+ track,
272
+ untracked,
273
+ recordDependency,
274
+ queueSignalNotification,
275
+ drainNotificationQueue,
276
+ notifySubscribers
277
+ };
@@ -0,0 +1,317 @@
1
+ import {
2
+ derived
3
+ } from "./chunk-NEKUBFPT.js";
4
+ import {
5
+ effect
6
+ } from "./chunk-CHF5OHIA.js";
7
+ import {
8
+ signal
9
+ } from "./chunk-WZSPOOER.js";
10
+
11
+ // src/patterns/machine.ts
12
+ function machine(config) {
13
+ const [state, setState] = signal(config.initial);
14
+ const [context, setContext] = signal(config.context || {});
15
+ const initialDef = config.states[config.initial];
16
+ if (initialDef?.entry) {
17
+ initialDef.entry(context());
18
+ }
19
+ function send(event) {
20
+ const currentState = state();
21
+ const stateDef = config.states[currentState];
22
+ if (!stateDef?.on) return;
23
+ const transition = stateDef.on[event];
24
+ if (!transition) return;
25
+ let target;
26
+ let guard;
27
+ let action;
28
+ if (typeof transition === "string") {
29
+ target = transition;
30
+ } else {
31
+ target = transition.target;
32
+ guard = transition.guard;
33
+ action = transition.action;
34
+ }
35
+ const ctx = context();
36
+ if (guard && !guard(ctx)) return;
37
+ if (stateDef.exit) {
38
+ stateDef.exit(ctx);
39
+ }
40
+ if (action) {
41
+ const rawPatch = action(ctx);
42
+ const next = { ...ctx };
43
+ for (const key of Object.keys(rawPatch)) {
44
+ if (key === "__proto__" || key === "constructor" || key === "prototype") continue;
45
+ next[key] = rawPatch[key];
46
+ }
47
+ setContext(next);
48
+ }
49
+ setState(target);
50
+ const targetDef = config.states[target];
51
+ if (targetDef?.entry) {
52
+ targetDef.entry(context());
53
+ }
54
+ }
55
+ function matches(s) {
56
+ return state() === s;
57
+ }
58
+ function can(event) {
59
+ const currentState = state();
60
+ const stateDef = config.states[currentState];
61
+ if (!stateDef?.on) return false;
62
+ const transition = stateDef.on[event];
63
+ if (!transition) return false;
64
+ if (typeof transition === "string") return true;
65
+ if (transition.guard) {
66
+ return transition.guard(context());
67
+ }
68
+ return true;
69
+ }
70
+ return { state, context, send, matches, can };
71
+ }
72
+
73
+ // src/patterns/persist.ts
74
+ function persisted(key, initial, options = {}) {
75
+ const storage = options.session ? sessionStorage : localStorage;
76
+ const serialize = options.serialize || JSON.stringify;
77
+ const deserialize = options.deserialize || JSON.parse;
78
+ const encrypt = options.encrypt;
79
+ const decrypt = options.decrypt;
80
+ const syncTabs = options.session ? false : options.syncTabs ?? true;
81
+ let restored = initial;
82
+ try {
83
+ let raw = storage.getItem(key);
84
+ if (raw !== null) {
85
+ if (decrypt) raw = decrypt(raw);
86
+ const parsed = deserialize(raw);
87
+ restored = options.validate && !options.validate(parsed) ? initial : parsed;
88
+ }
89
+ } catch {
90
+ }
91
+ const [value, setValue] = signal(restored);
92
+ let applyingFromStorage = false;
93
+ effect(() => {
94
+ const current = value();
95
+ if (applyingFromStorage) return;
96
+ try {
97
+ let serialized = serialize(current);
98
+ if (encrypt) serialized = encrypt(serialized);
99
+ storage.setItem(key, serialized);
100
+ } catch {
101
+ }
102
+ });
103
+ let storageListener = null;
104
+ if (syncTabs && typeof window !== "undefined") {
105
+ storageListener = (e) => {
106
+ if (e.storageArea !== storage || e.key !== key) return;
107
+ if (e.newValue === null) {
108
+ applyingFromStorage = true;
109
+ try {
110
+ setValue(initial);
111
+ } finally {
112
+ applyingFromStorage = false;
113
+ }
114
+ return;
115
+ }
116
+ try {
117
+ let raw = e.newValue;
118
+ if (decrypt) raw = decrypt(raw);
119
+ const parsed = deserialize(raw);
120
+ if (options.validate && !options.validate(parsed)) return;
121
+ applyingFromStorage = true;
122
+ try {
123
+ setValue(parsed);
124
+ } finally {
125
+ applyingFromStorage = false;
126
+ }
127
+ } catch {
128
+ }
129
+ };
130
+ window.addEventListener("storage", storageListener);
131
+ }
132
+ const dispose = () => {
133
+ if (storageListener && typeof window !== "undefined") {
134
+ window.removeEventListener("storage", storageListener);
135
+ storageListener = null;
136
+ }
137
+ };
138
+ Object.defineProperty(setValue, "dispose", {
139
+ value: dispose,
140
+ enumerable: false,
141
+ writable: false,
142
+ configurable: false
143
+ });
144
+ return [value, setValue];
145
+ }
146
+
147
+ // src/patterns/optimistic.ts
148
+ function optimistic(initialValue) {
149
+ const [value, setValue] = signal(initialValue);
150
+ const [_pending, setPending] = signal(false);
151
+ async function addOptimistic(optimisticValue, asyncAction) {
152
+ const previousValue = value();
153
+ setValue(optimisticValue);
154
+ setPending(true);
155
+ try {
156
+ const result = await asyncAction();
157
+ setValue(result);
158
+ } catch {
159
+ setValue(previousValue);
160
+ } finally {
161
+ setPending(false);
162
+ }
163
+ }
164
+ return [value, addOptimistic];
165
+ }
166
+ function optimisticList(initialValue) {
167
+ const [items, setItems] = signal([...initialValue]);
168
+ async function addOptimistic(item, asyncAction) {
169
+ const prev = items();
170
+ setItems([...prev, item]);
171
+ try {
172
+ const result = await asyncAction();
173
+ setItems((current) => {
174
+ const idx = current.lastIndexOf(item);
175
+ if (idx >= 0) {
176
+ const next = [...current];
177
+ next[idx] = result;
178
+ return next;
179
+ }
180
+ return [...current.filter((i) => i !== item), result];
181
+ });
182
+ } catch {
183
+ setItems(prev);
184
+ }
185
+ }
186
+ async function removeOptimistic(predicate, asyncAction) {
187
+ const prev = items();
188
+ setItems(prev.filter((item) => !predicate(item)));
189
+ try {
190
+ await asyncAction();
191
+ } catch {
192
+ setItems(prev);
193
+ }
194
+ }
195
+ async function updateOptimistic(predicate, update, asyncAction) {
196
+ const prev = items();
197
+ setItems(prev.map((item) => predicate(item) ? { ...item, ...update } : item));
198
+ try {
199
+ const result = await asyncAction();
200
+ setItems((current) => current.map((item) => predicate(item) ? result : item));
201
+ } catch {
202
+ setItems(prev);
203
+ }
204
+ }
205
+ return { items, addOptimistic, removeOptimistic, updateOptimistic };
206
+ }
207
+
208
+ // src/patterns/timeTravel.ts
209
+ function timeline(initial, maxHistory = 100) {
210
+ const [history, setHistory] = signal([initial]);
211
+ const [index, setIndex] = signal(0);
212
+ const value = derived(() => history()[index()]);
213
+ const canUndo = derived(() => index() > 0);
214
+ const canRedo = derived(() => index() < history().length - 1);
215
+ function set(next) {
216
+ const current = value();
217
+ const newValue = typeof next === "function" ? next(current) : next;
218
+ if (Object.is(newValue, current)) return;
219
+ const hist = history();
220
+ const idx = index();
221
+ const newHistory = hist.slice(0, idx + 1);
222
+ newHistory.push(newValue);
223
+ if (newHistory.length > maxHistory) {
224
+ newHistory.shift();
225
+ setHistory(newHistory);
226
+ setIndex(newHistory.length - 1);
227
+ } else {
228
+ setHistory(newHistory);
229
+ setIndex(idx + 1);
230
+ }
231
+ }
232
+ function undo() {
233
+ if (canUndo()) {
234
+ setIndex(index() - 1);
235
+ }
236
+ }
237
+ function redo() {
238
+ if (canRedo()) {
239
+ setIndex(index() + 1);
240
+ }
241
+ }
242
+ function reset() {
243
+ setHistory([initial]);
244
+ setIndex(0);
245
+ }
246
+ function jumpTo(targetIndex) {
247
+ const hist = history();
248
+ if (targetIndex >= 0 && targetIndex < hist.length) {
249
+ setIndex(targetIndex);
250
+ }
251
+ }
252
+ return { value, set, undo, redo, canUndo, canRedo, history, index, reset, jumpTo };
253
+ }
254
+
255
+ // src/patterns/globalStore.ts
256
+ function globalStore(config) {
257
+ const initialState = { ...config.state };
258
+ const [getState, setState] = signal({ ...initialState });
259
+ const listeners = /* @__PURE__ */ new Set();
260
+ const middlewares = config.middleware || [];
261
+ function dispatch(action, payload) {
262
+ const actionFn = config.actions[action];
263
+ if (!actionFn) throw new Error(`Unknown action: ${String(action)}`);
264
+ const execute = () => {
265
+ const current = getState();
266
+ const rawPatch = actionFn(current, payload);
267
+ const patch = {};
268
+ for (const key of Object.keys(rawPatch)) {
269
+ if (key !== "__proto__" && key !== "constructor" && key !== "prototype") {
270
+ patch[key] = rawPatch[key];
271
+ }
272
+ }
273
+ setState({ ...current, ...patch });
274
+ const newState = getState();
275
+ for (const listener of listeners) {
276
+ listener(newState);
277
+ }
278
+ };
279
+ if (middlewares.length === 0) {
280
+ execute();
281
+ return;
282
+ }
283
+ let index = 0;
284
+ const next = () => {
285
+ if (index < middlewares.length) {
286
+ const mw = middlewares[index++];
287
+ mw(getState(), String(action), payload, next);
288
+ } else {
289
+ execute();
290
+ }
291
+ };
292
+ next();
293
+ }
294
+ function select(selector) {
295
+ return derived(() => selector(getState()));
296
+ }
297
+ function subscribe(callback) {
298
+ listeners.add(callback);
299
+ return () => listeners.delete(callback);
300
+ }
301
+ function reset() {
302
+ setState({ ...initialState });
303
+ for (const listener of listeners) {
304
+ listener(getState());
305
+ }
306
+ }
307
+ return { getState, select, dispatch, subscribe, reset };
308
+ }
309
+
310
+ export {
311
+ machine,
312
+ persisted,
313
+ optimistic,
314
+ optimisticList,
315
+ timeline,
316
+ globalStore
317
+ };