@wooksjs/event-core 0.7.2 → 0.7.3

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
@@ -293,7 +293,7 @@ function resetContextInjector() {
293
293
  //#region packages/event-core/src/storage.ts
294
294
  const STORAGE_KEY = Symbol.for("wooks.core.asyncStorage");
295
295
  const VERSION_KEY = Symbol.for("wooks.core.asyncStorage.version");
296
- const CURRENT_VERSION = "0.7.1";
296
+ const CURRENT_VERSION = "0.7.2";
297
297
  const _g = globalThis;
298
298
  if (_g[STORAGE_KEY]) {
299
299
  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.`);
package/dist/index.mjs CHANGED
@@ -270,7 +270,7 @@ function resetContextInjector() {
270
270
  //#region packages/event-core/src/storage.ts
271
271
  const STORAGE_KEY = Symbol.for("wooks.core.asyncStorage");
272
272
  const VERSION_KEY = Symbol.for("wooks.core.asyncStorage.version");
273
- const CURRENT_VERSION = "0.7.1";
273
+ const CURRENT_VERSION = "0.7.2";
274
274
  const _g = globalThis;
275
275
  if (_g[STORAGE_KEY]) {
276
276
  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.`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-core",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "@wooksjs/event-core",
5
5
  "keywords": [
6
6
  "composables",
@@ -175,39 +175,52 @@ This prevents subtle bugs from multiple context stores.
175
175
 
176
176
  ### Pattern: Custom adapter
177
177
 
178
- Build an adapter that creates contexts and runs handlers:
178
+ Build an adapter that creates contexts and runs handlers. Each adapter exports a **context factory** that hardcodes the kind and delegates to `createEventContext`:
179
179
 
180
180
  ```ts
181
- import { EventContext, defineEventKind, slot, run } from '@wooksjs/event-core'
181
+ import { createEventContext, defineEventKind, slot } from '@wooksjs/event-core'
182
+ import type { EventContextOptions, EventKindSeeds } from '@wooksjs/event-core'
182
183
 
183
184
  const myKind = defineEventKind('my-event', {
184
185
  data: slot<unknown>(),
185
186
  })
186
187
 
187
- function handleEvent(data: unknown, handler: () => unknown, parent?: EventContext) {
188
- const ctx = new EventContext({ logger: console, parent })
189
- ctx.seed(myKind, { data })
190
- return run(ctx, handler)
188
+ // Context factory same signature as createEventContext, minus the kind
189
+ export function createMyEventContext<R>(
190
+ options: EventContextOptions,
191
+ seeds: EventKindSeeds<typeof myKind>,
192
+ fn: () => R,
193
+ ): R {
194
+ return createEventContext(options, myKind, seeds, fn)
195
+ }
196
+
197
+ // Usage in the adapter
198
+ function handleEvent(data: unknown, handler: () => unknown) {
199
+ return createMyEventContext({ logger: console }, { data }, handler)
191
200
  }
192
201
  ```
193
202
 
203
+ All built-in adapters follow this pattern: `createHttpContext`, `createCliContext`, `createWsConnectionContext`, `createWsMessageContext`, `createWfContext`, `resumeWfContext`.
204
+
194
205
  ### Pattern: Child contexts with parent links
195
206
 
196
- Instead of attaching multiple kinds to a single context, create child contexts with parent links. Each child sees its own data plus everything in the parent chain:
207
+ Instead of attaching multiple kinds to a single context, create child contexts by passing `parent` in the options. Each child sees its own data plus everything in the parent chain:
197
208
 
198
209
  ```ts
199
210
  createEventContext({ logger }, httpKind, httpSeeds, () => {
200
211
  const parentCtx = current()
201
212
 
202
- // Create a child context for workflow-specific data
203
- const childCtx = new EventContext({ logger, parent: parentCtx })
204
- childCtx.seed(workflowKind, { triggerId: 'deploy-42', payload })
205
-
206
- run(childCtx, () => {
207
- // Both HTTP and workflow composables work
208
- const { method } = useRequest() // found via parent chain
209
- const { triggerId } = useWorkflow() // found locally
210
- })
213
+ // Create a child context linked to the HTTP parent
214
+ createEventContext(
215
+ { logger, parent: parentCtx },
216
+ workflowKind,
217
+ { triggerId: 'deploy-42', payload },
218
+ () => {
219
+ // Both HTTP and workflow composables work
220
+ const { method } = useRequest() // found via parent chain
221
+ const { triggerId } = useWorkflow() // found locally
222
+ },
223
+ )
211
224
  })
212
225
  ```
213
226
 
@@ -253,6 +266,8 @@ resetContextInjector()
253
266
 
254
267
  The injector's `with()` method wraps `createEventContext` callbacks. All adapters (HTTP, CLI, WS, WF) route through `createEventContext`, so installing an injector automatically instruments every event type.
255
268
 
269
+ Adapter callbacks **return their result** (sync value or Promise), so `with()` can track async handler completion for span lifecycle. A `TracingInjector` can detect thenable returns and attach `.then()/.catch()` to end spans when the handler settles — no separate `onEnd()` hook needed.
270
+
256
271
  ## Best Practices
257
272
 
258
273
  - Let adapters handle context creation — in handler code, just use composables
@@ -62,7 +62,7 @@ Every event (HTTP request, CLI invocation, workflow step) gets its own `EventCon
62
62
  | `useEventId(ctx?)` | composable | Lazy UUID per event |
63
63
  | `routeParamsKey` | key | Standard key for route params |
64
64
  | `eventTypeKey` | key | Standard key for event type name |
65
- | `ContextInjector` | class | Observability hook point (OpenTelemetry etc.) |
65
+ | `ContextInjector` | class | Observability hook point (OpenTelemetry etc.). Defaults to `null` — zero overhead until installed. |
66
66
  | `getContextInjector()` | function | Get current injector (returns `null` when none installed) |
67
67
  | `replaceContextInjector(ci)` | function | Install a custom injector (e.g. with OTel spans) |
68
68
  | `resetContextInjector()` | function | Reset injector back to `null` (disables instrumentation) |