@wooksjs/event-wf 0.4.10 → 0.4.12
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 +67 -110
- package/dist/index.d.ts +144 -162
- package/dist/index.mjs +67 -110
- package/package.json +12 -4
package/dist/index.cjs
CHANGED
|
@@ -6,77 +6,45 @@ 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
|
});
|
|
20
26
|
}
|
|
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
27
|
function useWFContext() {
|
|
27
28
|
return eventCore.useEventContext('WF');
|
|
28
29
|
}
|
|
29
30
|
|
|
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
31
|
class WooksWorkflow extends wf.Workflow {
|
|
63
32
|
constructor(wooks) {
|
|
64
33
|
super([]);
|
|
65
34
|
this.wooks = wooks;
|
|
66
35
|
}
|
|
67
36
|
resolveStep(stepId) {
|
|
68
|
-
var _a, _b, _c;
|
|
69
37
|
try {
|
|
70
38
|
useWFContext();
|
|
71
39
|
const found = this.wooks.lookup('WF_STEP', '/' + stepId);
|
|
72
|
-
if (
|
|
40
|
+
if (found?.handlers?.length) {
|
|
73
41
|
return found.handlers[0]();
|
|
74
42
|
}
|
|
75
43
|
}
|
|
76
44
|
catch (e) {
|
|
77
45
|
const router = this.wooks.getRouter();
|
|
78
46
|
const found = router.lookup('WF_STEP', '/' + stepId);
|
|
79
|
-
if (
|
|
47
|
+
if (found?.route?.handlers?.length) {
|
|
80
48
|
return found.route.handlers[0]();
|
|
81
49
|
}
|
|
82
50
|
}
|
|
@@ -90,9 +58,9 @@ const wfShortcuts = {
|
|
|
90
58
|
};
|
|
91
59
|
class WooksWf extends wooks.WooksAdapterBase {
|
|
92
60
|
constructor(opts, wooks) {
|
|
93
|
-
super(wooks, opts
|
|
61
|
+
super(wooks, opts?.logger, opts?.router);
|
|
94
62
|
this.opts = opts;
|
|
95
|
-
this.logger =
|
|
63
|
+
this.logger = opts?.logger || this.getLogger('wooks-wf');
|
|
96
64
|
this.wf = new WooksWorkflow(this.wooks);
|
|
97
65
|
}
|
|
98
66
|
attachSpy(fn) {
|
|
@@ -115,76 +83,71 @@ class WooksWf extends wooks.WooksAdapterBase {
|
|
|
115
83
|
resume(schemaId, state, input, spy, cleanup) {
|
|
116
84
|
return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
|
|
117
85
|
}
|
|
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
|
-
|
|
86
|
+
async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
|
|
87
|
+
const resume = !!indexes?.length;
|
|
88
|
+
const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
|
|
89
|
+
inputContext,
|
|
90
|
+
schemaId,
|
|
91
|
+
indexes,
|
|
92
|
+
input,
|
|
93
|
+
}, this.mergeEventOptions(this.opts?.eventOptions));
|
|
94
|
+
const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
|
|
95
|
+
const handlers = foundHandlers ||
|
|
96
|
+
(this.opts?.onNotFound && [this.opts.onNotFound]) ||
|
|
97
|
+
null;
|
|
98
|
+
if (handlers && handlers.length) {
|
|
99
|
+
let result = {};
|
|
100
|
+
let firstStep = true;
|
|
101
|
+
const _spy = (...args) => {
|
|
102
|
+
if (spy) {
|
|
103
|
+
spy(...args);
|
|
104
|
+
}
|
|
105
|
+
if (firstStep && args[0] === 'step') {
|
|
106
|
+
firstStep = false;
|
|
107
|
+
restoreCtx();
|
|
108
|
+
const { store } = useWFContext();
|
|
109
|
+
store('event').set('input', undefined);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
try {
|
|
113
|
+
for (const handler of handlers) {
|
|
114
|
+
restoreCtx();
|
|
115
|
+
const { id, init } = (await handler());
|
|
116
|
+
if (init) {
|
|
142
117
|
restoreCtx();
|
|
143
|
-
|
|
144
|
-
store('event').set('input', undefined);
|
|
118
|
+
await init();
|
|
145
119
|
}
|
|
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
|
-
}
|
|
120
|
+
restoreCtx();
|
|
121
|
+
if (resume) {
|
|
122
|
+
result = await this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
result = await this.wf.start(id, inputContext, input, _spy);
|
|
127
|
+
break;
|
|
164
128
|
}
|
|
165
129
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
throw e;
|
|
169
|
-
}
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
170
132
|
clean();
|
|
171
|
-
|
|
172
|
-
return result;
|
|
133
|
+
throw e;
|
|
173
134
|
}
|
|
174
135
|
clean();
|
|
175
|
-
function clean() {
|
|
176
|
-
if (cleanup) {
|
|
177
|
-
restoreCtx();
|
|
178
|
-
cleanup();
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
136
|
clearCtx();
|
|
182
|
-
|
|
183
|
-
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
clean();
|
|
140
|
+
function clean() {
|
|
141
|
+
if (cleanup) {
|
|
142
|
+
restoreCtx();
|
|
143
|
+
cleanup();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
clearCtx();
|
|
147
|
+
throw new Error('Unknown schemaId: ' + schemaId);
|
|
184
148
|
}
|
|
185
149
|
onError(e) {
|
|
186
|
-
|
|
187
|
-
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
|
|
150
|
+
if (this.opts?.onError) {
|
|
188
151
|
this.opts.onError(e);
|
|
189
152
|
}
|
|
190
153
|
else {
|
|
@@ -201,12 +164,6 @@ class WooksWf extends wooks.WooksAdapterBase {
|
|
|
201
164
|
}
|
|
202
165
|
}
|
|
203
166
|
}
|
|
204
|
-
/**
|
|
205
|
-
* Factory for WooksWf App
|
|
206
|
-
* @param opts TWooksWfOptions
|
|
207
|
-
* @param wooks Wooks | WooksAdapterBase
|
|
208
|
-
* @returns WooksWf
|
|
209
|
-
*/
|
|
210
167
|
function createWfApp(opts, wooks) {
|
|
211
168
|
return new WooksWf(opts, wooks);
|
|
212
169
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,162 +1,144 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
input
|
|
124
|
-
schemaId: string
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
flow(id: string, schema: TWorkflowSchema<T>, init?: () => void | Promise<void>): TProstoRouterPathHandle<Record<string, string | string[]>>;
|
|
146
|
-
start<I>(schemaId: string, inputContext: T, input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
|
|
147
|
-
resume<I>(schemaId: string, state: {
|
|
148
|
-
indexes: number[];
|
|
149
|
-
context: T;
|
|
150
|
-
}, input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
|
|
151
|
-
protected _start<I>(schemaId: string, inputContext: T, indexes?: number[], input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
|
|
152
|
-
protected onError(e: Error): void;
|
|
153
|
-
protected error(e: string | Error): void;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
declare class WooksWorkflow<T> extends Workflow<T> {
|
|
157
|
-
protected wooks: Wooks;
|
|
158
|
-
constructor(wooks: Wooks);
|
|
159
|
-
protected resolveStep<I, D>(stepId: string): Step<T, I, D>;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export { }
|
|
1
|
+
import * as _wooksjs_event_core from '@wooksjs/event-core';
|
|
2
|
+
import { TEventOptions, TEmpty } from '@wooksjs/event-core';
|
|
3
|
+
import * as _prostojs_router from '@prostojs/router';
|
|
4
|
+
import { Wooks, TWooksHandler, TWooksOptions, WooksAdapterBase } from 'wooks';
|
|
5
|
+
import { TConsoleBase } from '@prostojs/logger';
|
|
6
|
+
import { Workflow, Step, TWorkflowSpy, TStepHandler, TWorkflowSchema, TFlowOutput } from '@prostojs/wf';
|
|
7
|
+
export { TStepHandler, TWorkflowSchema } from '@prostojs/wf';
|
|
8
|
+
|
|
9
|
+
interface TWFEventData {
|
|
10
|
+
schemaId: string;
|
|
11
|
+
inputContext: unknown;
|
|
12
|
+
indexes?: number[];
|
|
13
|
+
input?: unknown;
|
|
14
|
+
type: 'WF';
|
|
15
|
+
}
|
|
16
|
+
interface TWFContextStore {
|
|
17
|
+
resume: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare function createWfContext(data: Omit<TWFEventData, 'type'>, options: TEventOptions): {
|
|
21
|
+
getCtx: () => TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>;
|
|
22
|
+
restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
|
|
23
|
+
clearCtx: () => null;
|
|
24
|
+
store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K) => {
|
|
25
|
+
value: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
|
|
26
|
+
hook: <K2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
|
|
27
|
+
value: Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
|
|
28
|
+
isDefined: boolean;
|
|
29
|
+
};
|
|
30
|
+
init: <K2_1 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1]) => Required<Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1];
|
|
31
|
+
set: <K2_2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_2, v: Required<(TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2]) => Required<(TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2];
|
|
32
|
+
get: <K2_3 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_3) => Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2_3];
|
|
33
|
+
has: <K2_4 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
|
|
34
|
+
del: <K2_5 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
|
|
35
|
+
entries: () => [string, unknown][];
|
|
36
|
+
clear: () => void;
|
|
37
|
+
};
|
|
38
|
+
getStore: <K_1 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K_1) => (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_1];
|
|
39
|
+
setStore: <K_2 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K_2, v: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_2]) => void;
|
|
40
|
+
};
|
|
41
|
+
declare function resumeWfContext(data: Omit<TWFEventData, 'type'>, options: TEventOptions): {
|
|
42
|
+
getCtx: () => TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>;
|
|
43
|
+
restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
|
|
44
|
+
clearCtx: () => null;
|
|
45
|
+
store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K) => {
|
|
46
|
+
value: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
|
|
47
|
+
hook: <K2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
|
|
48
|
+
value: Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
|
|
49
|
+
isDefined: boolean;
|
|
50
|
+
};
|
|
51
|
+
init: <K2_1 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1]) => Required<Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1];
|
|
52
|
+
set: <K2_2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_2, v: Required<(TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2]) => Required<(TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2];
|
|
53
|
+
get: <K2_3 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_3) => Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2_3];
|
|
54
|
+
has: <K2_4 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
|
|
55
|
+
del: <K2_5 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
|
|
56
|
+
entries: () => [string, unknown][];
|
|
57
|
+
clear: () => void;
|
|
58
|
+
};
|
|
59
|
+
getStore: <K_1 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K_1) => (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_1];
|
|
60
|
+
setStore: <K_2 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K_2, v: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_2]) => void;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Wrapper on top of useEventContext that provides
|
|
64
|
+
* proper context types for WF event
|
|
65
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
66
|
+
*/
|
|
67
|
+
declare function useWFContext<T extends TEmpty>(): {
|
|
68
|
+
getCtx: () => TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>;
|
|
69
|
+
restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
|
|
70
|
+
clearCtx: () => null;
|
|
71
|
+
store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData> | keyof T>(key: K) => {
|
|
72
|
+
value: (TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
|
|
73
|
+
hook: <K2 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
|
|
74
|
+
value: Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
|
|
75
|
+
isDefined: boolean;
|
|
76
|
+
};
|
|
77
|
+
init: <K2_1 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1]) => Required<Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>[K2_1];
|
|
78
|
+
set: <K2_2 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_2, v: Required<(TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2]) => Required<(TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K]>[K2_2];
|
|
79
|
+
get: <K2_3 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_3) => Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2_3];
|
|
80
|
+
has: <K2_4 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
|
|
81
|
+
del: <K2_5 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
|
|
82
|
+
entries: () => [string, unknown][];
|
|
83
|
+
clear: () => void;
|
|
84
|
+
};
|
|
85
|
+
getStore: <K_1 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData> | keyof T>(key: K_1) => (TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_1];
|
|
86
|
+
setStore: <K_2 extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData> | keyof T>(key: K_2, v: (TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K_2]) => void;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
declare class WooksWorkflow<T> extends Workflow<T> {
|
|
90
|
+
protected wooks: Wooks;
|
|
91
|
+
constructor(wooks: Wooks);
|
|
92
|
+
protected resolveStep<I, D>(stepId: string): Step<T, I, D>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
declare const wfShortcuts: {
|
|
96
|
+
flow: string;
|
|
97
|
+
step: string;
|
|
98
|
+
};
|
|
99
|
+
interface TWooksWfOptions {
|
|
100
|
+
onError?(e: Error): void;
|
|
101
|
+
onNotFound?: TWooksHandler<unknown>;
|
|
102
|
+
onUnknownFlow?: (schemaId: string, raiseError: () => void) => unknown;
|
|
103
|
+
logger?: TConsoleBase;
|
|
104
|
+
eventOptions?: TEventOptions;
|
|
105
|
+
router?: TWooksOptions['router'];
|
|
106
|
+
}
|
|
107
|
+
declare class WooksWf<T> extends WooksAdapterBase {
|
|
108
|
+
protected opts?: TWooksWfOptions | undefined;
|
|
109
|
+
protected logger: TConsoleBase;
|
|
110
|
+
protected wf: WooksWorkflow<T>;
|
|
111
|
+
constructor(opts?: TWooksWfOptions | undefined, wooks?: Wooks | WooksAdapterBase);
|
|
112
|
+
attachSpy<I>(fn: TWorkflowSpy<T, I>): () => void;
|
|
113
|
+
detachSpy<I>(fn: TWorkflowSpy<T, I>): void;
|
|
114
|
+
step<I = any, D = any>(id: string, opts: {
|
|
115
|
+
input?: D;
|
|
116
|
+
handler: string | TStepHandler<T, I, D>;
|
|
117
|
+
}): _prostojs_router.TProstoRouterPathHandle<Record<string, string | string[]>>;
|
|
118
|
+
flow(id: string, schema: TWorkflowSchema<T>, init?: () => void | Promise<void>): _prostojs_router.TProstoRouterPathHandle<Record<string, string | string[]>>;
|
|
119
|
+
start<I>(schemaId: string, inputContext: T, input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
|
|
120
|
+
resume<I>(schemaId: string, state: {
|
|
121
|
+
indexes: number[];
|
|
122
|
+
context: T;
|
|
123
|
+
}, input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
|
|
124
|
+
protected _start<I>(schemaId: string, inputContext: T, indexes?: number[], input?: I, spy?: TWorkflowSpy<T, I>, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
|
|
125
|
+
protected onError(e: Error): void;
|
|
126
|
+
protected error(e: string | Error): void;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Factory for WooksWf App
|
|
130
|
+
* @param opts TWooksWfOptions
|
|
131
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
132
|
+
* @returns WooksWf
|
|
133
|
+
*/
|
|
134
|
+
declare function createWfApp<T>(opts?: TWooksWfOptions, wooks?: Wooks | WooksAdapterBase): WooksWf<T>;
|
|
135
|
+
|
|
136
|
+
declare function useWfState(): {
|
|
137
|
+
ctx: <T>() => T;
|
|
138
|
+
input: <I>() => I | undefined;
|
|
139
|
+
schemaId: string;
|
|
140
|
+
indexes: () => number[] | undefined;
|
|
141
|
+
resume: boolean;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export { type TWFContextStore, type TWFEventData, type TWooksWfOptions, WooksWf, createWfApp, createWfContext, resumeWfContext, useWFContext, useWfState, wfShortcuts };
|
package/dist/index.mjs
CHANGED
|
@@ -4,77 +4,45 @@ 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
|
});
|
|
18
24
|
}
|
|
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
25
|
function useWFContext() {
|
|
25
26
|
return useEventContext('WF');
|
|
26
27
|
}
|
|
27
28
|
|
|
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
29
|
class WooksWorkflow extends Workflow {
|
|
61
30
|
constructor(wooks) {
|
|
62
31
|
super([]);
|
|
63
32
|
this.wooks = wooks;
|
|
64
33
|
}
|
|
65
34
|
resolveStep(stepId) {
|
|
66
|
-
var _a, _b, _c;
|
|
67
35
|
try {
|
|
68
36
|
useWFContext();
|
|
69
37
|
const found = this.wooks.lookup('WF_STEP', '/' + stepId);
|
|
70
|
-
if (
|
|
38
|
+
if (found?.handlers?.length) {
|
|
71
39
|
return found.handlers[0]();
|
|
72
40
|
}
|
|
73
41
|
}
|
|
74
42
|
catch (e) {
|
|
75
43
|
const router = this.wooks.getRouter();
|
|
76
44
|
const found = router.lookup('WF_STEP', '/' + stepId);
|
|
77
|
-
if (
|
|
45
|
+
if (found?.route?.handlers?.length) {
|
|
78
46
|
return found.route.handlers[0]();
|
|
79
47
|
}
|
|
80
48
|
}
|
|
@@ -88,9 +56,9 @@ const wfShortcuts = {
|
|
|
88
56
|
};
|
|
89
57
|
class WooksWf extends WooksAdapterBase {
|
|
90
58
|
constructor(opts, wooks) {
|
|
91
|
-
super(wooks, opts
|
|
59
|
+
super(wooks, opts?.logger, opts?.router);
|
|
92
60
|
this.opts = opts;
|
|
93
|
-
this.logger =
|
|
61
|
+
this.logger = opts?.logger || this.getLogger('wooks-wf');
|
|
94
62
|
this.wf = new WooksWorkflow(this.wooks);
|
|
95
63
|
}
|
|
96
64
|
attachSpy(fn) {
|
|
@@ -113,76 +81,71 @@ class WooksWf extends WooksAdapterBase {
|
|
|
113
81
|
resume(schemaId, state, input, spy, cleanup) {
|
|
114
82
|
return this._start(schemaId, state.context, state.indexes, input, spy, cleanup);
|
|
115
83
|
}
|
|
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
|
-
|
|
84
|
+
async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
|
|
85
|
+
const resume = !!indexes?.length;
|
|
86
|
+
const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
|
|
87
|
+
inputContext,
|
|
88
|
+
schemaId,
|
|
89
|
+
indexes,
|
|
90
|
+
input,
|
|
91
|
+
}, this.mergeEventOptions(this.opts?.eventOptions));
|
|
92
|
+
const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
|
|
93
|
+
const handlers = foundHandlers ||
|
|
94
|
+
(this.opts?.onNotFound && [this.opts.onNotFound]) ||
|
|
95
|
+
null;
|
|
96
|
+
if (handlers && handlers.length) {
|
|
97
|
+
let result = {};
|
|
98
|
+
let firstStep = true;
|
|
99
|
+
const _spy = (...args) => {
|
|
100
|
+
if (spy) {
|
|
101
|
+
spy(...args);
|
|
102
|
+
}
|
|
103
|
+
if (firstStep && args[0] === 'step') {
|
|
104
|
+
firstStep = false;
|
|
105
|
+
restoreCtx();
|
|
106
|
+
const { store } = useWFContext();
|
|
107
|
+
store('event').set('input', undefined);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
try {
|
|
111
|
+
for (const handler of handlers) {
|
|
112
|
+
restoreCtx();
|
|
113
|
+
const { id, init } = (await handler());
|
|
114
|
+
if (init) {
|
|
140
115
|
restoreCtx();
|
|
141
|
-
|
|
142
|
-
store('event').set('input', undefined);
|
|
116
|
+
await init();
|
|
143
117
|
}
|
|
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
|
-
}
|
|
118
|
+
restoreCtx();
|
|
119
|
+
if (resume) {
|
|
120
|
+
result = await this.wf.resume(id, { context: inputContext, indexes }, input, _spy);
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
result = await this.wf.start(id, inputContext, input, _spy);
|
|
125
|
+
break;
|
|
162
126
|
}
|
|
163
127
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
throw e;
|
|
167
|
-
}
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
168
130
|
clean();
|
|
169
|
-
|
|
170
|
-
return result;
|
|
131
|
+
throw e;
|
|
171
132
|
}
|
|
172
133
|
clean();
|
|
173
|
-
function clean() {
|
|
174
|
-
if (cleanup) {
|
|
175
|
-
restoreCtx();
|
|
176
|
-
cleanup();
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
134
|
clearCtx();
|
|
180
|
-
|
|
181
|
-
}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
clean();
|
|
138
|
+
function clean() {
|
|
139
|
+
if (cleanup) {
|
|
140
|
+
restoreCtx();
|
|
141
|
+
cleanup();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
clearCtx();
|
|
145
|
+
throw new Error('Unknown schemaId: ' + schemaId);
|
|
182
146
|
}
|
|
183
147
|
onError(e) {
|
|
184
|
-
|
|
185
|
-
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
|
|
148
|
+
if (this.opts?.onError) {
|
|
186
149
|
this.opts.onError(e);
|
|
187
150
|
}
|
|
188
151
|
else {
|
|
@@ -199,12 +162,6 @@ class WooksWf extends WooksAdapterBase {
|
|
|
199
162
|
}
|
|
200
163
|
}
|
|
201
164
|
}
|
|
202
|
-
/**
|
|
203
|
-
* Factory for WooksWf App
|
|
204
|
-
* @param opts TWooksWfOptions
|
|
205
|
-
* @param wooks Wooks | WooksAdapterBase
|
|
206
|
-
* @returns WooksWf
|
|
207
|
-
*/
|
|
208
165
|
function createWfApp(opts, wooks) {
|
|
209
166
|
return new WooksWf(opts, wooks);
|
|
210
167
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-wf",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.12",
|
|
4
4
|
"description": "@wooksjs/event-wf",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -8,6 +8,14 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
|
+
"exports": {
|
|
12
|
+
"./package.json": "./package.json",
|
|
13
|
+
".": {
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
11
19
|
"repository": {
|
|
12
20
|
"type": "git",
|
|
13
21
|
"url": "git+https://github.com/wooksjs/wooksjs.git",
|
|
@@ -25,11 +33,11 @@
|
|
|
25
33
|
"author": "Artem Maltsev",
|
|
26
34
|
"license": "MIT",
|
|
27
35
|
"peerDependencies": {
|
|
28
|
-
"wooks": "0.4.
|
|
29
|
-
"@wooksjs/event-core": "0.4.
|
|
36
|
+
"wooks": "0.4.12",
|
|
37
|
+
"@wooksjs/event-core": "0.4.12"
|
|
30
38
|
},
|
|
31
39
|
"dependencies": {
|
|
32
|
-
"@prostojs/logger": "^0.
|
|
40
|
+
"@prostojs/logger": "^0.4.0",
|
|
33
41
|
"@prostojs/wf": "^0.0.10"
|
|
34
42
|
},
|
|
35
43
|
"bugs": {
|