storion 0.6.0 → 0.7.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.
@@ -0,0 +1,294 @@
1
+ import { g as getHooks, w as withHooks, s as scheduleNotification, e as emitter } from "./emitter-BA44OHdL.js";
2
+ function isPromiseLike(value) {
3
+ return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
4
+ }
5
+ function createEffectContext(nth, onRefresh) {
6
+ let cleanupEmitter = null;
7
+ let abortController = null;
8
+ let isStale = false;
9
+ const runCleanups = () => {
10
+ if (isStale) return;
11
+ isStale = true;
12
+ if (abortController) {
13
+ abortController.abort();
14
+ abortController = null;
15
+ }
16
+ if (cleanupEmitter && cleanupEmitter.size > 0) {
17
+ cleanupEmitter.emitAndClearLifo();
18
+ }
19
+ };
20
+ function wrapPromise(promise) {
21
+ return new Promise((resolve, reject) => {
22
+ promise.then(
23
+ (value) => {
24
+ if (!isStale) {
25
+ resolve(value);
26
+ }
27
+ },
28
+ (error) => {
29
+ if (!isStale) {
30
+ reject(error);
31
+ }
32
+ }
33
+ );
34
+ });
35
+ }
36
+ const context = {
37
+ nth,
38
+ get signal() {
39
+ if (!abortController) {
40
+ abortController = new AbortController();
41
+ }
42
+ return abortController.signal;
43
+ },
44
+ onCleanup(listener) {
45
+ if (!cleanupEmitter) {
46
+ cleanupEmitter = emitter();
47
+ }
48
+ return cleanupEmitter.on(listener);
49
+ },
50
+ safe(promiseOrCallback) {
51
+ if (promiseOrCallback instanceof Promise) {
52
+ return wrapPromise(promiseOrCallback);
53
+ }
54
+ return (...args) => {
55
+ if (!isStale) {
56
+ const result = promiseOrCallback(
57
+ ...args
58
+ );
59
+ if (isPromiseLike(result)) {
60
+ return wrapPromise(result);
61
+ }
62
+ return result;
63
+ }
64
+ return void 0;
65
+ };
66
+ },
67
+ refresh() {
68
+ if (!isStale) {
69
+ onRefresh();
70
+ }
71
+ }
72
+ };
73
+ return Object.assign(context, { _runCleanups: runCleanups });
74
+ }
75
+ class CannotRefreshError extends Error {
76
+ constructor() {
77
+ super("Effect is already running, cannot refresh");
78
+ }
79
+ }
80
+ function resolveErrorStrategy(effectOptions) {
81
+ return (effectOptions == null ? void 0 : effectOptions.onError) ?? "keepAlive";
82
+ }
83
+ function getRetryDelay(config, attempt) {
84
+ if (typeof config.delay === "function") {
85
+ return config.delay(attempt);
86
+ }
87
+ return config.delay ?? 100 * Math.pow(2, attempt);
88
+ }
89
+ function effect(fn, options) {
90
+ let currentContext = null;
91
+ let subscriptionEmitter = null;
92
+ let isStarted = false;
93
+ let isRunning = false;
94
+ let isDisposed = false;
95
+ let runGeneration = 0;
96
+ let retryCount = 0;
97
+ let retryTimeout = null;
98
+ let errorStrategy = "keepAlive";
99
+ let onErrorCallback = null;
100
+ let prevTrackedDeps = /* @__PURE__ */ new Map();
101
+ let prevSubscriptionEmitter = null;
102
+ let trackedDeps = /* @__PURE__ */ new Map();
103
+ const writtenProps = /* @__PURE__ */ new Set();
104
+ let newTrackedDeps = null;
105
+ const getSubscriptionEmitter = () => {
106
+ if (!subscriptionEmitter) {
107
+ subscriptionEmitter = emitter();
108
+ }
109
+ return subscriptionEmitter;
110
+ };
111
+ const clearSubscriptions = () => {
112
+ if (subscriptionEmitter && subscriptionEmitter.size > 0) {
113
+ subscriptionEmitter.emitAndClear();
114
+ }
115
+ };
116
+ const areDepsChanged = (prev, next) => {
117
+ if (prev.size !== next.size) return true;
118
+ for (const key of next.keys()) {
119
+ if (!prev.has(key)) return true;
120
+ }
121
+ return false;
122
+ };
123
+ const subscribeToTrackedDeps = () => {
124
+ for (const [key, dep] of trackedDeps) {
125
+ if (writtenProps.has(key)) continue;
126
+ const unsub = dep.subscribe(() => {
127
+ scheduleNotification(execute, fn);
128
+ });
129
+ getSubscriptionEmitter().on(unsub);
130
+ }
131
+ };
132
+ const handleError = (error) => {
133
+ onErrorCallback == null ? void 0 : onErrorCallback(error);
134
+ if (errorStrategy === "failFast") {
135
+ throw error;
136
+ }
137
+ if (errorStrategy === "keepAlive") {
138
+ console.error("Effect error (keepAlive):", error);
139
+ if (prevSubscriptionEmitter && prevSubscriptionEmitter.size > 0) {
140
+ trackedDeps = new Map(prevTrackedDeps);
141
+ subscriptionEmitter = prevSubscriptionEmitter;
142
+ prevSubscriptionEmitter = null;
143
+ return;
144
+ }
145
+ if (newTrackedDeps && newTrackedDeps.size > 0) {
146
+ trackedDeps = newTrackedDeps;
147
+ }
148
+ subscribeToTrackedDeps();
149
+ return;
150
+ }
151
+ if (typeof errorStrategy === "function") {
152
+ const retry = () => {
153
+ retryCount++;
154
+ execute();
155
+ };
156
+ errorStrategy({ error, retry, retryCount });
157
+ return;
158
+ }
159
+ const retryConfig = errorStrategy;
160
+ if (retryCount < retryConfig.maxRetries) {
161
+ const delay = getRetryDelay(retryConfig, retryCount);
162
+ retryCount++;
163
+ retryTimeout = setTimeout(() => {
164
+ retryTimeout = null;
165
+ execute();
166
+ }, delay);
167
+ } else {
168
+ console.error(
169
+ `Effect failed after ${retryConfig.maxRetries} retries:`,
170
+ error
171
+ );
172
+ }
173
+ };
174
+ let cachedHooks = null;
175
+ const getTrackingHooks = (current) => {
176
+ if (!cachedHooks) {
177
+ cachedHooks = {
178
+ ...current,
179
+ onRead: (event) => {
180
+ var _a;
181
+ (_a = current.onRead) == null ? void 0 : _a.call(current, event);
182
+ if (!newTrackedDeps.has(event.key)) {
183
+ newTrackedDeps.set(event.key, {
184
+ key: event.key,
185
+ value: event.value,
186
+ subscribe: event.subscribe
187
+ });
188
+ }
189
+ },
190
+ onWrite: (event) => {
191
+ var _a;
192
+ (_a = current.onWrite) == null ? void 0 : _a.call(current, event);
193
+ writtenProps.add(event.key);
194
+ },
195
+ scheduleNotification: current.scheduleNotification,
196
+ scheduleEffect: current.scheduleEffect
197
+ };
198
+ }
199
+ return cachedHooks;
200
+ };
201
+ const execute = () => {
202
+ if (isDisposed || isRunning) return;
203
+ isRunning = true;
204
+ let shouldRefresh = false;
205
+ const currentGeneration = ++runGeneration;
206
+ try {
207
+ currentContext == null ? void 0 : currentContext._runCleanups();
208
+ currentContext = null;
209
+ if (subscriptionEmitter && subscriptionEmitter.size > 0) {
210
+ prevTrackedDeps = new Map(trackedDeps);
211
+ prevSubscriptionEmitter = subscriptionEmitter;
212
+ subscriptionEmitter = null;
213
+ }
214
+ newTrackedDeps = /* @__PURE__ */ new Map();
215
+ writtenProps.clear();
216
+ let lazyContext = null;
217
+ if (fn.length) {
218
+ lazyContext = createEffectContext(currentGeneration, () => {
219
+ if (isRunning) {
220
+ throw new CannotRefreshError();
221
+ }
222
+ execute();
223
+ });
224
+ }
225
+ withHooks(getTrackingHooks, () => {
226
+ const result = fn(lazyContext);
227
+ if (result !== null && result !== void 0 && typeof result.then === "function") {
228
+ throw new Error(
229
+ "Effect function must be synchronous. Use ctx.safe(promise) for async operations instead of returning a Promise."
230
+ );
231
+ }
232
+ if (lazyContext && lazyContext.refresh === result) {
233
+ shouldRefresh = true;
234
+ }
235
+ });
236
+ currentContext = lazyContext;
237
+ retryCount = 0;
238
+ const depsChanged = areDepsChanged(trackedDeps, newTrackedDeps);
239
+ if (depsChanged) {
240
+ if (prevSubscriptionEmitter && prevSubscriptionEmitter.size > 0) {
241
+ prevSubscriptionEmitter.emitAndClear();
242
+ }
243
+ trackedDeps = newTrackedDeps;
244
+ subscribeToTrackedDeps();
245
+ } else {
246
+ if (prevSubscriptionEmitter) {
247
+ subscriptionEmitter = prevSubscriptionEmitter;
248
+ }
249
+ }
250
+ prevTrackedDeps.clear();
251
+ prevSubscriptionEmitter = null;
252
+ } catch (error) {
253
+ if (error instanceof Error && error instanceof CannotRefreshError) {
254
+ throw error;
255
+ }
256
+ handleError(error);
257
+ } finally {
258
+ isRunning = false;
259
+ }
260
+ if (shouldRefresh) {
261
+ execute();
262
+ }
263
+ };
264
+ const dispose = () => {
265
+ if (isDisposed) return;
266
+ isDisposed = true;
267
+ runGeneration++;
268
+ if (retryTimeout) {
269
+ clearTimeout(retryTimeout);
270
+ retryTimeout = null;
271
+ }
272
+ currentContext == null ? void 0 : currentContext._runCleanups();
273
+ currentContext = null;
274
+ clearSubscriptions();
275
+ if (prevSubscriptionEmitter && prevSubscriptionEmitter.size > 0) {
276
+ prevSubscriptionEmitter.emitAndClear();
277
+ }
278
+ };
279
+ const start = (runOptions) => {
280
+ if (isStarted) return;
281
+ isStarted = true;
282
+ errorStrategy = resolveErrorStrategy(options);
283
+ onErrorCallback = (runOptions == null ? void 0 : runOptions.onError) ?? null;
284
+ execute();
285
+ };
286
+ getHooks().scheduleEffect((runOptions) => {
287
+ start(runOptions);
288
+ return dispose;
289
+ });
290
+ return dispose;
291
+ }
292
+ export {
293
+ effect as e
294
+ };
@@ -0,0 +1,228 @@
1
+ let globalHooks = {
2
+ scheduleNotification(notify) {
3
+ notify();
4
+ },
5
+ scheduleEffect(runEffect) {
6
+ runEffect();
7
+ }
8
+ };
9
+ function getHooks() {
10
+ return globalHooks;
11
+ }
12
+ function hasReadHook() {
13
+ return globalHooks.onRead !== void 0;
14
+ }
15
+ function hasWriteHook() {
16
+ return globalHooks.onWrite !== void 0;
17
+ }
18
+ function withHooks(hooksOrSetup, fn, onFinish) {
19
+ const prev = globalHooks;
20
+ if (typeof hooksOrSetup === "function") {
21
+ globalHooks = {
22
+ ...globalHooks,
23
+ ...hooksOrSetup(prev)
24
+ };
25
+ } else {
26
+ globalHooks = { ...prev, ...hooksOrSetup };
27
+ }
28
+ try {
29
+ return fn();
30
+ } finally {
31
+ globalHooks = prev;
32
+ onFinish == null ? void 0 : onFinish();
33
+ }
34
+ }
35
+ function trackRead(storeId, prop, value, subscribe) {
36
+ var _a;
37
+ const key = `${storeId}.${prop}`;
38
+ (_a = globalHooks.onRead) == null ? void 0 : _a.call(globalHooks, { key, value, subscribe });
39
+ }
40
+ function trackWrite(storeId, prop, next, prev) {
41
+ var _a;
42
+ const key = `${storeId}.${prop}`;
43
+ (_a = globalHooks.onWrite) == null ? void 0 : _a.call(globalHooks, { key, next, prev });
44
+ }
45
+ function untrack(fn) {
46
+ return withHooks({ onRead: void 0, onWrite: void 0 }, fn);
47
+ }
48
+ function scheduleNotification(notify, key) {
49
+ globalHooks.scheduleNotification(notify, key);
50
+ }
51
+ function batch(fn) {
52
+ const pending = /* @__PURE__ */ new Map();
53
+ return withHooks(
54
+ (current) => ({
55
+ ...current,
56
+ scheduleNotification: (notify, key) => {
57
+ const actualKey = key ?? notify;
58
+ pending.set(actualKey, notify);
59
+ }
60
+ }),
61
+ fn,
62
+ // Flush on finish
63
+ () => {
64
+ for (const notify of pending.values()) {
65
+ notify();
66
+ }
67
+ }
68
+ );
69
+ }
70
+ function emitter(initialListeners) {
71
+ const listeners = /* @__PURE__ */ new Set([]);
72
+ let settledPayload;
73
+ let isSettled = false;
74
+ const noop = () => {
75
+ };
76
+ const doEmit = (payload, clear, lifo = false) => {
77
+ const copy = Array.from(listeners);
78
+ if (clear) {
79
+ listeners.clear();
80
+ }
81
+ const len = copy.length;
82
+ if (lifo) {
83
+ for (let i = len - 1; i >= 0; i--) {
84
+ copy[i](payload);
85
+ }
86
+ } else {
87
+ for (let i = 0; i < len; i++) {
88
+ copy[i](payload);
89
+ }
90
+ }
91
+ };
92
+ return {
93
+ get size() {
94
+ return listeners.size;
95
+ },
96
+ get settled() {
97
+ return isSettled;
98
+ },
99
+ /**
100
+ * Adds one or more listeners to the emitter.
101
+ *
102
+ * The listener(s) will be called whenever `emit()` is called.
103
+ * Returns an unsubscribe function that removes the listener(s).
104
+ *
105
+ * **Important**: The unsubscribe function is idempotent - calling it multiple
106
+ * times is safe and won't cause errors. If the same listener is added multiple
107
+ * times, it will only be called once per emit (Set deduplication).
108
+ *
109
+ * **Settled behavior**: If the emitter is settled, listeners are called
110
+ * immediately with the settled payload and a no-op unsubscribe is returned.
111
+ *
112
+ * @param newListeners - Single listener or array of listeners to add
113
+ * @returns An unsubscribe function that removes the listener(s)
114
+ */
115
+ on(...args) {
116
+ let newListeners = [];
117
+ if (args.length < 2) {
118
+ newListeners = Array.isArray(args[0]) ? args[0] : [args[0]];
119
+ } else {
120
+ const map = args[0];
121
+ const sourceListeners = Array.isArray(args[1]) ? args[1] : [args[1]];
122
+ newListeners = [
123
+ (value) => {
124
+ const mappedValue = map(value);
125
+ if (mappedValue) {
126
+ for (const listener of sourceListeners) {
127
+ listener(mappedValue.value);
128
+ }
129
+ }
130
+ }
131
+ ];
132
+ }
133
+ if (isSettled) {
134
+ for (const listener of newListeners) {
135
+ listener(settledPayload);
136
+ }
137
+ return noop;
138
+ }
139
+ for (const listener of newListeners) {
140
+ listeners.add(listener);
141
+ }
142
+ return () => {
143
+ for (const listener of newListeners) {
144
+ listeners.delete(listener);
145
+ }
146
+ };
147
+ },
148
+ /**
149
+ * Emits an event to all registered listeners.
150
+ *
151
+ * **Important**: Creates a snapshot of listeners before iterating to ensure
152
+ * that modifications during emission (adding/removing listeners) don't affect
153
+ * the current emission cycle. This prevents:
154
+ * - New listeners added during emission from being called immediately
155
+ * - Issues with listeners that unsubscribe during emission
156
+ *
157
+ * Performance: For typical use cases (< 20 listeners), Array.from() overhead
158
+ * is negligible compared to calling the listener functions themselves.
159
+ *
160
+ * **Settled behavior**: After `settle()` is called, `emit()` becomes a no-op.
161
+ *
162
+ * @param payload - The value to pass to all listeners
163
+ */
164
+ emit(payload) {
165
+ if (isSettled) return;
166
+ doEmit(payload, false);
167
+ },
168
+ emitLifo(payload) {
169
+ if (isSettled) return;
170
+ doEmit(payload, false, true);
171
+ },
172
+ /**
173
+ * Removes all registered listeners.
174
+ *
175
+ * After calling `clear()`, no listeners will be notified until new ones
176
+ * are added via `on()`.
177
+ */
178
+ clear() {
179
+ listeners.clear();
180
+ },
181
+ /**
182
+ * Emits an event to all registered listeners and then clears all listeners.
183
+ *
184
+ * **Settled behavior**: After `settle()` is called, `emitAndClear()` becomes a no-op.
185
+ *
186
+ * @param payload - The value to pass to all listeners
187
+ */
188
+ emitAndClear(payload) {
189
+ if (isSettled) return;
190
+ doEmit(payload, true);
191
+ },
192
+ emitAndClearLifo(payload) {
193
+ if (isSettled) return;
194
+ doEmit(payload, true, true);
195
+ },
196
+ /**
197
+ * Emit to all listeners, clear, and "settle" the emitter.
198
+ *
199
+ * After settling:
200
+ * - Any new `on()` call immediately invokes the listener with the settled payload
201
+ * - Returns a no-op unsubscribe function
202
+ * - `emit()` and `emitAndClear()` become no-ops
203
+ *
204
+ * **Important**: `isSettled` is set BEFORE emitting so that listeners
205
+ * added during emission see the settled state and get called immediately.
206
+ *
207
+ * @param payload - The final value to pass to all listeners
208
+ */
209
+ settle(payload) {
210
+ if (isSettled) return;
211
+ settledPayload = payload;
212
+ isSettled = true;
213
+ doEmit(payload, true);
214
+ }
215
+ };
216
+ }
217
+ export {
218
+ hasWriteHook as a,
219
+ batch as b,
220
+ trackWrite as c,
221
+ emitter as e,
222
+ getHooks as g,
223
+ hasReadHook as h,
224
+ scheduleNotification as s,
225
+ trackRead as t,
226
+ untrack as u,
227
+ withHooks as w
228
+ };
@@ -3,10 +3,13 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
  import { memo, useMemo, createElement, createContext, useContext, useReducer, useRef, useEffect, useLayoutEffect, useState, forwardRef } from "react";
5
5
  import { container } from "../storion.js";
6
- import { applyExcept, applyFor, compose, createLoggingMiddleware, createResolver, createValidationMiddleware, effect, forStores, pick, trigger, when } from "../storion.js";
7
- import { w as withHooks, i as isSpec, S as STORION_TYPE, r as resolveEquality, s as store } from "../store-DS-4XdM6.js";
8
- import { m, p, n, g, a, j, d, h, l, f, k, b, x, o, q, u, v, t } from "../store-DS-4XdM6.js";
6
+ import { applyExcept, applyFor, compose, createLoggingMiddleware, createResolver, createValidationMiddleware, forStores, pick, trigger, when } from "../storion.js";
7
+ import { i as isSpec, S as STORION_TYPE, r as resolveEquality, s as store } from "../store-BroaE7p4.js";
8
+ import { n, l, g, a, h, d, f, k, e, j, b, p, m, o, u, w } from "../store-BroaE7p4.js";
9
+ import { w as withHooks } from "../emitter-BA44OHdL.js";
10
+ import { b as b2, u as u2 } from "../emitter-BA44OHdL.js";
9
11
  import { jsx } from "react/jsx-runtime";
12
+ import { e as e2 } from "../effect-DPAYSuW3.js";
10
13
  const StoreContext = createContext(null);
11
14
  const StoreProvider = memo(
12
15
  ({ container: value, children }) => {
@@ -78,7 +81,7 @@ const useIsomorphicLayoutEffect = isServer ? useEffect : useLayoutEffect;
78
81
  const shouldScheduleDispose = !isServer && typeof useLayoutEffect === "function" && dev();
79
82
  function useLocalStore(spec) {
80
83
  var _a;
81
- const [, forceUpdate] = useReducer((x2) => x2 + 1, 0);
84
+ const [, forceUpdate] = useReducer((x) => x + 1, 0);
82
85
  const prevControllerRef = useRef(null);
83
86
  const controller = useMemo(() => new LocalStoreController(spec), [spec]);
84
87
  if (prevControllerRef.current !== controller) {
@@ -188,7 +191,7 @@ class LocalStoreController {
188
191
  }
189
192
  }
190
193
  function useStoreWithContainer(selector, container2) {
191
- const [, forceUpdate] = useReducer((x2) => x2 + 1, 0);
194
+ const [, forceUpdate] = useReducer((x) => x + 1, 0);
192
195
  const [refs] = useState(() => ({
193
196
  fresh: void 0,
194
197
  stableFns: /* @__PURE__ */ new Map(),
@@ -386,38 +389,38 @@ export {
386
389
  StoreProvider,
387
390
  applyExcept,
388
391
  applyFor,
389
- m as batch,
392
+ b2 as batch,
390
393
  compose,
391
394
  container,
392
395
  create,
393
396
  createLoggingMiddleware,
394
397
  createResolver,
395
398
  createValidationMiddleware,
396
- p as deepEqual,
397
- effect,
398
- n as equality,
399
+ n as deepEqual,
400
+ e2 as effect,
401
+ l as equality,
399
402
  forStores,
400
403
  g as getKind,
401
404
  a as is,
402
- j as isAction,
405
+ h as isAction,
403
406
  d as isContainer,
404
- h as isFocus,
405
- l as isSelectorContext,
407
+ f as isFocus,
408
+ k as isSelectorContext,
406
409
  isSpec,
407
- f as isStore,
408
- k as isStoreContext,
410
+ e as isStore,
411
+ j as isStoreContext,
409
412
  b as isStorion,
410
- x as isWrappedFn,
413
+ p as isWrappedFn,
411
414
  pick,
412
- o as shallowEqual,
415
+ m as shallowEqual,
413
416
  store,
414
- q as strictEqual,
417
+ o as strictEqual,
415
418
  trigger,
416
- u as untrack,
417
- v as unwrapFn,
419
+ u2 as untrack,
420
+ u as unwrapFn,
418
421
  useContainer,
419
422
  useStore,
420
423
  when,
421
424
  withStore,
422
- t as wrapFn
425
+ w as wrapFn
423
426
  };