@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.
- package/dist/index.cjs +179 -203
- package/dist/index.mjs +179 -203
- 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:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
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
|
|
87
|
-
this.opts = opts;
|
|
88
|
-
this.logger =
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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('[31m' + 'ERROR: ' + '[0m' + e);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
console.error('[31m' + 'ERROR: ' + '[0m' + 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:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
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
|
|
85
|
-
this.opts = opts;
|
|
86
|
-
this.logger =
|
|
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
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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('[31m' + 'ERROR: ' + '[0m' + e);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
console.error('[31m' + 'ERROR: ' + '[0m' + 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.
|
|
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.
|
|
29
|
-
"@wooksjs/event-core": "0.4.
|
|
28
|
+
"wooks": "0.4.11",
|
|
29
|
+
"@wooksjs/event-core": "0.4.11"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@prostojs/logger": "^0.3.7",
|