@wooksjs/event-wf 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
@@ -60,40 +60,16 @@ function useWfState() {
60
60
 
61
61
  //#endregion
62
62
  //#region packages/event-wf/src/event-wf.ts
63
- const wfSeeds = (data) => ({
64
- schemaId: data.schemaId,
65
- stepId: data.stepId,
66
- inputContext: data.inputContext,
67
- indexes: data.indexes,
68
- input: data.input
69
- });
70
- /**
71
- * Creates a new event context for a fresh workflow execution.
72
- * When `parentCtx` is provided, the workflow creates a child context
73
- * linked to the parent, so step handlers can transparently access
74
- * composables from the parent scope (e.g. HTTP) via the parent chain.
75
- */
76
- function createWfContext(data, options, parentCtx) {
77
- const ctxOptions = parentCtx ? {
78
- ...options,
79
- parent: parentCtx
80
- } : options;
81
- return (fn) => (0, __wooksjs_event_core.createEventContext)(ctxOptions, wfKind, wfSeeds(data), () => {
63
+ /** Creates a WF event context for a fresh workflow execution and runs `fn` inside it. */
64
+ function createWfContext(options, seeds, fn) {
65
+ return (0, __wooksjs_event_core.createEventContext)(options, wfKind, seeds, () => {
82
66
  (0, __wooksjs_event_core.current)().set(resumeKey, false);
83
67
  return fn();
84
68
  });
85
69
  }
86
- /**
87
- * Creates an event context for resuming a paused workflow.
88
- * When `parentCtx` is provided, the workflow creates a child context
89
- * linked to the parent.
90
- */
91
- function resumeWfContext(data, options, parentCtx) {
92
- const ctxOptions = parentCtx ? {
93
- ...options,
94
- parent: parentCtx
95
- } : options;
96
- return (fn) => (0, __wooksjs_event_core.createEventContext)(ctxOptions, wfKind, wfSeeds(data), () => {
70
+ /** Creates a WF event context for resuming a paused workflow and runs `fn` inside it. */
71
+ function resumeWfContext(options, seeds, fn) {
72
+ return (0, __wooksjs_event_core.createEventContext)(options, wfKind, seeds, () => {
97
73
  (0, __wooksjs_event_core.current)().set(resumeKey, true);
98
74
  return fn();
99
75
  });
@@ -136,11 +112,13 @@ const wfShortcuts = {
136
112
  var WooksWf = class extends wooks.WooksAdapterBase {
137
113
  logger;
138
114
  wf;
115
+ eventContextOptions;
139
116
  constructor(opts, wooks$1) {
140
117
  super(wooks$1, opts?.logger, opts?.router);
141
118
  this.opts = opts;
142
119
  this.logger = opts?.logger || this.getLogger(`[wooks-wf]`);
143
120
  this.wf = new WooksWorkflow(this.wooks);
121
+ this.eventContextOptions = this.getEventContextOptions();
144
122
  }
145
123
  /** Attaches a spy function to observe workflow step execution. */
146
124
  attachSpy(fn) {
@@ -198,14 +176,19 @@ var WooksWf = class extends wooks.WooksAdapterBase {
198
176
  async _start(schemaId, inputContext, indexes, opts, parentCtx) {
199
177
  const { input, spy, cleanup } = opts ?? {};
200
178
  const resume = !!indexes?.length;
201
- const runInContext = (resume ? resumeWfContext : createWfContext)({
179
+ const factory = resume ? resumeWfContext : createWfContext;
180
+ const ctxOptions = parentCtx ? {
181
+ ...this.eventContextOptions,
182
+ parent: parentCtx
183
+ } : this.eventContextOptions;
184
+ const seeds = {
202
185
  inputContext,
203
186
  schemaId,
204
187
  stepId: null,
205
188
  indexes,
206
189
  input
207
- }, this.getEventContextOptions(), parentCtx);
208
- return runInContext(async () => {
190
+ };
191
+ return factory(ctxOptions, seeds, async () => {
209
192
  const { handlers: foundHandlers } = this.wooks.lookup("WF_FLOW", `/${schemaId}`.replace(/^\/+/u, "/"));
210
193
  const handlers = foundHandlers || this.opts?.onNotFound && [this.opts.onNotFound] || null;
211
194
  if (handlers && handlers.length > 0) {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _wooksjs_event_core from '@wooksjs/event-core';
2
- import { EventContextOptions, EventContext } from '@wooksjs/event-core';
2
+ import { EventContextOptions, EventKindSeeds, EventContext } from '@wooksjs/event-core';
3
3
  export { EventContext, EventContextOptions, useLogger, useRouteParams } from '@wooksjs/event-core';
4
4
  import * as wooks from 'wooks';
5
5
  import { Wooks, TWooksHandler, TWooksOptions, WooksAdapterBase } from 'wooks';
@@ -25,6 +25,20 @@ declare function useWfState(): {
25
25
  resume: boolean;
26
26
  };
27
27
 
28
+ declare const wfKind: _wooksjs_event_core.EventKind<{
29
+ schemaId: _wooksjs_event_core.SlotMarker<string>;
30
+ stepId: _wooksjs_event_core.SlotMarker<string | null>;
31
+ inputContext: _wooksjs_event_core.SlotMarker<unknown>;
32
+ indexes: _wooksjs_event_core.SlotMarker<number[] | undefined>;
33
+ input: _wooksjs_event_core.SlotMarker<unknown>;
34
+ }>;
35
+ declare const resumeKey: _wooksjs_event_core.Key<boolean>;
36
+
37
+ /** Creates a WF event context for a fresh workflow execution and runs `fn` inside it. */
38
+ declare function createWfContext<R>(options: EventContextOptions, seeds: EventKindSeeds<typeof wfKind>, fn: () => R): R;
39
+ /** Creates a WF event context for resuming a paused workflow and runs `fn` inside it. */
40
+ declare function resumeWfContext<R>(options: EventContextOptions, seeds: EventKindSeeds<typeof wfKind>, fn: () => R): R;
41
+
28
42
  /** Input data for creating a workflow event context. */
29
43
  interface TWFEventInput {
30
44
  schemaId: string;
@@ -34,20 +48,6 @@ interface TWFEventInput {
34
48
  input?: unknown;
35
49
  }
36
50
 
37
- /**
38
- * Creates a new event context for a fresh workflow execution.
39
- * When `parentCtx` is provided, the workflow creates a child context
40
- * linked to the parent, so step handlers can transparently access
41
- * composables from the parent scope (e.g. HTTP) via the parent chain.
42
- */
43
- declare function createWfContext(data: TWFEventInput, options: EventContextOptions, parentCtx?: EventContext): <R>(fn: () => R) => R;
44
- /**
45
- * Creates an event context for resuming a paused workflow.
46
- * When `parentCtx` is provided, the workflow creates a child context
47
- * linked to the parent.
48
- */
49
- declare function resumeWfContext(data: TWFEventInput, options: EventContextOptions, parentCtx?: EventContext): <R>(fn: () => R) => R;
50
-
51
51
  /** Workflow engine that resolves steps via Wooks router lookup. */
52
52
  declare class WooksWorkflow<T, IR> extends Workflow<T, IR> {
53
53
  protected wooks: Wooks;
@@ -91,6 +91,7 @@ declare class WooksWf<T = any, IR = any> extends WooksAdapterBase {
91
91
  protected opts?: TWooksWfOptions | undefined;
92
92
  protected logger: TConsoleBase;
93
93
  protected wf: WooksWorkflow<T, IR>;
94
+ protected eventContextOptions: EventContextOptions;
94
95
  constructor(opts?: TWooksWfOptions | undefined, wooks?: Wooks | WooksAdapterBase);
95
96
  /** Attaches a spy function to observe workflow step execution. */
96
97
  attachSpy<I>(fn: TWorkflowSpy<T, I, IR>): () => void;
@@ -152,14 +153,5 @@ declare class WooksWf<T = any, IR = any> extends WooksAdapterBase {
152
153
  */
153
154
  declare function createWfApp<T>(opts?: TWooksWfOptions, wooks?: Wooks | WooksAdapterBase): WooksWf<T, any>;
154
155
 
155
- declare const wfKind: _wooksjs_event_core.EventKind<{
156
- schemaId: _wooksjs_event_core.SlotMarker<string>;
157
- stepId: _wooksjs_event_core.SlotMarker<string | null>;
158
- inputContext: _wooksjs_event_core.SlotMarker<unknown>;
159
- indexes: _wooksjs_event_core.SlotMarker<number[] | undefined>;
160
- input: _wooksjs_event_core.SlotMarker<unknown>;
161
- }>;
162
- declare const resumeKey: _wooksjs_event_core.Key<boolean>;
163
-
164
156
  export { WooksWf, createWfApp, createWfContext, resumeKey, resumeWfContext, useWfState, wfKind, wfShortcuts };
165
157
  export type { TWFEventInput, TWfRunOptions, TWooksWfOptions };
package/dist/index.mjs CHANGED
@@ -37,40 +37,16 @@ function useWfState() {
37
37
 
38
38
  //#endregion
39
39
  //#region packages/event-wf/src/event-wf.ts
40
- const wfSeeds = (data) => ({
41
- schemaId: data.schemaId,
42
- stepId: data.stepId,
43
- inputContext: data.inputContext,
44
- indexes: data.indexes,
45
- input: data.input
46
- });
47
- /**
48
- * Creates a new event context for a fresh workflow execution.
49
- * When `parentCtx` is provided, the workflow creates a child context
50
- * linked to the parent, so step handlers can transparently access
51
- * composables from the parent scope (e.g. HTTP) via the parent chain.
52
- */
53
- function createWfContext(data, options, parentCtx) {
54
- const ctxOptions = parentCtx ? {
55
- ...options,
56
- parent: parentCtx
57
- } : options;
58
- return (fn) => createEventContext(ctxOptions, wfKind, wfSeeds(data), () => {
40
+ /** Creates a WF event context for a fresh workflow execution and runs `fn` inside it. */
41
+ function createWfContext(options, seeds, fn) {
42
+ return createEventContext(options, wfKind, seeds, () => {
59
43
  current().set(resumeKey, false);
60
44
  return fn();
61
45
  });
62
46
  }
63
- /**
64
- * Creates an event context for resuming a paused workflow.
65
- * When `parentCtx` is provided, the workflow creates a child context
66
- * linked to the parent.
67
- */
68
- function resumeWfContext(data, options, parentCtx) {
69
- const ctxOptions = parentCtx ? {
70
- ...options,
71
- parent: parentCtx
72
- } : options;
73
- return (fn) => createEventContext(ctxOptions, wfKind, wfSeeds(data), () => {
47
+ /** Creates a WF event context for resuming a paused workflow and runs `fn` inside it. */
48
+ function resumeWfContext(options, seeds, fn) {
49
+ return createEventContext(options, wfKind, seeds, () => {
74
50
  current().set(resumeKey, true);
75
51
  return fn();
76
52
  });
@@ -113,11 +89,13 @@ const wfShortcuts = {
113
89
  var WooksWf = class extends WooksAdapterBase {
114
90
  logger;
115
91
  wf;
92
+ eventContextOptions;
116
93
  constructor(opts, wooks) {
117
94
  super(wooks, opts?.logger, opts?.router);
118
95
  this.opts = opts;
119
96
  this.logger = opts?.logger || this.getLogger(`[wooks-wf]`);
120
97
  this.wf = new WooksWorkflow(this.wooks);
98
+ this.eventContextOptions = this.getEventContextOptions();
121
99
  }
122
100
  /** Attaches a spy function to observe workflow step execution. */
123
101
  attachSpy(fn) {
@@ -175,14 +153,19 @@ var WooksWf = class extends WooksAdapterBase {
175
153
  async _start(schemaId, inputContext, indexes, opts, parentCtx) {
176
154
  const { input, spy, cleanup } = opts ?? {};
177
155
  const resume = !!indexes?.length;
178
- const runInContext = (resume ? resumeWfContext : createWfContext)({
156
+ const factory = resume ? resumeWfContext : createWfContext;
157
+ const ctxOptions = parentCtx ? {
158
+ ...this.eventContextOptions,
159
+ parent: parentCtx
160
+ } : this.eventContextOptions;
161
+ const seeds = {
179
162
  inputContext,
180
163
  schemaId,
181
164
  stepId: null,
182
165
  indexes,
183
166
  input
184
- }, this.getEventContextOptions(), parentCtx);
185
- return runInContext(async () => {
167
+ };
168
+ return factory(ctxOptions, seeds, async () => {
186
169
  const { handlers: foundHandlers } = this.wooks.lookup("WF_FLOW", `/${schemaId}`.replace(/^\/+/u, "/"));
187
170
  const handlers = foundHandlers || this.opts?.onNotFound && [this.opts.onNotFound] || null;
188
171
  if (handlers && handlers.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-wf",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "@wooksjs/event-wf",
5
5
  "keywords": [
6
6
  "app",
@@ -47,13 +47,13 @@
47
47
  "devDependencies": {
48
48
  "typescript": "^5.9.3",
49
49
  "vitest": "^3.2.4",
50
- "@wooksjs/event-core": "^0.7.2",
51
- "wooks": "^0.7.2"
50
+ "@wooksjs/event-core": "^0.7.3",
51
+ "wooks": "^0.7.3"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "@prostojs/logger": "^0.4.3",
55
- "@wooksjs/event-core": "^0.7.2",
56
- "wooks": "^0.7.2"
55
+ "@wooksjs/event-core": "^0.7.3",
56
+ "wooks": "^0.7.3"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "rolldown -c ../../rolldown.config.mjs",
@@ -184,33 +184,34 @@ const final = await app.resume(state, { input: { username: 'alice', password: 's
184
184
 
185
185
  ## How Workflow Context Works
186
186
 
187
- When `start()` or `resume()` is called, the adapter creates a workflow-specific event context:
187
+ When `start()` or `resume()` is called, the adapter creates a workflow-specific event context using `createWfContext` (or `resumeWfContext`). These are context factories that hardcode the `wfKind` and delegate to `createEventContext`:
188
188
 
189
189
  ```
190
- app.start(schemaId, inputContext)
191
- → createWfContext({ inputContext, schemaId, stepId, indexes, input }, options)
192
- AsyncLocalStorage.run(wfContextStore, handler)
193
- router matches flow ID → handler runs
194
- workflow engine executes steps sequentially
195
- each step can call useWfState(), useRouteParams(), etc.
196
- composables call current() from @wooksjs/event-core
197
- reads/writes the event context via key/cached accessors
190
+ app.start(schemaId, inputContext, opts)
191
+ → createWfContext(ctxOptions, seeds, async () => { ... })
192
+ createEventContext(ctxOptions, wfKind, seeds, fn)
193
+ AsyncLocalStorage.run(ctx, handler)
194
+ router matches flow ID → handler runs
195
+ workflow engine executes steps sequentially
196
+ each step can call useWfState(), useRouteParams(), etc.
197
+ composables call current() from @wooksjs/event-core
198
+ → reads/writes the event context via key/cached accessors
198
199
  ```
199
200
 
200
- ### The WF Context Store
201
+ When `eventContext` is passed in opts, `ctxOptions` includes `parent: eventContext`, linking the WF context to the parent (e.g. HTTP) via the parent chain.
201
202
 
202
- ```ts
203
- interface TWFContextStore {
204
- resume: boolean // true if this is a resumed execution
205
- }
203
+ ### The WF Event Kind
206
204
 
207
- interface TWFEventData {
205
+ The WF adapter defines its event kind with `defineEventKind`. Seeds are passed directly to the context factory:
206
+
207
+ ```ts
208
+ // Seeds for createWfContext / resumeWfContext
209
+ interface WfSeeds {
208
210
  schemaId: string // flow ID being executed
209
211
  stepId: string | null // current step ID (set during step execution)
210
212
  inputContext: unknown // the workflow context object (T)
211
213
  indexes?: number[] // position for resume
212
214
  input?: unknown // input for current step
213
- type: 'WF'
214
215
  }
215
216
  ```
216
217
 
@@ -383,38 +383,48 @@ logger.error('something failed')
383
383
 
384
384
  ## Creating an Event Context (for adapter authors)
385
385
 
386
- Build an adapter that creates contexts and runs handlers:
386
+ Each adapter exports a **context factory** that hardcodes the event kind and delegates to `createEventContext`. The factory signature mirrors `createEventContext` but omits the `kind` parameter:
387
387
 
388
388
  ```ts
389
- import { EventContext, defineEventKind, slot, run } from '@wooksjs/event-core'
389
+ import { createEventContext, defineEventKind, slot } from '@wooksjs/event-core'
390
+ import type { EventContextOptions, EventKindSeeds } from '@wooksjs/event-core'
390
391
 
391
392
  const myKind = defineEventKind('my-event', {
392
393
  data: slot<unknown>(),
393
394
  source: slot<string>(),
394
395
  })
395
396
 
397
+ // Context factory — kind is hardcoded inside
398
+ export function createMyEventContext<R>(
399
+ options: EventContextOptions,
400
+ seeds: EventKindSeeds<typeof myKind>,
401
+ fn: () => R,
402
+ ): R {
403
+ return createEventContext(options, myKind, seeds, fn)
404
+ }
405
+
406
+ // Usage in the adapter
396
407
  function handleEvent(data: unknown, source: string, handler: () => unknown) {
397
- const ctx = new EventContext({ logger: console })
398
- return run(ctx, () => ctx.seed(myKind, { data, source }, handler))
408
+ return createMyEventContext({ logger: console }, { data, source }, handler)
399
409
  }
400
410
  ```
401
411
 
412
+ All built-in adapters follow this pattern: `createHttpContext`, `createCliContext`, `createWsConnectionContext`, `createWsMessageContext`, `createWfContext`, `resumeWfContext`.
413
+
402
414
  ### Parent context (nested events)
403
415
 
404
- Create child contexts with parent links. Each child sees its own data plus everything in the parent chain:
416
+ Create child contexts by passing `parent` in the options. Each child sees its own data plus everything in the parent chain:
405
417
 
406
418
  ```ts
407
419
  createEventContext({ logger }, httpKind, httpSeeds, () => {
408
420
  const parentCtx = current()
409
421
 
410
- const childCtx = new EventContext({ logger, parent: parentCtx })
411
- run(childCtx, () =>
412
- childCtx.seed(workflowKind, wfSeeds, () => {
413
- // Both HTTP and workflow composables work
414
- const { method } = useRequest() // found via parent chain
415
- const { ctx } = useWfState() // found locally
416
- }),
417
- )
422
+ // Child context linked to the HTTP parent
423
+ createEventContext({ logger, parent: parentCtx }, workflowKind, wfSeeds, () => {
424
+ // Both HTTP and workflow composables work
425
+ const { method } = useRequest() // found via parent chain
426
+ const { ctx } = useWfState() // found locally
427
+ })
418
428
  })
419
429
  ```
420
430