@wooksjs/event-wf 0.4.9 → 0.4.10

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 +187 -180
  2. package/dist/index.mjs +187 -180
  3. package/package.json +3 -3
package/dist/index.cjs CHANGED
@@ -4,27 +4,27 @@ 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: 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');
28
28
  }
29
29
 
30
30
  /******************************************************************************
@@ -41,6 +41,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
41
41
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
42
42
  PERFORMANCE OF THIS SOFTWARE.
43
43
  ***************************************************************************** */
44
+ /* global Reflect, Promise, SuppressedError, Symbol */
45
+
44
46
 
45
47
  function __awaiter(thisArg, _arguments, P, generator) {
46
48
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -50,170 +52,175 @@ function __awaiter(thisArg, _arguments, P, generator) {
50
52
  function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
51
53
  step((generator = generator.apply(thisArg, _arguments || [])).next());
52
54
  });
53
- }
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
+ };
54
61
 
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
- }
62
+ class WooksWorkflow extends wf.Workflow {
63
+ constructor(wooks) {
64
+ super([]);
65
+ this.wooks = wooks;
66
+ }
67
+ resolveStep(stepId) {
68
+ var _a, _b, _c;
69
+ try {
70
+ useWFContext();
71
+ 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) {
73
+ return found.handlers[0]();
74
+ }
75
+ }
76
+ catch (e) {
77
+ const router = this.wooks.getRouter();
78
+ 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) {
80
+ return found.route.handlers[0]();
81
+ }
82
+ }
83
+ throw new Error(`Step "${stepId}" not found.`);
84
+ }
78
85
  }
79
86
 
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);
87
+ const wfShortcuts = {
88
+ flow: 'WF_FLOW',
89
+ step: 'WF_STEP',
90
+ };
91
+ class WooksWf extends wooks.WooksAdapterBase {
92
+ constructor(opts, wooks) {
93
+ super(wooks, opts === null || opts === void 0 ? void 0 : opts.logger, opts === null || opts === void 0 ? void 0 : opts.router);
94
+ this.opts = opts;
95
+ this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-wf');
96
+ this.wf = new WooksWorkflow(this.wooks);
97
+ }
98
+ attachSpy(fn) {
99
+ return this.wf.attachSpy(fn);
100
+ }
101
+ detachSpy(fn) {
102
+ return this.wf.detachSpy(fn);
103
+ }
104
+ step(id, opts) {
105
+ const step = wf.createStep(id, opts);
106
+ return this.on('WF_STEP', id, () => step);
107
+ }
108
+ flow(id, schema, init) {
109
+ this.wf.register(id, schema);
110
+ return this.on('WF_FLOW', id, () => ({ init, id }));
111
+ }
112
+ start(schemaId, inputContext, input, spy, cleanup) {
113
+ return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
114
+ }
115
+ resume(schemaId, state, input, spy, cleanup) {
116
+ return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
117
+ }
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;
142
+ restoreCtx();
143
+ const { store } = useWFContext();
144
+ store('event').set('input', undefined);
145
+ }
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
+ }
164
+ }
165
+ }
166
+ catch (e) {
167
+ clean();
168
+ throw e;
169
+ }
170
+ clean();
171
+ clearCtx();
172
+ return result;
173
+ }
174
+ clean();
175
+ function clean() {
176
+ if (cleanup) {
177
+ restoreCtx();
178
+ cleanup();
179
+ }
180
+ }
181
+ clearCtx();
182
+ throw new Error('Unknown schemaId: ' + schemaId);
183
+ });
184
+ }
185
+ onError(e) {
186
+ var _a;
187
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
188
+ this.opts.onError(e);
189
+ }
190
+ else {
191
+ this.error(e.message);
192
+ process.exit(1);
193
+ }
194
+ }
195
+ error(e) {
196
+ if (typeof e === 'string') {
197
+ console.error('' + 'ERROR: ' + '' + e);
198
+ }
199
+ else {
200
+ console.error('' + 'ERROR: ' + '' + e.message);
201
+ }
202
+ }
203
+ }
204
+ /**
205
+ * Factory for WooksWf App
206
+ * @param opts TWooksWfOptions
207
+ * @param wooks Wooks | WooksAdapterBase
208
+ * @returns WooksWf
209
+ */
210
+ function createWfApp(opts, wooks) {
211
+ return new WooksWf(opts, wooks);
205
212
  }
206
213
 
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
- };
214
+ function useWfState() {
215
+ const { store, getCtx } = useWFContext();
216
+ const event = store('event');
217
+ return {
218
+ ctx: () => event.get('inputContext'),
219
+ input: () => event.get('input'),
220
+ schemaId: event.get('schemaId'),
221
+ indexes: () => event.get('indexes'),
222
+ resume: getCtx().resume,
223
+ };
217
224
  }
218
225
 
219
226
  exports.WooksWf = WooksWf;
package/dist/index.mjs CHANGED
@@ -2,27 +2,27 @@ 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: 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');
26
26
  }
27
27
 
28
28
  /******************************************************************************
@@ -39,6 +39,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
39
39
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
40
40
  PERFORMANCE OF THIS SOFTWARE.
41
41
  ***************************************************************************** */
42
+ /* global Reflect, Promise, SuppressedError, Symbol */
43
+
42
44
 
43
45
  function __awaiter(thisArg, _arguments, P, generator) {
44
46
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -48,170 +50,175 @@ function __awaiter(thisArg, _arguments, P, generator) {
48
50
  function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
49
51
  step((generator = generator.apply(thisArg, _arguments || [])).next());
50
52
  });
51
- }
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
+ };
52
59
 
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
- }
60
+ class WooksWorkflow extends Workflow {
61
+ constructor(wooks) {
62
+ super([]);
63
+ this.wooks = wooks;
64
+ }
65
+ resolveStep(stepId) {
66
+ var _a, _b, _c;
67
+ try {
68
+ useWFContext();
69
+ 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) {
71
+ return found.handlers[0]();
72
+ }
73
+ }
74
+ catch (e) {
75
+ const router = this.wooks.getRouter();
76
+ 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) {
78
+ return found.route.handlers[0]();
79
+ }
80
+ }
81
+ throw new Error(`Step "${stepId}" not found.`);
82
+ }
76
83
  }
77
84
 
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);
85
+ const wfShortcuts = {
86
+ flow: 'WF_FLOW',
87
+ step: 'WF_STEP',
88
+ };
89
+ class WooksWf extends WooksAdapterBase {
90
+ constructor(opts, wooks) {
91
+ super(wooks, opts === null || opts === void 0 ? void 0 : opts.logger, opts === null || opts === void 0 ? void 0 : opts.router);
92
+ this.opts = opts;
93
+ this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-wf');
94
+ this.wf = new WooksWorkflow(this.wooks);
95
+ }
96
+ attachSpy(fn) {
97
+ return this.wf.attachSpy(fn);
98
+ }
99
+ detachSpy(fn) {
100
+ return this.wf.detachSpy(fn);
101
+ }
102
+ step(id, opts) {
103
+ const step = createStep(id, opts);
104
+ return this.on('WF_STEP', id, () => step);
105
+ }
106
+ flow(id, schema, init) {
107
+ this.wf.register(id, schema);
108
+ return this.on('WF_FLOW', id, () => ({ init, id }));
109
+ }
110
+ start(schemaId, inputContext, input, spy, cleanup) {
111
+ return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
112
+ }
113
+ resume(schemaId, state, input, spy, cleanup) {
114
+ return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
115
+ }
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;
140
+ restoreCtx();
141
+ const { store } = useWFContext();
142
+ store('event').set('input', undefined);
143
+ }
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
+ }
162
+ }
163
+ }
164
+ catch (e) {
165
+ clean();
166
+ throw e;
167
+ }
168
+ clean();
169
+ clearCtx();
170
+ return result;
171
+ }
172
+ clean();
173
+ function clean() {
174
+ if (cleanup) {
175
+ restoreCtx();
176
+ cleanup();
177
+ }
178
+ }
179
+ clearCtx();
180
+ throw new Error('Unknown schemaId: ' + schemaId);
181
+ });
182
+ }
183
+ onError(e) {
184
+ var _a;
185
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
186
+ this.opts.onError(e);
187
+ }
188
+ else {
189
+ this.error(e.message);
190
+ process.exit(1);
191
+ }
192
+ }
193
+ error(e) {
194
+ if (typeof e === 'string') {
195
+ console.error('' + 'ERROR: ' + '' + e);
196
+ }
197
+ else {
198
+ console.error('' + 'ERROR: ' + '' + e.message);
199
+ }
200
+ }
201
+ }
202
+ /**
203
+ * Factory for WooksWf App
204
+ * @param opts TWooksWfOptions
205
+ * @param wooks Wooks | WooksAdapterBase
206
+ * @returns WooksWf
207
+ */
208
+ function createWfApp(opts, wooks) {
209
+ return new WooksWf(opts, wooks);
203
210
  }
204
211
 
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
- };
212
+ function useWfState() {
213
+ const { store, getCtx } = useWFContext();
214
+ const event = store('event');
215
+ return {
216
+ ctx: () => event.get('inputContext'),
217
+ input: () => event.get('input'),
218
+ schemaId: event.get('schemaId'),
219
+ indexes: () => event.get('indexes'),
220
+ resume: getCtx().resume,
221
+ };
215
222
  }
216
223
 
217
224
  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.10",
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.10",
29
+ "@wooksjs/event-core": "0.4.10"
30
30
  },
31
31
  "dependencies": {
32
32
  "@prostojs/logger": "^0.3.7",