@wooksjs/event-wf 0.4.10 → 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 +68 -99
- package/dist/index.mjs +68 -99
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -6,14 +6,20 @@ var wf = require('@prostojs/wf');
|
|
|
6
6
|
|
|
7
7
|
function createWfContext(data, options) {
|
|
8
8
|
return eventCore.createEventContext({
|
|
9
|
-
event:
|
|
9
|
+
event: {
|
|
10
|
+
...data,
|
|
11
|
+
type: 'WF',
|
|
12
|
+
},
|
|
10
13
|
resume: false,
|
|
11
14
|
options,
|
|
12
15
|
});
|
|
13
16
|
}
|
|
14
17
|
function resumeWfContext(data, options) {
|
|
15
18
|
return eventCore.createEventContext({
|
|
16
|
-
event:
|
|
19
|
+
event: {
|
|
20
|
+
...data,
|
|
21
|
+
type: 'WF',
|
|
22
|
+
},
|
|
17
23
|
resume: true,
|
|
18
24
|
options,
|
|
19
25
|
});
|
|
@@ -27,56 +33,23 @@ function useWFContext() {
|
|
|
27
33
|
return eventCore.useEventContext('WF');
|
|
28
34
|
}
|
|
29
35
|
|
|
30
|
-
/******************************************************************************
|
|
31
|
-
Copyright (c) Microsoft Corporation.
|
|
32
|
-
|
|
33
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
34
|
-
purpose with or without fee is hereby granted.
|
|
35
|
-
|
|
36
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
37
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
38
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
39
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
40
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
41
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
42
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
43
|
-
***************************************************************************** */
|
|
44
|
-
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
48
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
49
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
50
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
51
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
52
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
53
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
58
|
-
var e = new Error(message);
|
|
59
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
36
|
class WooksWorkflow extends wf.Workflow {
|
|
63
37
|
constructor(wooks) {
|
|
64
38
|
super([]);
|
|
65
39
|
this.wooks = wooks;
|
|
66
40
|
}
|
|
67
41
|
resolveStep(stepId) {
|
|
68
|
-
var _a, _b, _c;
|
|
69
42
|
try {
|
|
70
43
|
useWFContext();
|
|
71
44
|
const found = this.wooks.lookup('WF_STEP', '/' + stepId);
|
|
72
|
-
if (
|
|
45
|
+
if (found?.handlers?.length) {
|
|
73
46
|
return found.handlers[0]();
|
|
74
47
|
}
|
|
75
48
|
}
|
|
76
49
|
catch (e) {
|
|
77
50
|
const router = this.wooks.getRouter();
|
|
78
51
|
const found = router.lookup('WF_STEP', '/' + stepId);
|
|
79
|
-
if (
|
|
52
|
+
if (found?.route?.handlers?.length) {
|
|
80
53
|
return found.route.handlers[0]();
|
|
81
54
|
}
|
|
82
55
|
}
|
|
@@ -90,9 +63,9 @@ const wfShortcuts = {
|
|
|
90
63
|
};
|
|
91
64
|
class WooksWf extends wooks.WooksAdapterBase {
|
|
92
65
|
constructor(opts, wooks) {
|
|
93
|
-
super(wooks, opts
|
|
66
|
+
super(wooks, opts?.logger, opts?.router);
|
|
94
67
|
this.opts = opts;
|
|
95
|
-
this.logger =
|
|
68
|
+
this.logger = opts?.logger || this.getLogger('wooks-wf');
|
|
96
69
|
this.wf = new WooksWorkflow(this.wooks);
|
|
97
70
|
}
|
|
98
71
|
attachSpy(fn) {
|
|
@@ -115,76 +88,72 @@ class WooksWf extends wooks.WooksAdapterBase {
|
|
|
115
88
|
resume(schemaId, state, input, spy, cleanup) {
|
|
116
89
|
return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
|
|
117
90
|
}
|
|
118
|
-
_start(schemaId, inputContext, indexes, input, spy, cleanup) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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) {
|
|
142
123
|
restoreCtx();
|
|
143
|
-
|
|
144
|
-
store('event').set('input', undefined);
|
|
124
|
+
await init();
|
|
145
125
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
}
|
|
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;
|
|
164
134
|
}
|
|
165
135
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
throw e;
|
|
169
|
-
}
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
170
138
|
clean();
|
|
171
|
-
|
|
172
|
-
return result;
|
|
139
|
+
throw e;
|
|
173
140
|
}
|
|
174
141
|
clean();
|
|
175
|
-
function clean() {
|
|
176
|
-
if (cleanup) {
|
|
177
|
-
restoreCtx();
|
|
178
|
-
cleanup();
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
142
|
clearCtx();
|
|
182
|
-
|
|
183
|
-
}
|
|
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);
|
|
184
154
|
}
|
|
185
155
|
onError(e) {
|
|
186
|
-
|
|
187
|
-
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
|
|
156
|
+
if (this.opts?.onError) {
|
|
188
157
|
this.opts.onError(e);
|
|
189
158
|
}
|
|
190
159
|
else {
|
package/dist/index.mjs
CHANGED
|
@@ -4,14 +4,20 @@ import { Workflow, createStep } from '@prostojs/wf';
|
|
|
4
4
|
|
|
5
5
|
function createWfContext(data, options) {
|
|
6
6
|
return createEventContext({
|
|
7
|
-
event:
|
|
7
|
+
event: {
|
|
8
|
+
...data,
|
|
9
|
+
type: 'WF',
|
|
10
|
+
},
|
|
8
11
|
resume: false,
|
|
9
12
|
options,
|
|
10
13
|
});
|
|
11
14
|
}
|
|
12
15
|
function resumeWfContext(data, options) {
|
|
13
16
|
return createEventContext({
|
|
14
|
-
event:
|
|
17
|
+
event: {
|
|
18
|
+
...data,
|
|
19
|
+
type: 'WF',
|
|
20
|
+
},
|
|
15
21
|
resume: true,
|
|
16
22
|
options,
|
|
17
23
|
});
|
|
@@ -25,56 +31,23 @@ function useWFContext() {
|
|
|
25
31
|
return useEventContext('WF');
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
/******************************************************************************
|
|
29
|
-
Copyright (c) Microsoft Corporation.
|
|
30
|
-
|
|
31
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
32
|
-
purpose with or without fee is hereby granted.
|
|
33
|
-
|
|
34
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
35
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
36
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
37
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
38
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
39
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
40
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
41
|
-
***************************************************************************** */
|
|
42
|
-
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
46
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
47
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
48
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
49
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
50
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
51
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
56
|
-
var e = new Error(message);
|
|
57
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
34
|
class WooksWorkflow extends Workflow {
|
|
61
35
|
constructor(wooks) {
|
|
62
36
|
super([]);
|
|
63
37
|
this.wooks = wooks;
|
|
64
38
|
}
|
|
65
39
|
resolveStep(stepId) {
|
|
66
|
-
var _a, _b, _c;
|
|
67
40
|
try {
|
|
68
41
|
useWFContext();
|
|
69
42
|
const found = this.wooks.lookup('WF_STEP', '/' + stepId);
|
|
70
|
-
if (
|
|
43
|
+
if (found?.handlers?.length) {
|
|
71
44
|
return found.handlers[0]();
|
|
72
45
|
}
|
|
73
46
|
}
|
|
74
47
|
catch (e) {
|
|
75
48
|
const router = this.wooks.getRouter();
|
|
76
49
|
const found = router.lookup('WF_STEP', '/' + stepId);
|
|
77
|
-
if (
|
|
50
|
+
if (found?.route?.handlers?.length) {
|
|
78
51
|
return found.route.handlers[0]();
|
|
79
52
|
}
|
|
80
53
|
}
|
|
@@ -88,9 +61,9 @@ const wfShortcuts = {
|
|
|
88
61
|
};
|
|
89
62
|
class WooksWf extends WooksAdapterBase {
|
|
90
63
|
constructor(opts, wooks) {
|
|
91
|
-
super(wooks, opts
|
|
64
|
+
super(wooks, opts?.logger, opts?.router);
|
|
92
65
|
this.opts = opts;
|
|
93
|
-
this.logger =
|
|
66
|
+
this.logger = opts?.logger || this.getLogger('wooks-wf');
|
|
94
67
|
this.wf = new WooksWorkflow(this.wooks);
|
|
95
68
|
}
|
|
96
69
|
attachSpy(fn) {
|
|
@@ -113,76 +86,72 @@ class WooksWf extends WooksAdapterBase {
|
|
|
113
86
|
resume(schemaId, state, input, spy, cleanup) {
|
|
114
87
|
return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
|
|
115
88
|
}
|
|
116
|
-
_start(schemaId, inputContext, indexes, input, spy, cleanup) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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) {
|
|
140
121
|
restoreCtx();
|
|
141
|
-
|
|
142
|
-
store('event').set('input', undefined);
|
|
122
|
+
await init();
|
|
143
123
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
}
|
|
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;
|
|
162
132
|
}
|
|
163
133
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
throw e;
|
|
167
|
-
}
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
168
136
|
clean();
|
|
169
|
-
|
|
170
|
-
return result;
|
|
137
|
+
throw e;
|
|
171
138
|
}
|
|
172
139
|
clean();
|
|
173
|
-
function clean() {
|
|
174
|
-
if (cleanup) {
|
|
175
|
-
restoreCtx();
|
|
176
|
-
cleanup();
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
140
|
clearCtx();
|
|
180
|
-
|
|
181
|
-
}
|
|
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);
|
|
182
152
|
}
|
|
183
153
|
onError(e) {
|
|
184
|
-
|
|
185
|
-
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
|
|
154
|
+
if (this.opts?.onError) {
|
|
186
155
|
this.opts.onError(e);
|
|
187
156
|
}
|
|
188
157
|
else {
|
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",
|