@wooksjs/event-wf 0.4.9 → 0.4.11

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.
Files changed (3) hide show
  1. package/dist/index.cjs +179 -203
  2. package/dist/index.mjs +179 -203
  3. package/package.json +3 -3
package/dist/index.cjs CHANGED
@@ -4,216 +4,192 @@ var eventCore = require('@wooksjs/event-core');
4
4
  var wooks = require('wooks');
5
5
  var wf = require('@prostojs/wf');
6
6
 
7
- function createWfContext(data, options) {
8
- return eventCore.createEventContext({
9
- event: Object.assign(Object.assign({}, data), { type: 'WF' }),
10
- resume: false,
11
- options,
12
- });
13
- }
14
- function resumeWfContext(data, options) {
15
- return eventCore.createEventContext({
16
- event: Object.assign(Object.assign({}, data), { type: 'WF' }),
17
- resume: true,
18
- options,
19
- });
20
- }
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
- function useWFContext() {
27
- return eventCore.useEventContext('WF');
7
+ function createWfContext(data, options) {
8
+ return eventCore.createEventContext({
9
+ event: {
10
+ ...data,
11
+ type: 'WF',
12
+ },
13
+ resume: false,
14
+ options,
15
+ });
28
16
  }
29
-
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
-
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
- });
17
+ function resumeWfContext(data, options) {
18
+ return eventCore.createEventContext({
19
+ event: {
20
+ ...data,
21
+ type: 'WF',
22
+ },
23
+ resume: true,
24
+ options,
25
+ });
26
+ }
27
+ /**
28
+ * Wrapper on top of useEventContext that provides
29
+ * proper context types for WF event
30
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
31
+ */
32
+ function useWFContext() {
33
+ return eventCore.useEventContext('WF');
53
34
  }
54
35
 
55
- class WooksWorkflow extends wf.Workflow {
56
- constructor(wooks) {
57
- super([]);
58
- this.wooks = wooks;
59
- }
60
- resolveStep(stepId) {
61
- var _a, _b, _c;
62
- try {
63
- useWFContext();
64
- const found = this.wooks.lookup('WF_STEP', '/' + stepId);
65
- if ((_a = found === null || found === void 0 ? void 0 : found.handlers) === null || _a === void 0 ? void 0 : _a.length) {
66
- return found.handlers[0]();
67
- }
68
- }
69
- catch (e) {
70
- const router = this.wooks.getRouter();
71
- const found = router.lookup('WF_STEP', '/' + stepId);
72
- 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) {
73
- return found.route.handlers[0]();
74
- }
75
- }
76
- throw new Error(`Step "${stepId}" not found.`);
77
- }
36
+ class WooksWorkflow extends wf.Workflow {
37
+ constructor(wooks) {
38
+ super([]);
39
+ this.wooks = wooks;
40
+ }
41
+ resolveStep(stepId) {
42
+ try {
43
+ useWFContext();
44
+ const found = this.wooks.lookup('WF_STEP', '/' + stepId);
45
+ if (found?.handlers?.length) {
46
+ return found.handlers[0]();
47
+ }
48
+ }
49
+ catch (e) {
50
+ const router = this.wooks.getRouter();
51
+ const found = router.lookup('WF_STEP', '/' + stepId);
52
+ if (found?.route?.handlers?.length) {
53
+ return found.route.handlers[0]();
54
+ }
55
+ }
56
+ throw new Error(`Step "${stepId}" not found.`);
57
+ }
78
58
  }
79
59
 
80
- const wfShortcuts = {
81
- flow: 'WF_FLOW',
82
- step: 'WF_STEP',
83
- };
84
- class WooksWf extends wooks.WooksAdapterBase {
85
- constructor(opts, wooks) {
86
- super(wooks, opts === null || opts === void 0 ? void 0 : opts.logger, opts === null || opts === void 0 ? void 0 : opts.router);
87
- this.opts = opts;
88
- this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-wf');
89
- this.wf = new WooksWorkflow(this.wooks);
90
- }
91
- attachSpy(fn) {
92
- return this.wf.attachSpy(fn);
93
- }
94
- detachSpy(fn) {
95
- return this.wf.detachSpy(fn);
96
- }
97
- step(id, opts) {
98
- const step = wf.createStep(id, opts);
99
- return this.on('WF_STEP', id, () => step);
100
- }
101
- flow(id, schema, init) {
102
- this.wf.register(id, schema);
103
- return this.on('WF_FLOW', id, () => ({ init, id }));
104
- }
105
- start(schemaId, inputContext, input, spy, cleanup) {
106
- return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
107
- }
108
- resume(schemaId, state, input, spy, cleanup) {
109
- return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
110
- }
111
- _start(schemaId, inputContext, indexes, input, spy, cleanup) {
112
- var _a, _b;
113
- return __awaiter(this, void 0, void 0, function* () {
114
- const resume = !!(indexes === null || indexes === void 0 ? void 0 : indexes.length);
115
- const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
116
- inputContext,
117
- schemaId,
118
- indexes,
119
- input,
120
- }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
121
- const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
122
- const handlers = foundHandlers ||
123
- (((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound]) ||
124
- null;
125
- if (handlers && handlers.length) {
126
- let result = {};
127
- let firstStep = true;
128
- const _spy = (...args) => {
129
- if (spy) {
130
- spy(...args);
131
- }
132
- if (firstStep && args[0] === 'step') {
133
- // cleanup input after the first step
134
- firstStep = false;
135
- restoreCtx();
136
- const { store } = useWFContext();
137
- store('event').set('input', undefined);
138
- }
139
- };
140
- try {
141
- for (const handler of handlers) {
142
- restoreCtx();
143
- const { id, init } = (yield handler());
144
- if (init) {
145
- restoreCtx();
146
- yield init();
147
- }
148
- restoreCtx();
149
- if (resume) {
150
- result = yield this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
151
- break;
152
- }
153
- else {
154
- result = yield this.wf.start(id, inputContext, input, _spy);
155
- break;
156
- }
157
- }
158
- }
159
- catch (e) {
160
- clean();
161
- throw e;
162
- }
163
- clean();
164
- clearCtx();
165
- return result;
166
- }
167
- clean();
168
- function clean() {
169
- if (cleanup) {
170
- restoreCtx();
171
- cleanup();
172
- }
173
- }
174
- clearCtx();
175
- throw new Error('Unknown schemaId: ' + schemaId);
176
- });
177
- }
178
- onError(e) {
179
- var _a;
180
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
181
- this.opts.onError(e);
182
- }
183
- else {
184
- this.error(e.message);
185
- process.exit(1);
186
- }
187
- }
188
- error(e) {
189
- if (typeof e === 'string') {
190
- console.error('' + 'ERROR: ' + '' + e);
191
- }
192
- else {
193
- console.error('' + 'ERROR: ' + '' + e.message);
194
- }
195
- }
196
- }
197
- /**
198
- * Factory for WooksWf App
199
- * @param opts TWooksWfOptions
200
- * @param wooks Wooks | WooksAdapterBase
201
- * @returns WooksWf
202
- */
203
- function createWfApp(opts, wooks) {
204
- return new WooksWf(opts, wooks);
60
+ const wfShortcuts = {
61
+ flow: 'WF_FLOW',
62
+ step: 'WF_STEP',
63
+ };
64
+ class WooksWf extends wooks.WooksAdapterBase {
65
+ constructor(opts, wooks) {
66
+ super(wooks, opts?.logger, opts?.router);
67
+ this.opts = opts;
68
+ this.logger = opts?.logger || this.getLogger('wooks-wf');
69
+ this.wf = new WooksWorkflow(this.wooks);
70
+ }
71
+ attachSpy(fn) {
72
+ return this.wf.attachSpy(fn);
73
+ }
74
+ detachSpy(fn) {
75
+ return this.wf.detachSpy(fn);
76
+ }
77
+ step(id, opts) {
78
+ const step = wf.createStep(id, opts);
79
+ return this.on('WF_STEP', id, () => step);
80
+ }
81
+ flow(id, schema, init) {
82
+ this.wf.register(id, schema);
83
+ return this.on('WF_FLOW', id, () => ({ init, id }));
84
+ }
85
+ start(schemaId, inputContext, input, spy, cleanup) {
86
+ return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
87
+ }
88
+ resume(schemaId, state, input, spy, cleanup) {
89
+ return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
90
+ }
91
+ async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
92
+ const resume = !!indexes?.length;
93
+ const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
94
+ inputContext,
95
+ schemaId,
96
+ indexes,
97
+ input,
98
+ }, this.mergeEventOptions(this.opts?.eventOptions));
99
+ const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
100
+ const handlers = foundHandlers ||
101
+ (this.opts?.onNotFound && [this.opts.onNotFound]) ||
102
+ null;
103
+ if (handlers && handlers.length) {
104
+ let result = {};
105
+ let firstStep = true;
106
+ const _spy = (...args) => {
107
+ if (spy) {
108
+ spy(...args);
109
+ }
110
+ if (firstStep && args[0] === 'step') {
111
+ // cleanup input after the first step
112
+ firstStep = false;
113
+ restoreCtx();
114
+ const { store } = useWFContext();
115
+ store('event').set('input', undefined);
116
+ }
117
+ };
118
+ try {
119
+ for (const handler of handlers) {
120
+ restoreCtx();
121
+ const { id, init } = (await handler());
122
+ if (init) {
123
+ restoreCtx();
124
+ await init();
125
+ }
126
+ restoreCtx();
127
+ if (resume) {
128
+ result = await this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
129
+ break;
130
+ }
131
+ else {
132
+ result = await this.wf.start(id, inputContext, input, _spy);
133
+ break;
134
+ }
135
+ }
136
+ }
137
+ catch (e) {
138
+ clean();
139
+ throw e;
140
+ }
141
+ clean();
142
+ clearCtx();
143
+ return result;
144
+ }
145
+ clean();
146
+ function clean() {
147
+ if (cleanup) {
148
+ restoreCtx();
149
+ cleanup();
150
+ }
151
+ }
152
+ clearCtx();
153
+ throw new Error('Unknown schemaId: ' + schemaId);
154
+ }
155
+ onError(e) {
156
+ if (this.opts?.onError) {
157
+ this.opts.onError(e);
158
+ }
159
+ else {
160
+ this.error(e.message);
161
+ process.exit(1);
162
+ }
163
+ }
164
+ error(e) {
165
+ if (typeof e === 'string') {
166
+ console.error('' + 'ERROR: ' + '' + e);
167
+ }
168
+ else {
169
+ console.error('' + 'ERROR: ' + '' + e.message);
170
+ }
171
+ }
172
+ }
173
+ /**
174
+ * Factory for WooksWf App
175
+ * @param opts TWooksWfOptions
176
+ * @param wooks Wooks | WooksAdapterBase
177
+ * @returns WooksWf
178
+ */
179
+ function createWfApp(opts, wooks) {
180
+ return new WooksWf(opts, wooks);
205
181
  }
206
182
 
207
- function useWfState() {
208
- const { store, getCtx } = useWFContext();
209
- const event = store('event');
210
- return {
211
- ctx: () => event.get('inputContext'),
212
- input: () => event.get('input'),
213
- schemaId: event.get('schemaId'),
214
- indexes: () => event.get('indexes'),
215
- resume: getCtx().resume,
216
- };
183
+ function useWfState() {
184
+ const { store, getCtx } = useWFContext();
185
+ const event = store('event');
186
+ return {
187
+ ctx: () => event.get('inputContext'),
188
+ input: () => event.get('input'),
189
+ schemaId: event.get('schemaId'),
190
+ indexes: () => event.get('indexes'),
191
+ resume: getCtx().resume,
192
+ };
217
193
  }
218
194
 
219
195
  exports.WooksWf = WooksWf;
package/dist/index.mjs CHANGED
@@ -2,216 +2,192 @@ import { createEventContext, useEventContext } from '@wooksjs/event-core';
2
2
  import { WooksAdapterBase } from 'wooks';
3
3
  import { Workflow, createStep } from '@prostojs/wf';
4
4
 
5
- function createWfContext(data, options) {
6
- return createEventContext({
7
- event: Object.assign(Object.assign({}, data), { type: 'WF' }),
8
- resume: false,
9
- options,
10
- });
11
- }
12
- function resumeWfContext(data, options) {
13
- return createEventContext({
14
- event: Object.assign(Object.assign({}, data), { type: 'WF' }),
15
- resume: true,
16
- options,
17
- });
18
- }
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
- function useWFContext() {
25
- return useEventContext('WF');
5
+ function createWfContext(data, options) {
6
+ return createEventContext({
7
+ event: {
8
+ ...data,
9
+ type: 'WF',
10
+ },
11
+ resume: false,
12
+ options,
13
+ });
26
14
  }
27
-
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
-
43
- function __awaiter(thisArg, _arguments, P, generator) {
44
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
45
- return new (P || (P = Promise))(function (resolve, reject) {
46
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
47
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
48
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
49
- step((generator = generator.apply(thisArg, _arguments || [])).next());
50
- });
15
+ function resumeWfContext(data, options) {
16
+ return createEventContext({
17
+ event: {
18
+ ...data,
19
+ type: 'WF',
20
+ },
21
+ resume: true,
22
+ options,
23
+ });
24
+ }
25
+ /**
26
+ * Wrapper on top of useEventContext that provides
27
+ * proper context types for WF event
28
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
29
+ */
30
+ function useWFContext() {
31
+ return useEventContext('WF');
51
32
  }
52
33
 
53
- class WooksWorkflow extends Workflow {
54
- constructor(wooks) {
55
- super([]);
56
- this.wooks = wooks;
57
- }
58
- resolveStep(stepId) {
59
- var _a, _b, _c;
60
- try {
61
- useWFContext();
62
- const found = this.wooks.lookup('WF_STEP', '/' + stepId);
63
- if ((_a = found === null || found === void 0 ? void 0 : found.handlers) === null || _a === void 0 ? void 0 : _a.length) {
64
- return found.handlers[0]();
65
- }
66
- }
67
- catch (e) {
68
- const router = this.wooks.getRouter();
69
- const found = router.lookup('WF_STEP', '/' + stepId);
70
- 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) {
71
- return found.route.handlers[0]();
72
- }
73
- }
74
- throw new Error(`Step "${stepId}" not found.`);
75
- }
34
+ class WooksWorkflow extends Workflow {
35
+ constructor(wooks) {
36
+ super([]);
37
+ this.wooks = wooks;
38
+ }
39
+ resolveStep(stepId) {
40
+ try {
41
+ useWFContext();
42
+ const found = this.wooks.lookup('WF_STEP', '/' + stepId);
43
+ if (found?.handlers?.length) {
44
+ return found.handlers[0]();
45
+ }
46
+ }
47
+ catch (e) {
48
+ const router = this.wooks.getRouter();
49
+ const found = router.lookup('WF_STEP', '/' + stepId);
50
+ if (found?.route?.handlers?.length) {
51
+ return found.route.handlers[0]();
52
+ }
53
+ }
54
+ throw new Error(`Step "${stepId}" not found.`);
55
+ }
76
56
  }
77
57
 
78
- const wfShortcuts = {
79
- flow: 'WF_FLOW',
80
- step: 'WF_STEP',
81
- };
82
- class WooksWf extends WooksAdapterBase {
83
- constructor(opts, wooks) {
84
- super(wooks, opts === null || opts === void 0 ? void 0 : opts.logger, opts === null || opts === void 0 ? void 0 : opts.router);
85
- this.opts = opts;
86
- this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-wf');
87
- this.wf = new WooksWorkflow(this.wooks);
88
- }
89
- attachSpy(fn) {
90
- return this.wf.attachSpy(fn);
91
- }
92
- detachSpy(fn) {
93
- return this.wf.detachSpy(fn);
94
- }
95
- step(id, opts) {
96
- const step = createStep(id, opts);
97
- return this.on('WF_STEP', id, () => step);
98
- }
99
- flow(id, schema, init) {
100
- this.wf.register(id, schema);
101
- return this.on('WF_FLOW', id, () => ({ init, id }));
102
- }
103
- start(schemaId, inputContext, input, spy, cleanup) {
104
- return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
105
- }
106
- resume(schemaId, state, input, spy, cleanup) {
107
- return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
108
- }
109
- _start(schemaId, inputContext, indexes, input, spy, cleanup) {
110
- var _a, _b;
111
- return __awaiter(this, void 0, void 0, function* () {
112
- const resume = !!(indexes === null || indexes === void 0 ? void 0 : indexes.length);
113
- const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
114
- inputContext,
115
- schemaId,
116
- indexes,
117
- input,
118
- }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
119
- const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
120
- const handlers = foundHandlers ||
121
- (((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound]) ||
122
- null;
123
- if (handlers && handlers.length) {
124
- let result = {};
125
- let firstStep = true;
126
- const _spy = (...args) => {
127
- if (spy) {
128
- spy(...args);
129
- }
130
- if (firstStep && args[0] === 'step') {
131
- // cleanup input after the first step
132
- firstStep = false;
133
- restoreCtx();
134
- const { store } = useWFContext();
135
- store('event').set('input', undefined);
136
- }
137
- };
138
- try {
139
- for (const handler of handlers) {
140
- restoreCtx();
141
- const { id, init } = (yield handler());
142
- if (init) {
143
- restoreCtx();
144
- yield init();
145
- }
146
- restoreCtx();
147
- if (resume) {
148
- result = yield this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
149
- break;
150
- }
151
- else {
152
- result = yield this.wf.start(id, inputContext, input, _spy);
153
- break;
154
- }
155
- }
156
- }
157
- catch (e) {
158
- clean();
159
- throw e;
160
- }
161
- clean();
162
- clearCtx();
163
- return result;
164
- }
165
- clean();
166
- function clean() {
167
- if (cleanup) {
168
- restoreCtx();
169
- cleanup();
170
- }
171
- }
172
- clearCtx();
173
- throw new Error('Unknown schemaId: ' + schemaId);
174
- });
175
- }
176
- onError(e) {
177
- var _a;
178
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
179
- this.opts.onError(e);
180
- }
181
- else {
182
- this.error(e.message);
183
- process.exit(1);
184
- }
185
- }
186
- error(e) {
187
- if (typeof e === 'string') {
188
- console.error('' + 'ERROR: ' + '' + e);
189
- }
190
- else {
191
- console.error('' + 'ERROR: ' + '' + e.message);
192
- }
193
- }
194
- }
195
- /**
196
- * Factory for WooksWf App
197
- * @param opts TWooksWfOptions
198
- * @param wooks Wooks | WooksAdapterBase
199
- * @returns WooksWf
200
- */
201
- function createWfApp(opts, wooks) {
202
- return new WooksWf(opts, wooks);
58
+ const wfShortcuts = {
59
+ flow: 'WF_FLOW',
60
+ step: 'WF_STEP',
61
+ };
62
+ class WooksWf extends WooksAdapterBase {
63
+ constructor(opts, wooks) {
64
+ super(wooks, opts?.logger, opts?.router);
65
+ this.opts = opts;
66
+ this.logger = opts?.logger || this.getLogger('wooks-wf');
67
+ this.wf = new WooksWorkflow(this.wooks);
68
+ }
69
+ attachSpy(fn) {
70
+ return this.wf.attachSpy(fn);
71
+ }
72
+ detachSpy(fn) {
73
+ return this.wf.detachSpy(fn);
74
+ }
75
+ step(id, opts) {
76
+ const step = createStep(id, opts);
77
+ return this.on('WF_STEP', id, () => step);
78
+ }
79
+ flow(id, schema, init) {
80
+ this.wf.register(id, schema);
81
+ return this.on('WF_FLOW', id, () => ({ init, id }));
82
+ }
83
+ start(schemaId, inputContext, input, spy, cleanup) {
84
+ return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
85
+ }
86
+ resume(schemaId, state, input, spy, cleanup) {
87
+ return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
88
+ }
89
+ async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
90
+ const resume = !!indexes?.length;
91
+ const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
92
+ inputContext,
93
+ schemaId,
94
+ indexes,
95
+ input,
96
+ }, this.mergeEventOptions(this.opts?.eventOptions));
97
+ const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
98
+ const handlers = foundHandlers ||
99
+ (this.opts?.onNotFound && [this.opts.onNotFound]) ||
100
+ null;
101
+ if (handlers && handlers.length) {
102
+ let result = {};
103
+ let firstStep = true;
104
+ const _spy = (...args) => {
105
+ if (spy) {
106
+ spy(...args);
107
+ }
108
+ if (firstStep && args[0] === 'step') {
109
+ // cleanup input after the first step
110
+ firstStep = false;
111
+ restoreCtx();
112
+ const { store } = useWFContext();
113
+ store('event').set('input', undefined);
114
+ }
115
+ };
116
+ try {
117
+ for (const handler of handlers) {
118
+ restoreCtx();
119
+ const { id, init } = (await handler());
120
+ if (init) {
121
+ restoreCtx();
122
+ await init();
123
+ }
124
+ restoreCtx();
125
+ if (resume) {
126
+ result = await this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
127
+ break;
128
+ }
129
+ else {
130
+ result = await this.wf.start(id, inputContext, input, _spy);
131
+ break;
132
+ }
133
+ }
134
+ }
135
+ catch (e) {
136
+ clean();
137
+ throw e;
138
+ }
139
+ clean();
140
+ clearCtx();
141
+ return result;
142
+ }
143
+ clean();
144
+ function clean() {
145
+ if (cleanup) {
146
+ restoreCtx();
147
+ cleanup();
148
+ }
149
+ }
150
+ clearCtx();
151
+ throw new Error('Unknown schemaId: ' + schemaId);
152
+ }
153
+ onError(e) {
154
+ if (this.opts?.onError) {
155
+ this.opts.onError(e);
156
+ }
157
+ else {
158
+ this.error(e.message);
159
+ process.exit(1);
160
+ }
161
+ }
162
+ error(e) {
163
+ if (typeof e === 'string') {
164
+ console.error('' + 'ERROR: ' + '' + e);
165
+ }
166
+ else {
167
+ console.error('' + 'ERROR: ' + '' + e.message);
168
+ }
169
+ }
170
+ }
171
+ /**
172
+ * Factory for WooksWf App
173
+ * @param opts TWooksWfOptions
174
+ * @param wooks Wooks | WooksAdapterBase
175
+ * @returns WooksWf
176
+ */
177
+ function createWfApp(opts, wooks) {
178
+ return new WooksWf(opts, wooks);
203
179
  }
204
180
 
205
- function useWfState() {
206
- const { store, getCtx } = useWFContext();
207
- const event = store('event');
208
- return {
209
- ctx: () => event.get('inputContext'),
210
- input: () => event.get('input'),
211
- schemaId: event.get('schemaId'),
212
- indexes: () => event.get('indexes'),
213
- resume: getCtx().resume,
214
- };
181
+ function useWfState() {
182
+ const { store, getCtx } = useWFContext();
183
+ const event = store('event');
184
+ return {
185
+ ctx: () => event.get('inputContext'),
186
+ input: () => event.get('input'),
187
+ schemaId: event.get('schemaId'),
188
+ indexes: () => event.get('indexes'),
189
+ resume: getCtx().resume,
190
+ };
215
191
  }
216
192
 
217
193
  export { WooksWf, createWfApp, createWfContext, resumeWfContext, useWFContext, useWfState, wfShortcuts };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-wf",
3
- "version": "0.4.9",
3
+ "version": "0.4.11",
4
4
  "description": "@wooksjs/event-wf",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -25,8 +25,8 @@
25
25
  "author": "Artem Maltsev",
26
26
  "license": "MIT",
27
27
  "peerDependencies": {
28
- "wooks": "0.4.9",
29
- "@wooksjs/event-core": "0.4.9"
28
+ "wooks": "0.4.11",
29
+ "@wooksjs/event-core": "0.4.11"
30
30
  },
31
31
  "dependencies": {
32
32
  "@prostojs/logger": "^0.3.7",