@wooksjs/event-wf 0.4.10 → 0.4.12

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
@@ -6,77 +6,45 @@ var wf = require('@prostojs/wf');
6
6
 
7
7
  function createWfContext(data, options) {
8
8
  return eventCore.createEventContext({
9
- event: Object.assign(Object.assign({}, data), { type: 'WF' }),
9
+ event: {
10
+ ...data,
11
+ type: 'WF',
12
+ },
10
13
  resume: false,
11
14
  options,
12
15
  });
13
16
  }
14
17
  function resumeWfContext(data, options) {
15
18
  return eventCore.createEventContext({
16
- event: Object.assign(Object.assign({}, data), { type: 'WF' }),
19
+ event: {
20
+ ...data,
21
+ type: 'WF',
22
+ },
17
23
  resume: true,
18
24
  options,
19
25
  });
20
26
  }
21
- /**
22
- * Wrapper on top of useEventContext that provides
23
- * proper context types for WF event
24
- * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
25
- */
26
27
  function useWFContext() {
27
28
  return eventCore.useEventContext('WF');
28
29
  }
29
30
 
30
- /******************************************************************************
31
- Copyright (c) Microsoft Corporation.
32
-
33
- Permission to use, copy, modify, and/or distribute this software for any
34
- purpose with or without fee is hereby granted.
35
-
36
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
37
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
38
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
39
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
40
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
41
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
42
- PERFORMANCE OF THIS SOFTWARE.
43
- ***************************************************************************** */
44
- /* global Reflect, Promise, SuppressedError, Symbol */
45
-
46
-
47
- function __awaiter(thisArg, _arguments, P, generator) {
48
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
49
- return new (P || (P = Promise))(function (resolve, reject) {
50
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
51
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
52
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
53
- step((generator = generator.apply(thisArg, _arguments || [])).next());
54
- });
55
- }
56
-
57
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
58
- var e = new Error(message);
59
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
60
- };
61
-
62
31
  class WooksWorkflow extends wf.Workflow {
63
32
  constructor(wooks) {
64
33
  super([]);
65
34
  this.wooks = wooks;
66
35
  }
67
36
  resolveStep(stepId) {
68
- var _a, _b, _c;
69
37
  try {
70
38
  useWFContext();
71
39
  const found = this.wooks.lookup('WF_STEP', '/' + stepId);
72
- if ((_a = found === null || found === void 0 ? void 0 : found.handlers) === null || _a === void 0 ? void 0 : _a.length) {
40
+ if (found?.handlers?.length) {
73
41
  return found.handlers[0]();
74
42
  }
75
43
  }
76
44
  catch (e) {
77
45
  const router = this.wooks.getRouter();
78
46
  const found = router.lookup('WF_STEP', '/' + stepId);
79
- if ((_c = (_b = found === null || found === void 0 ? void 0 : found.route) === null || _b === void 0 ? void 0 : _b.handlers) === null || _c === void 0 ? void 0 : _c.length) {
47
+ if (found?.route?.handlers?.length) {
80
48
  return found.route.handlers[0]();
81
49
  }
82
50
  }
@@ -90,9 +58,9 @@ const wfShortcuts = {
90
58
  };
91
59
  class WooksWf extends wooks.WooksAdapterBase {
92
60
  constructor(opts, wooks) {
93
- super(wooks, opts === null || opts === void 0 ? void 0 : opts.logger, opts === null || opts === void 0 ? void 0 : opts.router);
61
+ super(wooks, opts?.logger, opts?.router);
94
62
  this.opts = opts;
95
- this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-wf');
63
+ this.logger = opts?.logger || this.getLogger('wooks-wf');
96
64
  this.wf = new WooksWorkflow(this.wooks);
97
65
  }
98
66
  attachSpy(fn) {
@@ -115,76 +83,71 @@ class WooksWf extends wooks.WooksAdapterBase {
115
83
  resume(schemaId, state, input, spy, cleanup) {
116
84
  return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
117
85
  }
118
- _start(schemaId, inputContext, indexes, input, spy, cleanup) {
119
- var _a, _b;
120
- return __awaiter(this, void 0, void 0, function* () {
121
- const resume = !!(indexes === null || indexes === void 0 ? void 0 : indexes.length);
122
- const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
123
- inputContext,
124
- schemaId,
125
- indexes,
126
- input,
127
- }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
128
- const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
129
- const handlers = foundHandlers ||
130
- (((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound]) ||
131
- null;
132
- if (handlers && handlers.length) {
133
- let result = {};
134
- let firstStep = true;
135
- const _spy = (...args) => {
136
- if (spy) {
137
- spy(...args);
138
- }
139
- if (firstStep && args[0] === 'step') {
140
- // cleanup input after the first step
141
- firstStep = false;
86
+ async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
87
+ const resume = !!indexes?.length;
88
+ const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
89
+ inputContext,
90
+ schemaId,
91
+ indexes,
92
+ input,
93
+ }, this.mergeEventOptions(this.opts?.eventOptions));
94
+ const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
95
+ const handlers = foundHandlers ||
96
+ (this.opts?.onNotFound && [this.opts.onNotFound]) ||
97
+ null;
98
+ if (handlers && handlers.length) {
99
+ let result = {};
100
+ let firstStep = true;
101
+ const _spy = (...args) => {
102
+ if (spy) {
103
+ spy(...args);
104
+ }
105
+ if (firstStep && args[0] === 'step') {
106
+ firstStep = false;
107
+ restoreCtx();
108
+ const { store } = useWFContext();
109
+ store('event').set('input', undefined);
110
+ }
111
+ };
112
+ try {
113
+ for (const handler of handlers) {
114
+ restoreCtx();
115
+ const { id, init } = (await handler());
116
+ if (init) {
142
117
  restoreCtx();
143
- const { store } = useWFContext();
144
- store('event').set('input', undefined);
118
+ await init();
145
119
  }
146
- };
147
- try {
148
- for (const handler of handlers) {
149
- restoreCtx();
150
- const { id, init } = (yield handler());
151
- if (init) {
152
- restoreCtx();
153
- yield init();
154
- }
155
- restoreCtx();
156
- if (resume) {
157
- result = yield this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
158
- break;
159
- }
160
- else {
161
- result = yield this.wf.start(id, inputContext, input, _spy);
162
- break;
163
- }
120
+ restoreCtx();
121
+ if (resume) {
122
+ result = await this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
123
+ break;
124
+ }
125
+ else {
126
+ result = await this.wf.start(id, inputContext, input, _spy);
127
+ break;
164
128
  }
165
129
  }
166
- catch (e) {
167
- clean();
168
- throw e;
169
- }
130
+ }
131
+ catch (e) {
170
132
  clean();
171
- clearCtx();
172
- return result;
133
+ throw e;
173
134
  }
174
135
  clean();
175
- function clean() {
176
- if (cleanup) {
177
- restoreCtx();
178
- cleanup();
179
- }
180
- }
181
136
  clearCtx();
182
- throw new Error('Unknown schemaId: ' + schemaId);
183
- });
137
+ return result;
138
+ }
139
+ clean();
140
+ function clean() {
141
+ if (cleanup) {
142
+ restoreCtx();
143
+ cleanup();
144
+ }
145
+ }
146
+ clearCtx();
147
+ throw new Error('Unknown schemaId: ' + schemaId);
184
148
  }
185
149
  onError(e) {
186
- var _a;
187
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
150
+ if (this.opts?.onError) {
188
151
  this.opts.onError(e);
189
152
  }
190
153
  else {
@@ -201,12 +164,6 @@ class WooksWf extends wooks.WooksAdapterBase {
201
164
  }
202
165
  }
203
166
  }
204
- /**
205
- * Factory for WooksWf App
206
- * @param opts TWooksWfOptions
207
- * @param wooks Wooks | WooksAdapterBase
208
- * @returns WooksWf
209
- */
210
167
  function createWfApp(opts, wooks) {
211
168
  return new WooksWf(opts, wooks);
212
169
  }
package/dist/index.d.ts CHANGED
@@ -1,162 +1,144 @@
1
- import { Step } from '@prostojs/wf';
2
- import { TConsoleBase } from '@prostojs/logger';
3
- import { TEmpty } from '@wooksjs/event-core';
4
- import { TEventOptions } from '@wooksjs/event-core';
5
- import { TFlowOutput } from '@prostojs/wf';
6
- import { TGenericContextStore } from '@wooksjs/event-core';
7
- import { TProstoRouterPathHandle } from '@prostojs/router';
8
- import { TStepHandler } from '@prostojs/wf';
9
- import { TWooksHandler } from 'wooks';
10
- import { TWooksOptions } from 'wooks';
11
- import { TWorkflowSchema } from '@prostojs/wf';
12
- import { TWorkflowSpy } from '@prostojs/wf';
13
- import { Wooks } from 'wooks';
14
- import { WooksAdapterBase } from 'wooks';
15
- import { Workflow } from '@prostojs/wf';
16
-
17
- /**
18
- * Factory for WooksWf App
19
- * @param opts TWooksWfOptions
20
- * @param wooks Wooks | WooksAdapterBase
21
- * @returns WooksWf
22
- */
23
- export declare function createWfApp<T>(opts?: TWooksWfOptions, wooks?: Wooks | WooksAdapterBase): WooksWf<T>;
24
-
25
- export declare function createWfContext(data: Omit<TWFEventData, 'type'>, options: TEventOptions): {
26
- getCtx: () => TWFContextStore & TGenericContextStore<TWFEventData>;
27
- restoreCtx: () => TGenericContextStore<TEmpty>;
28
- clearCtx: () => null;
29
- store: <K extends "resume" | keyof TGenericContextStore<TWFEventData>>(key: K) => {
30
- value: (TWFContextStore & TGenericContextStore<TWFEventData>)[K];
31
- hook: <K2 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
32
- value: Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K][K2];
33
- isDefined: boolean;
34
- };
35
- init: <K2_1 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>[K2_1]) => Required<Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>[K2_1];
36
- set: <K2_2 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_2, v: Required<(TWFContextStore & TGenericContextStore<TWFEventData>)[K]>[K2_2]) => Required<(TWFContextStore & TGenericContextStore<TWFEventData>)[K]>[K2_2];
37
- get: <K2_3 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_3) => Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K][K2_3];
38
- has: <K2_4 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
39
- del: <K2_5 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
40
- entries: () => [string, unknown][];
41
- clear: () => void;
42
- };
43
- getStore: <K_1 extends "resume" | keyof TGenericContextStore<TWFEventData>>(key: K_1) => (TWFContextStore & TGenericContextStore<TWFEventData>)[K_1];
44
- setStore: <K_2 extends "resume" | keyof TGenericContextStore<TWFEventData>>(key: K_2, v: (TWFContextStore & TGenericContextStore<TWFEventData>)[K_2]) => void;
45
- };
46
-
47
- export declare function resumeWfContext(data: Omit<TWFEventData, 'type'>, options: TEventOptions): {
48
- getCtx: () => TWFContextStore & TGenericContextStore<TWFEventData>;
49
- restoreCtx: () => TGenericContextStore<TEmpty>;
50
- clearCtx: () => null;
51
- store: <K extends "resume" | keyof TGenericContextStore<TWFEventData>>(key: K) => {
52
- value: (TWFContextStore & TGenericContextStore<TWFEventData>)[K];
53
- hook: <K2 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
54
- value: Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K][K2];
55
- isDefined: boolean;
56
- };
57
- init: <K2_1 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>[K2_1]) => Required<Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>[K2_1];
58
- set: <K2_2 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_2, v: Required<(TWFContextStore & TGenericContextStore<TWFEventData>)[K]>[K2_2]) => Required<(TWFContextStore & TGenericContextStore<TWFEventData>)[K]>[K2_2];
59
- get: <K2_3 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_3) => Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K][K2_3];
60
- has: <K2_4 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
61
- del: <K2_5 extends keyof Required<TWFContextStore & TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
62
- entries: () => [string, unknown][];
63
- clear: () => void;
64
- };
65
- getStore: <K_1 extends "resume" | keyof TGenericContextStore<TWFEventData>>(key: K_1) => (TWFContextStore & TGenericContextStore<TWFEventData>)[K_1];
66
- setStore: <K_2 extends "resume" | keyof TGenericContextStore<TWFEventData>>(key: K_2, v: (TWFContextStore & TGenericContextStore<TWFEventData>)[K_2]) => void;
67
- };
68
-
69
- export { TStepHandler }
70
-
71
- export declare interface TWFContextStore {
72
- resume: boolean;
73
- }
74
-
75
- export declare interface TWFEventData {
76
- schemaId: string;
77
- inputContext: unknown;
78
- indexes?: number[];
79
- input?: unknown;
80
- type: 'WF';
81
- }
82
-
83
- export declare interface TWooksWfOptions {
84
- onError?(e: Error): void;
85
- onNotFound?: TWooksHandler<unknown>;
86
- onUnknownFlow?: (schemaId: string, raiseError: () => void) => unknown;
87
- logger?: TConsoleBase;
88
- eventOptions?: TEventOptions;
89
- router?: TWooksOptions['router'];
90
- }
91
-
92
- export { TWorkflowSchema }
93
-
94
- /**
95
- * Wrapper on top of useEventContext that provides
96
- * proper context types for WF event
97
- * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
98
- */
99
- export declare function useWFContext<T extends TEmpty>(): {
100
- getCtx: () => TWFContextStore & T & TGenericContextStore<TWFEventData>;
101
- restoreCtx: () => TGenericContextStore<TEmpty>;
102
- clearCtx: () => null;
103
- store: <K extends "resume" | keyof TGenericContextStore<TWFEventData> | keyof T>(key: K) => {
104
- value: (TWFContextStore & T & TGenericContextStore<TWFEventData>)[K];
105
- hook: <K2 extends keyof Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
106
- value: Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K][K2];
107
- isDefined: boolean;
108
- };
109
- init: <K2_1 extends keyof Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K]>[K2_1]) => Required<Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K]>[K2_1];
110
- set: <K2_2 extends keyof Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K]>(key2: K2_2, v: Required<(TWFContextStore & T & TGenericContextStore<TWFEventData>)[K]>[K2_2]) => Required<(TWFContextStore & T & TGenericContextStore<TWFEventData>)[K]>[K2_2];
111
- get: <K2_3 extends keyof Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K]>(key2: K2_3) => Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K][K2_3];
112
- has: <K2_4 extends keyof Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
113
- del: <K2_5 extends keyof Required<TWFContextStore & T & TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
114
- entries: () => [string, unknown][];
115
- clear: () => void;
116
- };
117
- getStore: <K_1 extends "resume" | keyof TGenericContextStore<TWFEventData> | keyof T>(key: K_1) => (TWFContextStore & T & TGenericContextStore<TWFEventData>)[K_1];
118
- setStore: <K_2 extends "resume" | keyof TGenericContextStore<TWFEventData> | keyof T>(key: K_2, v: (TWFContextStore & T & TGenericContextStore<TWFEventData>)[K_2]) => void;
119
- };
120
-
121
- export declare function useWfState(): {
122
- ctx: <T>() => T;
123
- input: <I>() => I | undefined;
124
- schemaId: string;
125
- indexes: () => number[] | undefined;
126
- resume: boolean;
127
- };
128
-
129
- export declare const wfShortcuts: {
130
- flow: string;
131
- step: string;
132
- };
133
-
134
- export declare class WooksWf<T> extends WooksAdapterBase {
135
- protected opts?: TWooksWfOptions | undefined;
136
- protected logger: TConsoleBase;
137
- protected wf: WooksWorkflow<T>;
138
- constructor(opts?: TWooksWfOptions | undefined, wooks?: Wooks | WooksAdapterBase);
139
- attachSpy<I>(fn: TWorkflowSpy<T, I>): () => void;
140
- detachSpy<I>(fn: TWorkflowSpy<T, I>): void;
141
- step<I = any, D = any>(id: string, opts: {
142
- input?: D;
143
- handler: string | TStepHandler<T, I, D>;
144
- }): TProstoRouterPathHandle<Record<string, string | string[]>>;
145
- flow(id: string, schema: TWorkflowSchema<T>, init?: () => void | Promise<void>): TProstoRouterPathHandle<Record<string, string | string[]>>;
146
- start<I>(schemaId: string, inputContext: T, input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
147
- resume<I>(schemaId: string, state: {
148
- indexes: number[];
149
- context: T;
150
- }, input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
151
- protected _start<I>(schemaId: string, inputContext: T, indexes?: number[], input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
152
- protected onError(e: Error): void;
153
- protected error(e: string | Error): void;
154
- }
155
-
156
- declare class WooksWorkflow<T> extends Workflow<T> {
157
- protected wooks: Wooks;
158
- constructor(wooks: Wooks);
159
- protected resolveStep<I, D>(stepId: string): Step<T, I, D>;
160
- }
161
-
162
- export { }
1
+ import * as _wooksjs_event_core from '@wooksjs/event-core';
2
+ import { TEventOptions, TEmpty } from '@wooksjs/event-core';
3
+ import * as _prostojs_router from '@prostojs/router';
4
+ import { Wooks, TWooksHandler, TWooksOptions, WooksAdapterBase } from 'wooks';
5
+ import { TConsoleBase } from '@prostojs/logger';
6
+ import { Workflow, Step, TWorkflowSpy, TStepHandler, TWorkflowSchema, TFlowOutput } from '@prostojs/wf';
7
+ export { TStepHandler, TWorkflowSchema } from '@prostojs/wf';
8
+
9
+ interface TWFEventData {
10
+ schemaId: string;
11
+ inputContext: unknown;
12
+ indexes?: number[];
13
+ input?: unknown;
14
+ type: 'WF';
15
+ }
16
+ interface TWFContextStore {
17
+ resume: boolean;
18
+ }
19
+
20
+ declare function createWfContext(data: Omit<TWFEventData, 'type'>, options: TEventOptions): {
21
+ getCtx: () => TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>;
22
+ restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
23
+ clearCtx: () => null;
24
+ store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K) => {
25
+ value: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
26
+ hook: <K2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
27
+ value: Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
28
+ isDefined: boolean;
29
+ };
30
+ init: <K2_1 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1]) => Required<Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1];
31
+ set: <K2_2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_2, v: Required<(TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2]) => Required<(TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2];
32
+ get: <K2_3 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_3) => Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2_3];
33
+ has: <K2_4 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
34
+ del: <K2_5 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
35
+ entries: () => [string, unknown][];
36
+ clear: () => void;
37
+ };
38
+ getStore: <K_1 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K_1) => (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_1];
39
+ setStore: <K_2 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K_2, v: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_2]) => void;
40
+ };
41
+ declare function resumeWfContext(data: Omit<TWFEventData, 'type'>, options: TEventOptions): {
42
+ getCtx: () => TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>;
43
+ restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
44
+ clearCtx: () => null;
45
+ store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K) => {
46
+ value: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
47
+ hook: <K2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
48
+ value: Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
49
+ isDefined: boolean;
50
+ };
51
+ init: <K2_1 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1]) => Required<Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1];
52
+ set: <K2_2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_2, v: Required<(TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2]) => Required<(TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2];
53
+ get: <K2_3 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_3) => Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2_3];
54
+ has: <K2_4 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
55
+ del: <K2_5 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
56
+ entries: () => [string, unknown][];
57
+ clear: () => void;
58
+ };
59
+ getStore: <K_1 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K_1) => (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_1];
60
+ setStore: <K_2 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K_2, v: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_2]) => void;
61
+ };
62
+ /**
63
+ * Wrapper on top of useEventContext that provides
64
+ * proper context types for WF event
65
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
66
+ */
67
+ declare function useWFContext<T extends TEmpty>(): {
68
+ getCtx: () => TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>;
69
+ restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
70
+ clearCtx: () => null;
71
+ store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData> | keyof T>(key: K) => {
72
+ value: (TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
73
+ hook: <K2 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
74
+ value: Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
75
+ isDefined: boolean;
76
+ };
77
+ init: <K2_1 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1]) => Required<Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1];
78
+ set: <K2_2 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_2, v: Required<(TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2]) => Required<(TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2];
79
+ get: <K2_3 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_3) => Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2_3];
80
+ has: <K2_4 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
81
+ del: <K2_5 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
82
+ entries: () => [string, unknown][];
83
+ clear: () => void;
84
+ };
85
+ getStore: <K_1 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData> | keyof T>(key: K_1) => (TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_1];
86
+ setStore: <K_2 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData> | keyof T>(key: K_2, v: (TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_2]) => void;
87
+ };
88
+
89
+ declare class WooksWorkflow<T> extends Workflow<T> {
90
+ protected wooks: Wooks;
91
+ constructor(wooks: Wooks);
92
+ protected resolveStep<I, D>(stepId: string): Step<T, I, D>;
93
+ }
94
+
95
+ declare const wfShortcuts: {
96
+ flow: string;
97
+ step: string;
98
+ };
99
+ interface TWooksWfOptions {
100
+ onError?(e: Error): void;
101
+ onNotFound?: TWooksHandler<unknown>;
102
+ onUnknownFlow?: (schemaId: string, raiseError: () => void) => unknown;
103
+ logger?: TConsoleBase;
104
+ eventOptions?: TEventOptions;
105
+ router?: TWooksOptions['router'];
106
+ }
107
+ declare class WooksWf<T> extends WooksAdapterBase {
108
+ protected opts?: TWooksWfOptions | undefined;
109
+ protected logger: TConsoleBase;
110
+ protected wf: WooksWorkflow<T>;
111
+ constructor(opts?: TWooksWfOptions | undefined, wooks?: Wooks | WooksAdapterBase);
112
+ attachSpy<I>(fn: TWorkflowSpy<T, I>): () => void;
113
+ detachSpy<I>(fn: TWorkflowSpy<T, I>): void;
114
+ step<I = any, D = any>(id: string, opts: {
115
+ input?: D;
116
+ handler: string | TStepHandler<T, I, D>;
117
+ }): _prostojs_router.TProstoRouterPathHandle<Record<string, string | string[]>>;
118
+ flow(id: string, schema: TWorkflowSchema<T>, init?: () => void | Promise<void>): _prostojs_router.TProstoRouterPathHandle<Record<string, string | string[]>>;
119
+ start<I>(schemaId: string, inputContext: T, input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
120
+ resume<I>(schemaId: string, state: {
121
+ indexes: number[];
122
+ context: T;
123
+ }, input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
124
+ protected _start<I>(schemaId: string, inputContext: T, indexes?: number[], input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
125
+ protected onError(e: Error): void;
126
+ protected error(e: string | Error): void;
127
+ }
128
+ /**
129
+ * Factory for WooksWf App
130
+ * @param opts TWooksWfOptions
131
+ * @param wooks Wooks | WooksAdapterBase
132
+ * @returns WooksWf
133
+ */
134
+ declare function createWfApp<T>(opts?: TWooksWfOptions, wooks?: Wooks | WooksAdapterBase): WooksWf<T>;
135
+
136
+ declare function useWfState(): {
137
+ ctx: <T>() => T;
138
+ input: <I>() => I | undefined;
139
+ schemaId: string;
140
+ indexes: () => number[] | undefined;
141
+ resume: boolean;
142
+ };
143
+
144
+ export { type TWFContextStore, type TWFEventData, type TWooksWfOptions, WooksWf, createWfApp, createWfContext, resumeWfContext, useWFContext, useWfState, wfShortcuts };
package/dist/index.mjs CHANGED
@@ -4,77 +4,45 @@ import { Workflow, createStep } from '@prostojs/wf';
4
4
 
5
5
  function createWfContext(data, options) {
6
6
  return createEventContext({
7
- event: Object.assign(Object.assign({}, data), { type: 'WF' }),
7
+ event: {
8
+ ...data,
9
+ type: 'WF',
10
+ },
8
11
  resume: false,
9
12
  options,
10
13
  });
11
14
  }
12
15
  function resumeWfContext(data, options) {
13
16
  return createEventContext({
14
- event: Object.assign(Object.assign({}, data), { type: 'WF' }),
17
+ event: {
18
+ ...data,
19
+ type: 'WF',
20
+ },
15
21
  resume: true,
16
22
  options,
17
23
  });
18
24
  }
19
- /**
20
- * Wrapper on top of useEventContext that provides
21
- * proper context types for WF event
22
- * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
23
- */
24
25
  function useWFContext() {
25
26
  return useEventContext('WF');
26
27
  }
27
28
 
28
- /******************************************************************************
29
- Copyright (c) Microsoft Corporation.
30
-
31
- Permission to use, copy, modify, and/or distribute this software for any
32
- purpose with or without fee is hereby granted.
33
-
34
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
35
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
37
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
38
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
39
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
40
- PERFORMANCE OF THIS SOFTWARE.
41
- ***************************************************************************** */
42
- /* global Reflect, Promise, SuppressedError, Symbol */
43
-
44
-
45
- function __awaiter(thisArg, _arguments, P, generator) {
46
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
47
- return new (P || (P = Promise))(function (resolve, reject) {
48
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
49
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
50
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
51
- step((generator = generator.apply(thisArg, _arguments || [])).next());
52
- });
53
- }
54
-
55
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
56
- var e = new Error(message);
57
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
58
- };
59
-
60
29
  class WooksWorkflow extends Workflow {
61
30
  constructor(wooks) {
62
31
  super([]);
63
32
  this.wooks = wooks;
64
33
  }
65
34
  resolveStep(stepId) {
66
- var _a, _b, _c;
67
35
  try {
68
36
  useWFContext();
69
37
  const found = this.wooks.lookup('WF_STEP', '/' + stepId);
70
- if ((_a = found === null || found === void 0 ? void 0 : found.handlers) === null || _a === void 0 ? void 0 : _a.length) {
38
+ if (found?.handlers?.length) {
71
39
  return found.handlers[0]();
72
40
  }
73
41
  }
74
42
  catch (e) {
75
43
  const router = this.wooks.getRouter();
76
44
  const found = router.lookup('WF_STEP', '/' + stepId);
77
- if ((_c = (_b = found === null || found === void 0 ? void 0 : found.route) === null || _b === void 0 ? void 0 : _b.handlers) === null || _c === void 0 ? void 0 : _c.length) {
45
+ if (found?.route?.handlers?.length) {
78
46
  return found.route.handlers[0]();
79
47
  }
80
48
  }
@@ -88,9 +56,9 @@ const wfShortcuts = {
88
56
  };
89
57
  class WooksWf extends WooksAdapterBase {
90
58
  constructor(opts, wooks) {
91
- super(wooks, opts === null || opts === void 0 ? void 0 : opts.logger, opts === null || opts === void 0 ? void 0 : opts.router);
59
+ super(wooks, opts?.logger, opts?.router);
92
60
  this.opts = opts;
93
- this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-wf');
61
+ this.logger = opts?.logger || this.getLogger('wooks-wf');
94
62
  this.wf = new WooksWorkflow(this.wooks);
95
63
  }
96
64
  attachSpy(fn) {
@@ -113,76 +81,71 @@ class WooksWf extends WooksAdapterBase {
113
81
  resume(schemaId, state, input, spy, cleanup) {
114
82
  return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
115
83
  }
116
- _start(schemaId, inputContext, indexes, input, spy, cleanup) {
117
- var _a, _b;
118
- return __awaiter(this, void 0, void 0, function* () {
119
- const resume = !!(indexes === null || indexes === void 0 ? void 0 : indexes.length);
120
- const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
121
- inputContext,
122
- schemaId,
123
- indexes,
124
- input,
125
- }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
126
- const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
127
- const handlers = foundHandlers ||
128
- (((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound]) ||
129
- null;
130
- if (handlers && handlers.length) {
131
- let result = {};
132
- let firstStep = true;
133
- const _spy = (...args) => {
134
- if (spy) {
135
- spy(...args);
136
- }
137
- if (firstStep && args[0] === 'step') {
138
- // cleanup input after the first step
139
- firstStep = false;
84
+ async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
85
+ const resume = !!indexes?.length;
86
+ const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
87
+ inputContext,
88
+ schemaId,
89
+ indexes,
90
+ input,
91
+ }, this.mergeEventOptions(this.opts?.eventOptions));
92
+ const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
93
+ const handlers = foundHandlers ||
94
+ (this.opts?.onNotFound && [this.opts.onNotFound]) ||
95
+ null;
96
+ if (handlers && handlers.length) {
97
+ let result = {};
98
+ let firstStep = true;
99
+ const _spy = (...args) => {
100
+ if (spy) {
101
+ spy(...args);
102
+ }
103
+ if (firstStep && args[0] === 'step') {
104
+ firstStep = false;
105
+ restoreCtx();
106
+ const { store } = useWFContext();
107
+ store('event').set('input', undefined);
108
+ }
109
+ };
110
+ try {
111
+ for (const handler of handlers) {
112
+ restoreCtx();
113
+ const { id, init } = (await handler());
114
+ if (init) {
140
115
  restoreCtx();
141
- const { store } = useWFContext();
142
- store('event').set('input', undefined);
116
+ await init();
143
117
  }
144
- };
145
- try {
146
- for (const handler of handlers) {
147
- restoreCtx();
148
- const { id, init } = (yield handler());
149
- if (init) {
150
- restoreCtx();
151
- yield init();
152
- }
153
- restoreCtx();
154
- if (resume) {
155
- result = yield this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
156
- break;
157
- }
158
- else {
159
- result = yield this.wf.start(id, inputContext, input, _spy);
160
- break;
161
- }
118
+ restoreCtx();
119
+ if (resume) {
120
+ result = await this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
121
+ break;
122
+ }
123
+ else {
124
+ result = await this.wf.start(id, inputContext, input, _spy);
125
+ break;
162
126
  }
163
127
  }
164
- catch (e) {
165
- clean();
166
- throw e;
167
- }
128
+ }
129
+ catch (e) {
168
130
  clean();
169
- clearCtx();
170
- return result;
131
+ throw e;
171
132
  }
172
133
  clean();
173
- function clean() {
174
- if (cleanup) {
175
- restoreCtx();
176
- cleanup();
177
- }
178
- }
179
134
  clearCtx();
180
- throw new Error('Unknown schemaId: ' + schemaId);
181
- });
135
+ return result;
136
+ }
137
+ clean();
138
+ function clean() {
139
+ if (cleanup) {
140
+ restoreCtx();
141
+ cleanup();
142
+ }
143
+ }
144
+ clearCtx();
145
+ throw new Error('Unknown schemaId: ' + schemaId);
182
146
  }
183
147
  onError(e) {
184
- var _a;
185
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
148
+ if (this.opts?.onError) {
186
149
  this.opts.onError(e);
187
150
  }
188
151
  else {
@@ -199,12 +162,6 @@ class WooksWf extends WooksAdapterBase {
199
162
  }
200
163
  }
201
164
  }
202
- /**
203
- * Factory for WooksWf App
204
- * @param opts TWooksWfOptions
205
- * @param wooks Wooks | WooksAdapterBase
206
- * @returns WooksWf
207
- */
208
165
  function createWfApp(opts, wooks) {
209
166
  return new WooksWf(opts, wooks);
210
167
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-wf",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "description": "@wooksjs/event-wf",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -8,6 +8,14 @@
8
8
  "files": [
9
9
  "dist"
10
10
  ],
11
+ "exports": {
12
+ "./package.json": "./package.json",
13
+ ".": {
14
+ "require": "./dist/index.cjs",
15
+ "import": "./dist/index.mjs",
16
+ "types": "./dist/index.d.ts"
17
+ }
18
+ },
11
19
  "repository": {
12
20
  "type": "git",
13
21
  "url": "git+https://github.com/wooksjs/wooksjs.git",
@@ -25,11 +33,11 @@
25
33
  "author": "Artem Maltsev",
26
34
  "license": "MIT",
27
35
  "peerDependencies": {
28
- "wooks": "0.4.10",
29
- "@wooksjs/event-core": "0.4.10"
36
+ "wooks": "0.4.12",
37
+ "@wooksjs/event-core": "0.4.12"
30
38
  },
31
39
  "dependencies": {
32
- "@prostojs/logger": "^0.3.7",
40
+ "@prostojs/logger": "^0.4.0",
33
41
  "@prostojs/wf": "^0.0.10"
34
42
  },
35
43
  "bugs": {