storion 0.3.0 → 0.5.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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACL,YAAY,EACZ,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,YAAY,EAEjB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAEf,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,QAAQ,EACb,KAAK,eAAe,GACrB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,EAAE,EACF,SAAS,EACT,OAAO,EACP,MAAM,EACN,WAAW,EACX,OAAO,EACP,OAAO,EACP,QAAQ,EACR,cAAc,EACd,iBAAiB,GAClB,MAAM,MAAM,CAAC;AAGd,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAGjD,O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j, 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, 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-XP2pujaJ.js";
8
- import { m, p, n, g, a, j, d, h, l, f, k, b, x, o, q, u, v, t } from "../store-XP2pujaJ.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-CwA4YTVb.js";
8
+ import { m, p, n, g, a, j, d, h, l, f, k, b, x, o, q, u, v, t } from "../store-CwA4YTVb.js";
9
9
  import { jsx } from "react/jsx-runtime";
10
10
  const StoreContext = createContext(null);
11
11
  const StoreProvider = memo(
@@ -161,7 +161,7 @@ class LocalStoreController {
161
161
  if (depsCount > 0) {
162
162
  this.dispose();
163
163
  throw new Error(
164
- `Local store must not have dependencies, but "${this.spec.name}" has ${depsCount} dependencies. Use useStore() with a global container for stores with dependencies.`
164
+ `Local store must not have dependencies, but "${this.spec.displayName}" has ${depsCount} dependencies. Use useStore() with a global container for stores with dependencies.`
165
165
  );
166
166
  }
167
167
  if (shouldScheduleDispose) {
@@ -392,6 +392,7 @@ export {
392
392
  p as deepEqual,
393
393
  effect,
394
394
  n as equality,
395
+ forStores,
395
396
  g as getKind,
396
397
  a as is,
397
398
  j as isAction,
@@ -1898,6 +1898,9 @@ function createStoreContext(options) {
1898
1898
  reset() {
1899
1899
  reset();
1900
1900
  },
1901
+ onDispose(callback) {
1902
+ onDispose == null ? void 0 : onDispose(callback);
1903
+ },
1901
1904
  mixin(mixin, ...args) {
1902
1905
  if (!isSetupPhase()) {
1903
1906
  throw new Error(
@@ -2210,13 +2213,17 @@ function collection(createItem, initialItems) {
2210
2213
  };
2211
2214
  }
2212
2215
  function store(options) {
2213
- const name = options.name ?? generateSpecName();
2216
+ const displayName = options.name ?? generateSpecName();
2214
2217
  const spec = function(resolver) {
2215
- return createStoreInstance(spec, resolver, {});
2218
+ return createStoreInstance(
2219
+ spec,
2220
+ resolver,
2221
+ {}
2222
+ );
2216
2223
  };
2217
2224
  Object.defineProperties(spec, {
2218
2225
  [STORION_TYPE]: { value: "store.spec", enumerable: false },
2219
- name: { value: name, enumerable: true, writable: false },
2226
+ displayName: { value: displayName, enumerable: true, writable: false },
2220
2227
  options: { value: options, enumerable: true, writable: false }
2221
2228
  });
2222
2229
  return spec;
@@ -2224,7 +2231,7 @@ function store(options) {
2224
2231
  function createStoreInstance(spec, resolver, instanceOptions = {}) {
2225
2232
  var _a;
2226
2233
  const options = spec.options;
2227
- const storeId = generateStoreId(spec.name);
2234
+ const storeId = generateStoreId(spec.displayName);
2228
2235
  let disposed = false;
2229
2236
  let currentState = { ...options.state };
2230
2237
  let initialState = currentState;
package/dist/storion.js CHANGED
@@ -1,5 +1,146 @@
1
- import { e as emitter, i as isSpec, S as STORION_TYPE, u as untrack, y as getHooks, w as withHooks, r as resolveEquality, z as scheduleNotification, v as unwrapFn, o as shallowEqual } from "./store-XP2pujaJ.js";
2
- import { m, p, n, g, a, j, d, h, l, f, k, b, x, s, q, t } from "./store-XP2pujaJ.js";
1
+ import { i as isSpec, e as emitter, u as untrack, S as STORION_TYPE, y as getHooks, w as withHooks, r as resolveEquality, z as scheduleNotification, v as unwrapFn, o as shallowEqual } from "./store-CwA4YTVb.js";
2
+ import { m, p, n, g, a, j, d, h, l, f, k, b, x, s, q, t } from "./store-CwA4YTVb.js";
3
+ function extractDisplayName(factory) {
4
+ if (isSpec(factory)) {
5
+ return factory.displayName;
6
+ }
7
+ if (typeof factory.displayName === "string" && factory.displayName) {
8
+ return factory.displayName;
9
+ }
10
+ if (factory.name && factory.name !== "") {
11
+ return factory.name;
12
+ }
13
+ return void 0;
14
+ }
15
+ function createResolver(options = {}) {
16
+ const {
17
+ middleware = [],
18
+ parent,
19
+ invokeResolver: invokeResolverOption
20
+ } = options;
21
+ const cache2 = /* @__PURE__ */ new Map();
22
+ const overrides = /* @__PURE__ */ new Map();
23
+ const resolve = (factory) => overrides.get(factory) ?? factory;
24
+ const invoke = (factory, resolverForCtx) => {
25
+ const isStoreSpec = isSpec(factory);
26
+ const displayName = extractDisplayName(factory);
27
+ const chain = middleware.reduceRight(
28
+ (next, mw) => () => {
29
+ if (isStoreSpec) {
30
+ const ctx = {
31
+ type: "store",
32
+ factory,
33
+ resolver: resolverForCtx,
34
+ next,
35
+ displayName,
36
+ spec: factory
37
+ };
38
+ return mw(ctx);
39
+ } else {
40
+ const ctx = {
41
+ type: "factory",
42
+ factory,
43
+ resolver: resolverForCtx,
44
+ next,
45
+ displayName
46
+ };
47
+ return mw(ctx);
48
+ }
49
+ },
50
+ () => factory(resolverForCtx)
51
+ );
52
+ return chain();
53
+ };
54
+ const resolver = {
55
+ get(factory) {
56
+ if (cache2.has(factory)) {
57
+ return cache2.get(factory);
58
+ }
59
+ if (parent && overrides.size === 0 && parent.has(factory)) {
60
+ return parent.get(factory);
61
+ }
62
+ const instance = invoke(
63
+ resolve(factory),
64
+ invokeResolverOption ?? resolver
65
+ );
66
+ cache2.set(factory, instance);
67
+ return instance;
68
+ },
69
+ create(factory) {
70
+ return invoke(resolve(factory), invokeResolverOption ?? resolver);
71
+ },
72
+ set(factory, override) {
73
+ overrides.set(factory, override);
74
+ cache2.delete(factory);
75
+ },
76
+ has(factory) {
77
+ const inParent = parent && overrides.size === 0 && parent.has(factory);
78
+ return cache2.has(factory) || (inParent ?? false);
79
+ },
80
+ tryGet(factory) {
81
+ if (cache2.has(factory)) {
82
+ return cache2.get(factory);
83
+ }
84
+ if (parent && overrides.size === 0) {
85
+ return parent.tryGet(factory);
86
+ }
87
+ return void 0;
88
+ },
89
+ delete(factory) {
90
+ const instance = cache2.get(factory);
91
+ if (instance) {
92
+ if (instance && typeof instance === "object" && "dispose" in instance && typeof instance.dispose === "function") {
93
+ instance.dispose();
94
+ }
95
+ cache2.delete(factory);
96
+ return true;
97
+ }
98
+ return false;
99
+ },
100
+ clear() {
101
+ for (const instance of cache2.values()) {
102
+ if (instance && typeof instance === "object" && "dispose" in instance && typeof instance.dispose === "function") {
103
+ instance.dispose();
104
+ }
105
+ }
106
+ cache2.clear();
107
+ },
108
+ scope(scopeOptions = {}) {
109
+ return createResolver({
110
+ middleware: scopeOptions.middleware ?? middleware,
111
+ parent: resolver,
112
+ ...scopeOptions
113
+ });
114
+ }
115
+ };
116
+ return resolver;
117
+ }
118
+ function when(predicate, middleware) {
119
+ return (ctx) => {
120
+ if (predicate(ctx.factory)) {
121
+ return middleware(ctx);
122
+ }
123
+ return ctx.next();
124
+ };
125
+ }
126
+ function createLoggingMiddleware(prefix = "Resolver") {
127
+ return (ctx) => {
128
+ const name = ctx.factory.name || "anonymous";
129
+ console.log(`[${prefix}] Creating: ${name}`);
130
+ const start = performance.now();
131
+ const result = ctx.next();
132
+ const duration = (performance.now() - start).toFixed(2);
133
+ console.log(`[${prefix}] Created: ${name} (${duration}ms)`);
134
+ return result;
135
+ };
136
+ }
137
+ function createValidationMiddleware(validate) {
138
+ return (ctx) => {
139
+ const result = ctx.next();
140
+ validate(result, ctx.factory);
141
+ return result;
142
+ };
143
+ }
3
144
  let defaultMiddlewareConfig = {};
4
145
  const container = function(options = {}) {
5
146
  const middleware = [
@@ -7,156 +148,93 @@ const container = function(options = {}) {
7
148
  ...options.middleware ?? [],
8
149
  ...defaultMiddlewareConfig.post ?? []
9
150
  ];
10
- const cache2 = /* @__PURE__ */ new Map();
11
- const factoryCache = /* @__PURE__ */ new Map();
12
151
  const instancesById = /* @__PURE__ */ new Map();
13
- const overrides = /* @__PURE__ */ new Map();
14
- const factoryOverrides = /* @__PURE__ */ new Map();
15
152
  const creationOrder = [];
16
153
  const createEmitter = emitter();
17
154
  const disposeEmitter = emitter();
18
- const creating = /* @__PURE__ */ new Set();
19
- const creatingFactories = /* @__PURE__ */ new Set();
20
155
  const parent = options._parent;
21
- const resolve = (spec) => overrides.get(spec) ?? spec;
22
- function buildMiddlewareChain() {
23
- let chain = (spec, next) => next(spec);
24
- for (let i = middleware.length - 1; i >= 0; i--) {
25
- const currentMiddleware = middleware[i];
26
- const nextInChain = chain;
27
- chain = (spec, next) => currentMiddleware(spec, (s2) => nextInChain(s2, next));
156
+ let containerApi;
157
+ const internalResolver = createResolver({
158
+ middleware,
159
+ parent,
160
+ get invokeResolver() {
161
+ return containerApi;
28
162
  }
29
- return (spec) => chain(spec, (s2) => s2(containerApi));
30
- }
31
- const createWithMiddleware = buildMiddlewareChain();
32
- function createInstance(spec) {
33
- const instance = untrack(() => createWithMiddleware(spec));
163
+ });
164
+ function trackStore(spec, instance) {
165
+ instancesById.set(instance.id, instance);
166
+ creationOrder.push(spec);
34
167
  if (typeof instance.onDispose === "function") {
35
168
  instance.onDispose(() => {
36
169
  disposeEmitter.emit(instance);
37
- cache2.delete(spec);
38
170
  instancesById.delete(instance.id);
171
+ internalResolver.delete(spec);
39
172
  const index = creationOrder.indexOf(spec);
40
173
  if (index !== -1) {
41
174
  creationOrder.splice(index, 1);
42
175
  }
43
176
  });
44
177
  }
45
- return instance;
178
+ createEmitter.emit(instance);
46
179
  }
47
- const containerApi = {
180
+ containerApi = {
48
181
  [STORION_TYPE]: "container",
49
- // ========================================================================
50
- // Resolver Methods
51
- // ========================================================================
52
- // Implementation handles multiple overloads
182
+ // Get by ID or factory/spec
53
183
  get(specOrIdOrFactory) {
54
184
  if (typeof specOrIdOrFactory === "string") {
55
185
  return instancesById.get(specOrIdOrFactory);
56
186
  }
57
- if (!isSpec(specOrIdOrFactory)) {
58
- const factory = specOrIdOrFactory;
59
- if (factoryCache.has(factory)) {
60
- return factoryCache.get(factory);
61
- }
62
- if (parent && factoryOverrides.size === 0 && "tryGet" in parent && typeof parent.tryGet === "function") {
63
- const parentInstance = parent.tryGet(factory);
64
- if (parentInstance !== void 0) {
65
- return parentInstance;
66
- }
67
- }
68
- if (creatingFactories.has(factory)) {
69
- const name = factory.name || "anonymous";
70
- throw new Error(
71
- `Circular dependency detected: factory "${name}" is being created while already in creation stack.`
72
- );
73
- }
74
- creatingFactories.add(factory);
75
- try {
76
- const mapped = factoryOverrides.get(factory) ?? factory;
77
- const instance = mapped(containerApi);
78
- factoryCache.set(factory, instance);
79
- return instance;
80
- } finally {
81
- creatingFactories.delete(factory);
82
- }
83
- }
84
- const spec = specOrIdOrFactory;
85
- if (cache2.has(spec)) {
86
- return cache2.get(spec);
87
- }
88
- if (parent && overrides.size === 0 && parent.has(spec)) {
89
- return parent.get(spec);
90
- }
91
- if (creating.has(spec)) {
92
- const name = spec.name ?? "unknown";
93
- throw new Error(
94
- `Circular dependency detected: "${name}" is being created while already in creation stack.`
95
- );
96
- }
97
- creating.add(spec);
98
- try {
99
- const mapped = resolve(spec);
100
- const instance = createInstance(mapped);
101
- cache2.set(spec, instance);
102
- instancesById.set(instance.id, instance);
103
- creationOrder.push(spec);
104
- createEmitter.emit(instance);
105
- return instance;
106
- } finally {
107
- creating.delete(spec);
187
+ const wasCached = internalResolver.has(specOrIdOrFactory);
188
+ const instance = untrack(() => internalResolver.get(specOrIdOrFactory));
189
+ if (!wasCached && isSpec(specOrIdOrFactory)) {
190
+ trackStore(specOrIdOrFactory, instance);
108
191
  }
192
+ return instance;
109
193
  },
110
- // Implementation handles both StoreSpec and Factory overloads
111
194
  create(specOrFactory) {
112
- if (!isSpec(specOrFactory)) {
113
- const factory = specOrFactory;
114
- const mapped2 = factoryOverrides.get(factory) ?? factory;
115
- return mapped2(containerApi);
195
+ const instance = untrack(() => internalResolver.create(specOrFactory));
196
+ if (isSpec(specOrFactory)) {
197
+ const storeInstance = instance;
198
+ instancesById.set(storeInstance.id, storeInstance);
199
+ if (typeof storeInstance.onDispose === "function") {
200
+ storeInstance.onDispose(() => {
201
+ disposeEmitter.emit(storeInstance);
202
+ instancesById.delete(storeInstance.id);
203
+ });
204
+ }
116
205
  }
117
- const mapped = resolve(specOrFactory);
118
- return createInstance(mapped);
206
+ return instance;
119
207
  },
120
208
  set(spec, override) {
121
- overrides.set(spec, override);
122
- const existing = cache2.get(spec);
209
+ const existing = internalResolver.tryGet(spec);
123
210
  if (existing) {
124
211
  existing.dispose();
125
212
  }
213
+ internalResolver.set(spec, override);
126
214
  },
127
215
  has(spec) {
128
- const inParent = parent && overrides.size === 0 && parent.has(spec);
129
- return cache2.has(spec) || (inParent ?? false);
216
+ return internalResolver.has(spec);
130
217
  },
131
218
  tryGet(spec) {
132
- if (cache2.has(spec)) {
133
- return cache2.get(spec);
134
- }
135
- if (parent && overrides.size === 0) {
136
- return parent.tryGet(spec);
137
- }
138
- return void 0;
219
+ return internalResolver.tryGet(spec);
139
220
  },
140
221
  delete(spec) {
141
- const instance = cache2.get(spec);
222
+ const instance = internalResolver.tryGet(spec);
142
223
  if (instance) {
143
224
  instance.dispose();
144
225
  return true;
145
226
  }
146
227
  return false;
147
228
  },
148
- // ========================================================================
149
- // Container-Specific Methods
150
- // ========================================================================
151
229
  clear() {
152
230
  const specs = [...creationOrder].reverse();
153
231
  for (const spec of specs) {
154
- const instance = cache2.get(spec);
232
+ const instance = internalResolver.tryGet(spec);
155
233
  if (instance) {
156
234
  instance.dispose();
157
235
  }
158
236
  }
159
- cache2.clear();
237
+ internalResolver.clear();
160
238
  instancesById.clear();
161
239
  creationOrder.length = 0;
162
240
  },
@@ -184,96 +262,6 @@ defaultsFn.clear = () => {
184
262
  defaultMiddlewareConfig = {};
185
263
  };
186
264
  container.defaults = defaultsFn;
187
- function createResolver(options = {}) {
188
- const { middleware = [], parent } = options;
189
- const cache2 = /* @__PURE__ */ new Map();
190
- const overrides = /* @__PURE__ */ new Map();
191
- const resolve = (factory) => overrides.get(factory) ?? factory;
192
- const invoke = (factory, resolver2) => {
193
- const chain = middleware.reduceRight(
194
- (next, mw) => () => mw({
195
- factory,
196
- resolver: resolver2,
197
- next
198
- }),
199
- () => factory(resolver2)
200
- );
201
- return chain();
202
- };
203
- const resolver = {
204
- get(factory) {
205
- if (cache2.has(factory)) {
206
- return cache2.get(factory);
207
- }
208
- if (parent && overrides.size === 0 && parent.has(factory)) {
209
- return parent.get(factory);
210
- }
211
- const instance = invoke(resolve(factory), resolver);
212
- cache2.set(factory, instance);
213
- return instance;
214
- },
215
- create(factory) {
216
- return invoke(resolve(factory), resolver);
217
- },
218
- set(factory, override) {
219
- overrides.set(factory, override);
220
- cache2.delete(factory);
221
- },
222
- has(factory) {
223
- const inParent = parent && overrides.size === 0 && parent.has(factory);
224
- return cache2.has(factory) || (inParent ?? false);
225
- },
226
- tryGet(factory) {
227
- if (cache2.has(factory)) {
228
- return cache2.get(factory);
229
- }
230
- if (parent && overrides.size === 0) {
231
- return parent.tryGet(factory);
232
- }
233
- return void 0;
234
- },
235
- delete(factory) {
236
- return cache2.delete(factory);
237
- },
238
- clear() {
239
- cache2.clear();
240
- },
241
- scope(scopeOptions = {}) {
242
- return createResolver({
243
- middleware: scopeOptions.middleware ?? middleware,
244
- parent: resolver,
245
- ...scopeOptions
246
- });
247
- }
248
- };
249
- return resolver;
250
- }
251
- function when(predicate, middleware) {
252
- return (ctx) => {
253
- if (predicate(ctx.factory)) {
254
- return middleware(ctx);
255
- }
256
- return ctx.next();
257
- };
258
- }
259
- function createLoggingMiddleware(prefix = "Resolver") {
260
- return (ctx) => {
261
- const name = ctx.factory.name || "anonymous";
262
- console.log(`[${prefix}] Creating: ${name}`);
263
- const start = performance.now();
264
- const result = ctx.next();
265
- const duration = (performance.now() - start).toFixed(2);
266
- console.log(`[${prefix}] Created: ${name} (${duration}ms)`);
267
- return result;
268
- };
269
- }
270
- function createValidationMiddleware(validate) {
271
- return (ctx) => {
272
- const result = ctx.next();
273
- validate(result, ctx.factory);
274
- return result;
275
- };
276
- }
277
265
  let pickIdCounter = 0;
278
266
  function pick(selector, equality) {
279
267
  var _a;
@@ -337,10 +325,16 @@ function pick(selector, equality) {
337
325
  });
338
326
  return currentValue;
339
327
  }
340
- function createEffectContext(nth, isStale) {
328
+ function isPromiseLike(value) {
329
+ return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
330
+ }
331
+ function createEffectContext(nth) {
341
332
  let cleanupEmitter = null;
342
333
  let abortController = null;
334
+ let isStale = false;
343
335
  const runCleanups = () => {
336
+ if (isStale) return;
337
+ isStale = true;
344
338
  if (abortController) {
345
339
  abortController.abort();
346
340
  abortController = null;
@@ -349,6 +343,22 @@ function createEffectContext(nth, isStale) {
349
343
  cleanupEmitter.emitAndClearLifo();
350
344
  }
351
345
  };
346
+ function wrapPromise(promise) {
347
+ return new Promise((resolve, reject) => {
348
+ promise.then(
349
+ (value) => {
350
+ if (!isStale) {
351
+ resolve(value);
352
+ }
353
+ },
354
+ (error) => {
355
+ if (!isStale) {
356
+ reject(error);
357
+ }
358
+ }
359
+ );
360
+ });
361
+ }
352
362
  const context = {
353
363
  nth,
354
364
  get signal() {
@@ -365,24 +375,17 @@ function createEffectContext(nth, isStale) {
365
375
  },
366
376
  safe(promiseOrCallback) {
367
377
  if (promiseOrCallback instanceof Promise) {
368
- return new Promise((resolve, reject) => {
369
- promiseOrCallback.then(
370
- (value) => {
371
- if (!isStale()) {
372
- resolve(value);
373
- }
374
- },
375
- (error) => {
376
- if (!isStale()) {
377
- reject(error);
378
- }
379
- }
380
- );
381
- });
378
+ return wrapPromise(promiseOrCallback);
382
379
  }
383
380
  return (...args) => {
384
- if (!isStale()) {
385
- return promiseOrCallback(...args);
381
+ if (!isStale) {
382
+ const result = promiseOrCallback(
383
+ ...args
384
+ );
385
+ if (isPromiseLike(result)) {
386
+ return wrapPromise(result);
387
+ }
388
+ return result;
386
389
  }
387
390
  return void 0;
388
391
  };
@@ -525,11 +528,10 @@ function effect(fn, options) {
525
528
  }
526
529
  newTrackedDeps = /* @__PURE__ */ new Map();
527
530
  writtenProps.clear();
528
- const isStale = () => isDisposed || runGeneration !== currentGeneration;
529
531
  let lazyContext = null;
530
532
  const getOrCreateContext = () => {
531
533
  if (!lazyContext) {
532
- lazyContext = createEffectContext(currentGeneration, isStale);
534
+ lazyContext = createEffectContext(currentGeneration);
533
535
  }
534
536
  return lazyContext;
535
537
  };
@@ -619,43 +621,55 @@ function patternToPredicate(pattern) {
619
621
  function patternsToPredicate(patterns) {
620
622
  if (Array.isArray(patterns)) {
621
623
  const predicates = patterns.map(patternToPredicate);
622
- return (spec) => predicates.some((p2) => p2(spec.name));
624
+ return (ctx) => ctx.displayName !== void 0 && predicates.some((p2) => p2(ctx.displayName));
623
625
  }
624
626
  const predicate = patternToPredicate(patterns);
625
- return (spec) => predicate(spec.name);
627
+ return (ctx) => ctx.displayName !== void 0 && predicate(ctx.displayName);
626
628
  }
627
629
  function compose(...middlewares) {
628
630
  if (middlewares.length === 0) {
629
- return (spec, next) => next(spec);
631
+ return (ctx) => ctx.next();
630
632
  }
631
633
  if (middlewares.length === 1) {
632
634
  return middlewares[0];
633
635
  }
634
- return (spec, next) => {
635
- let chain = next;
636
- for (let i = middlewares.length - 1; i >= 0; i--) {
637
- const middleware = middlewares[i];
638
- const prevChain = chain;
639
- chain = (s2) => middleware(s2, prevChain);
640
- }
641
- return chain(spec);
636
+ return (ctx) => {
637
+ let index = 0;
638
+ const executeNext = () => {
639
+ if (index >= middlewares.length) {
640
+ return ctx.next();
641
+ }
642
+ const currentMiddleware = middlewares[index];
643
+ index++;
644
+ const wrappedCtx = ctx.type === "store" ? { ...ctx, next: executeNext } : { ...ctx, next: executeNext };
645
+ return currentMiddleware(wrappedCtx);
646
+ };
647
+ return executeNext();
642
648
  };
643
649
  }
644
650
  function applyFor(predicateOrPatterns, middleware) {
645
651
  const predicate = typeof predicateOrPatterns === "function" ? predicateOrPatterns : patternsToPredicate(predicateOrPatterns);
646
652
  const composedMiddleware = Array.isArray(middleware) ? compose(...middleware) : middleware;
647
- return (spec, next) => {
648
- if (predicate(spec)) {
649
- return composedMiddleware(spec, next);
653
+ return (ctx) => {
654
+ if (predicate(ctx)) {
655
+ return composedMiddleware(ctx);
650
656
  }
651
- return next(spec);
657
+ return ctx.next();
652
658
  };
653
659
  }
654
660
  function applyExcept(predicateOrPatterns, middleware) {
655
661
  const matchPredicate = typeof predicateOrPatterns === "function" ? predicateOrPatterns : patternsToPredicate(predicateOrPatterns);
656
- const invertedPredicate = (spec) => !matchPredicate(spec);
662
+ const invertedPredicate = (ctx) => !matchPredicate(ctx);
657
663
  return applyFor(invertedPredicate, middleware);
658
664
  }
665
+ function forStores(storeMiddleware) {
666
+ return (ctx) => {
667
+ if (ctx.type === "store") {
668
+ return storeMiddleware(ctx);
669
+ }
670
+ return ctx.next();
671
+ };
672
+ }
659
673
  const DEFAULT_KEY = {};
660
674
  const cache = /* @__PURE__ */ new Map();
661
675
  function getKeyCache(key) {
@@ -720,6 +734,7 @@ export {
720
734
  p as deepEqual,
721
735
  effect,
722
736
  n as equality,
737
+ forStores,
723
738
  g as getKind,
724
739
  a as is,
725
740
  j as isAction,