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