@wooksjs/event-core 0.7.4 → 0.7.5

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.
package/dist/index.cjs CHANGED
@@ -116,6 +116,17 @@ var EventContext = class {
116
116
  this.parent = options.parent;
117
117
  }
118
118
  /**
119
+ * Controls whether `get()`, `has()`, and `set()` traverse the parent chain
120
+ * for a given slot. Override in subclasses to isolate specific slots —
121
+ * returning `false` forces local computation/storage, preventing inheritance.
122
+ *
123
+ * @param _id - The numeric slot identifier (`accessor._id`)
124
+ * @returns `true` to allow parent traversal (default), `false` to block it
125
+ */
126
+ _shouldTraverseParent(_id) {
127
+ return true;
128
+ }
129
+ /**
119
130
  * Reads a value from a typed slot.
120
131
  * - For `Key<T>`: returns the previously `set` value, checking parent chain if not found locally.
121
132
  * - For `Cached<T>`: returns a cached result from this context or any parent. If not found
@@ -125,7 +136,7 @@ var EventContext = class {
125
136
  get(accessor) {
126
137
  const id = accessor._id;
127
138
  let val = this.slots.get(id);
128
- if (val === void 0 && this.parent) val = this.parent._findSlot(id);
139
+ if (val === void 0 && this.parent && this._shouldTraverseParent(id)) val = this.parent._findSlot(id);
129
140
  if (val !== void 0) {
130
141
  if (val === COMPUTING) throw new Error(`Circular dependency detected for "${accessor._name}"`);
131
142
  if (val instanceof CachedError) throw val.error;
@@ -155,7 +166,7 @@ var EventContext = class {
155
166
  this.slots.set(key$1._id, encoded);
156
167
  return;
157
168
  }
158
- if (this.parent && this.parent._setIfExists(key$1._id, encoded)) return;
169
+ if (this.parent && this._shouldTraverseParent(key$1._id) && this.parent._setIfExists(key$1._id, encoded)) return;
159
170
  this.slots.set(key$1._id, encoded);
160
171
  }
161
172
  /**
@@ -164,7 +175,8 @@ var EventContext = class {
164
175
  has(accessor) {
165
176
  const val = this.slots.get(accessor._id);
166
177
  if (val !== void 0 && val !== COMPUTING) return true;
167
- return this.parent?.has(accessor) ?? false;
178
+ if (this.parent && this._shouldTraverseParent(accessor._id)) return this.parent.has(accessor);
179
+ return false;
168
180
  }
169
181
  /**
170
182
  * Reads a value from a typed slot in this context only, ignoring parents.
@@ -293,7 +305,7 @@ function resetContextInjector() {
293
305
  //#region packages/event-core/src/storage.ts
294
306
  const STORAGE_KEY = Symbol.for("wooks.core.asyncStorage");
295
307
  const VERSION_KEY = Symbol.for("wooks.core.asyncStorage.version");
296
- const CURRENT_VERSION = "0.7.3";
308
+ const CURRENT_VERSION = "0.7.4";
297
309
  const _g = globalThis;
298
310
  if (_g[STORAGE_KEY]) {
299
311
  if (_g[VERSION_KEY] !== CURRENT_VERSION) throw new Error(`[wooks] Incompatible versions of @wooksjs/event-core detected: existing v${_g[VERSION_KEY]}, loading v${CURRENT_VERSION}. All packages must use the same @wooksjs/event-core version.`);
@@ -460,7 +472,7 @@ function defineEventKind(name, schema) {
460
472
  * composables (`useRequest`, `useResponse`, `useCookies`, etc.) are created with `defineWook`.
461
473
  *
462
474
  * @param factory - Receives the `EventContext` and returns the composable's public API
463
- * @returns A composable function `(ctx?: EventContext) => T`
475
+ * @returns A composable function with an exposed `_slot` for isolation
464
476
  *
465
477
  * @example
466
478
  * ```ts
@@ -478,8 +490,8 @@ function defineEventKind(name, schema) {
478
490
  * ```
479
491
  */
480
492
  function defineWook(factory) {
481
- const slot$1 = cached(factory);
482
- return (ctx) => (ctx ?? current()).get(slot$1);
493
+ const _slot = cached(factory);
494
+ return Object.assign((ctx) => (ctx ?? current()).get(_slot), { _slot });
483
495
  }
484
496
 
485
497
  //#endregion
package/dist/index.d.ts CHANGED
@@ -98,6 +98,15 @@ declare class EventContext {
98
98
  readonly parent?: EventContext;
99
99
  private slots;
100
100
  constructor(options: EventContextOptions);
101
+ /**
102
+ * Controls whether `get()`, `has()`, and `set()` traverse the parent chain
103
+ * for a given slot. Override in subclasses to isolate specific slots —
104
+ * returning `false` forces local computation/storage, preventing inheritance.
105
+ *
106
+ * @param _id - The numeric slot identifier (`accessor._id`)
107
+ * @returns `true` to allow parent traversal (default), `false` to block it
108
+ */
109
+ protected _shouldTraverseParent(_id: number): boolean;
101
110
  /**
102
111
  * Reads a value from a typed slot.
103
112
  * - For `Key<T>`: returns the previously `set` value, checking parent chain if not found locally.
@@ -230,6 +239,15 @@ declare function slot<T>(): SlotMarker<T>;
230
239
  */
231
240
  declare function defineEventKind<S extends Record<string, SlotMarker<any>>>(name: string, schema: S): EventKind<S>;
232
241
 
242
+ /**
243
+ * A composable function created by {@link defineWook}. Callable as `(ctx?) => T`,
244
+ * with an exposed `_slot` for advanced use cases such as slot isolation in child contexts.
245
+ */
246
+ interface WookComposable<T> {
247
+ (ctx?: EventContext): T;
248
+ /** The underlying `Cached` slot. Useful for building isolation lists in child contexts. */
249
+ readonly _slot: Cached<T>;
250
+ }
233
251
  /**
234
252
  * Creates a composable with per-event caching. The factory runs once per
235
253
  * `EventContext`; subsequent calls within the same event return the cached result.
@@ -238,7 +256,7 @@ declare function defineEventKind<S extends Record<string, SlotMarker<any>>>(name
238
256
  * composables (`useRequest`, `useResponse`, `useCookies`, etc.) are created with `defineWook`.
239
257
  *
240
258
  * @param factory - Receives the `EventContext` and returns the composable's public API
241
- * @returns A composable function `(ctx?: EventContext) => T`
259
+ * @returns A composable function with an exposed `_slot` for isolation
242
260
  *
243
261
  * @example
244
262
  * ```ts
@@ -255,7 +273,7 @@ declare function defineEventKind<S extends Record<string, SlotMarker<any>>>(name
255
273
  * const { username, profile } = useCurrentUser()
256
274
  * ```
257
275
  */
258
- declare function defineWook<T>(factory: (ctx: EventContext) => T): (ctx?: EventContext) => T;
276
+ declare function defineWook<T>(factory: (ctx: EventContext) => T): WookComposable<T>;
259
277
 
260
278
  /**
261
279
  * No-op base class for observability integration. Subclass and override
@@ -437,4 +455,4 @@ declare function createEventContext<R>(options: EventContextOptions, fn: () => R
437
455
  declare function createEventContext<S extends Record<string, any>, R>(options: EventContextOptions, kind: EventKind<S>, seeds: EventKindSeeds<EventKind<S>>, fn: () => R): R;
438
456
 
439
457
  export { ContextInjector, EventContext, cached, cachedBy, createEventContext, current, defineEventKind, defineWook, eventTypeKey, getContextInjector, key, replaceContextInjector, resetContextInjector, routeParamsKey, run, slot, tryGetCurrent, useEventId, useLogger, useRouteParams };
440
- export type { Accessor, Cached, EventContextOptions, EventKind, EventKindSeeds, Key, Logger, SlotMarker, TContextInjectorHooks };
458
+ export type { Accessor, Cached, EventContextOptions, EventKind, EventKindSeeds, Key, Logger, SlotMarker, TContextInjectorHooks, WookComposable };
package/dist/index.mjs CHANGED
@@ -93,6 +93,17 @@ var EventContext = class {
93
93
  this.parent = options.parent;
94
94
  }
95
95
  /**
96
+ * Controls whether `get()`, `has()`, and `set()` traverse the parent chain
97
+ * for a given slot. Override in subclasses to isolate specific slots —
98
+ * returning `false` forces local computation/storage, preventing inheritance.
99
+ *
100
+ * @param _id - The numeric slot identifier (`accessor._id`)
101
+ * @returns `true` to allow parent traversal (default), `false` to block it
102
+ */
103
+ _shouldTraverseParent(_id) {
104
+ return true;
105
+ }
106
+ /**
96
107
  * Reads a value from a typed slot.
97
108
  * - For `Key<T>`: returns the previously `set` value, checking parent chain if not found locally.
98
109
  * - For `Cached<T>`: returns a cached result from this context or any parent. If not found
@@ -102,7 +113,7 @@ var EventContext = class {
102
113
  get(accessor) {
103
114
  const id = accessor._id;
104
115
  let val = this.slots.get(id);
105
- if (val === void 0 && this.parent) val = this.parent._findSlot(id);
116
+ if (val === void 0 && this.parent && this._shouldTraverseParent(id)) val = this.parent._findSlot(id);
106
117
  if (val !== void 0) {
107
118
  if (val === COMPUTING) throw new Error(`Circular dependency detected for "${accessor._name}"`);
108
119
  if (val instanceof CachedError) throw val.error;
@@ -132,7 +143,7 @@ var EventContext = class {
132
143
  this.slots.set(key$1._id, encoded);
133
144
  return;
134
145
  }
135
- if (this.parent && this.parent._setIfExists(key$1._id, encoded)) return;
146
+ if (this.parent && this._shouldTraverseParent(key$1._id) && this.parent._setIfExists(key$1._id, encoded)) return;
136
147
  this.slots.set(key$1._id, encoded);
137
148
  }
138
149
  /**
@@ -141,7 +152,8 @@ var EventContext = class {
141
152
  has(accessor) {
142
153
  const val = this.slots.get(accessor._id);
143
154
  if (val !== void 0 && val !== COMPUTING) return true;
144
- return this.parent?.has(accessor) ?? false;
155
+ if (this.parent && this._shouldTraverseParent(accessor._id)) return this.parent.has(accessor);
156
+ return false;
145
157
  }
146
158
  /**
147
159
  * Reads a value from a typed slot in this context only, ignoring parents.
@@ -270,7 +282,7 @@ function resetContextInjector() {
270
282
  //#region packages/event-core/src/storage.ts
271
283
  const STORAGE_KEY = Symbol.for("wooks.core.asyncStorage");
272
284
  const VERSION_KEY = Symbol.for("wooks.core.asyncStorage.version");
273
- const CURRENT_VERSION = "0.7.3";
285
+ const CURRENT_VERSION = "0.7.4";
274
286
  const _g = globalThis;
275
287
  if (_g[STORAGE_KEY]) {
276
288
  if (_g[VERSION_KEY] !== CURRENT_VERSION) throw new Error(`[wooks] Incompatible versions of @wooksjs/event-core detected: existing v${_g[VERSION_KEY]}, loading v${CURRENT_VERSION}. All packages must use the same @wooksjs/event-core version.`);
@@ -437,7 +449,7 @@ function defineEventKind(name, schema) {
437
449
  * composables (`useRequest`, `useResponse`, `useCookies`, etc.) are created with `defineWook`.
438
450
  *
439
451
  * @param factory - Receives the `EventContext` and returns the composable's public API
440
- * @returns A composable function `(ctx?: EventContext) => T`
452
+ * @returns A composable function with an exposed `_slot` for isolation
441
453
  *
442
454
  * @example
443
455
  * ```ts
@@ -455,8 +467,8 @@ function defineEventKind(name, schema) {
455
467
  * ```
456
468
  */
457
469
  function defineWook(factory) {
458
- const slot$1 = cached(factory);
459
- return (ctx) => (ctx ?? current()).get(slot$1);
470
+ const _slot = cached(factory);
471
+ return Object.assign((ctx) => (ctx ?? current()).get(_slot), { _slot });
460
472
  }
461
473
 
462
474
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-core",
3
- "version": "0.7.4",
3
+ "version": "0.7.5",
4
4
  "description": "@wooksjs/event-core",
5
5
  "keywords": [
6
6
  "composables",