@wooksjs/event-wf 0.5.19 → 0.5.25
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/LICENSE +21 -0
- package/dist/index.cjs +192 -175
- package/dist/index.d.ts +5 -247
- package/dist/index.mjs +162 -167
- package/package.json +12 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 wooksjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
CHANGED
|
@@ -1,192 +1,209 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
2
23
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
24
|
+
//#endregion
|
|
25
|
+
const __wooksjs_event_core = __toESM(require("@wooksjs/event-core"));
|
|
26
|
+
const __prostojs_wf = __toESM(require("@prostojs/wf"));
|
|
27
|
+
const wooks = __toESM(require("wooks"));
|
|
6
28
|
|
|
29
|
+
//#region packages/event-wf/src/event-wf.ts
|
|
7
30
|
function createWfContext(data, options) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
31
|
+
return (0, __wooksjs_event_core.createAsyncEventContext)({
|
|
32
|
+
event: {
|
|
33
|
+
...data,
|
|
34
|
+
type: "WF"
|
|
35
|
+
},
|
|
36
|
+
resume: false,
|
|
37
|
+
options
|
|
38
|
+
});
|
|
16
39
|
}
|
|
17
40
|
function resumeWfContext(data, options) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
41
|
+
return (0, __wooksjs_event_core.createAsyncEventContext)({
|
|
42
|
+
event: {
|
|
43
|
+
...data,
|
|
44
|
+
type: "WF"
|
|
45
|
+
},
|
|
46
|
+
resume: true,
|
|
47
|
+
options
|
|
48
|
+
});
|
|
26
49
|
}
|
|
27
50
|
function useWFContext() {
|
|
28
|
-
|
|
51
|
+
return (0, __wooksjs_event_core.useAsyncEventContext)("WF");
|
|
29
52
|
}
|
|
30
53
|
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region packages/event-wf/src/composables/wf-state.ts
|
|
31
56
|
function useWfState() {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
57
|
+
const { store, getCtx } = useWFContext();
|
|
58
|
+
const event = store("event");
|
|
59
|
+
return {
|
|
60
|
+
ctx: () => event.get("inputContext"),
|
|
61
|
+
input: () => event.get("input"),
|
|
62
|
+
schemaId: event.get("schemaId"),
|
|
63
|
+
stepId: () => event.get("stepId"),
|
|
64
|
+
indexes: () => event.get("indexes"),
|
|
65
|
+
resume: getCtx().resume
|
|
66
|
+
};
|
|
42
67
|
}
|
|
43
68
|
|
|
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
|
+
//#endregion
|
|
70
|
+
//#region packages/event-wf/src/workflow.ts
|
|
71
|
+
var WooksWorkflow = class extends __prostojs_wf.Workflow {
|
|
72
|
+
constructor(wooks$1) {
|
|
73
|
+
super([]);
|
|
74
|
+
this.wooks = wooks$1;
|
|
75
|
+
}
|
|
76
|
+
resolveStep(stepId) {
|
|
77
|
+
const stepIdNorm = `/${stepId}`.replace(/\/{2,}/gu, "/");
|
|
78
|
+
try {
|
|
79
|
+
const store = useWFContext().store("event");
|
|
80
|
+
const found = this.wooks.lookup("WF_STEP", stepIdNorm);
|
|
81
|
+
if (found.handlers?.length) {
|
|
82
|
+
store.set("stepId", stepIdNorm);
|
|
83
|
+
return found.handlers[0]();
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
const router = this.wooks.getRouter();
|
|
87
|
+
const found = router.lookup("WF_STEP", stepIdNorm);
|
|
88
|
+
if (found?.route?.handlers.length) return found.route.handlers[0]();
|
|
89
|
+
}
|
|
90
|
+
throw new Error(`Step "${stepIdNorm}" not found.`);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
69
93
|
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region packages/event-wf/src/wf-adapter.ts
|
|
70
96
|
const wfShortcuts = {
|
|
71
|
-
|
|
72
|
-
|
|
97
|
+
flow: "WF_FLOW",
|
|
98
|
+
step: "WF_STEP"
|
|
73
99
|
};
|
|
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
|
-
|
|
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
|
-
if (typeof e === 'string') {
|
|
175
|
-
console.error(`${'[31m'}ERROR: ${'[0m'}${e}`);
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
console.error(`${'[31m'}ERROR: ${'[0m'}${e.message}`);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
function createWfApp(opts, wooks) {
|
|
183
|
-
return new WooksWf(opts, wooks);
|
|
100
|
+
var WooksWf = class extends wooks.WooksAdapterBase {
|
|
101
|
+
logger;
|
|
102
|
+
wf;
|
|
103
|
+
constructor(opts, wooks$1) {
|
|
104
|
+
super(wooks$1, opts?.logger, opts?.router);
|
|
105
|
+
this.opts = opts;
|
|
106
|
+
this.logger = opts?.logger || this.getLogger(`${"\x1B[96m"}[wooks-wf]`);
|
|
107
|
+
this.wf = new WooksWorkflow(this.wooks);
|
|
108
|
+
}
|
|
109
|
+
attachSpy(fn) {
|
|
110
|
+
return this.wf.attachSpy(fn);
|
|
111
|
+
}
|
|
112
|
+
detachSpy(fn) {
|
|
113
|
+
this.wf.detachSpy(fn);
|
|
114
|
+
}
|
|
115
|
+
step(id, opts) {
|
|
116
|
+
const step = (0, __prostojs_wf.createStep)(id, opts);
|
|
117
|
+
return this.on("WF_STEP", id, () => step);
|
|
118
|
+
}
|
|
119
|
+
flow(id, schema, prefix, init) {
|
|
120
|
+
this.wf.register(id, schema, prefix);
|
|
121
|
+
return this.on("WF_FLOW", id, () => ({
|
|
122
|
+
init,
|
|
123
|
+
id
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
start(schemaId, inputContext, input, spy, cleanup) {
|
|
127
|
+
return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
|
|
128
|
+
}
|
|
129
|
+
resume(state, input, spy, cleanup) {
|
|
130
|
+
return this._start(state.schemaId, state.context, state.indexes, input, spy, cleanup);
|
|
131
|
+
}
|
|
132
|
+
async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
|
|
133
|
+
const resume = !!indexes?.length;
|
|
134
|
+
const runInContext = (resume ? resumeWfContext : createWfContext)({
|
|
135
|
+
inputContext,
|
|
136
|
+
schemaId,
|
|
137
|
+
stepId: null,
|
|
138
|
+
indexes,
|
|
139
|
+
input
|
|
140
|
+
}, this.mergeEventOptions(this.opts?.eventOptions));
|
|
141
|
+
return runInContext(async () => {
|
|
142
|
+
const { handlers: foundHandlers } = this.wooks.lookup("WF_FLOW", `/${schemaId}`.replace(/^\/+/u, "/"));
|
|
143
|
+
const handlers = foundHandlers || this.opts?.onNotFound && [this.opts.onNotFound] || null;
|
|
144
|
+
if (handlers && handlers.length > 0) {
|
|
145
|
+
let result = {};
|
|
146
|
+
let firstStep = true;
|
|
147
|
+
const _spy = (...args) => {
|
|
148
|
+
if (spy) spy(...args);
|
|
149
|
+
if (firstStep && args[0] === "step") {
|
|
150
|
+
firstStep = false;
|
|
151
|
+
const { store } = useWFContext();
|
|
152
|
+
store("event").set("input", undefined);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
try {
|
|
156
|
+
for (const handler of handlers) {
|
|
157
|
+
const { id, init } = await handler();
|
|
158
|
+
if (init) await init();
|
|
159
|
+
if (resume) {
|
|
160
|
+
result = await this.wf.resume({
|
|
161
|
+
schemaId: id,
|
|
162
|
+
context: inputContext,
|
|
163
|
+
indexes
|
|
164
|
+
}, input, _spy);
|
|
165
|
+
break;
|
|
166
|
+
} else {
|
|
167
|
+
result = await this.wf.start(id, inputContext, input, _spy);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} catch (error) {
|
|
172
|
+
clean();
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
clean();
|
|
176
|
+
if (result.resume) result.resume = (_input) => this.resume(result.state, _input, spy, cleanup);
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
clean();
|
|
180
|
+
throw new Error(`Unknown schemaId: ${schemaId}`);
|
|
181
|
+
function clean() {
|
|
182
|
+
if (cleanup) cleanup();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
onError(e) {
|
|
187
|
+
if (this.opts?.onError) this.opts.onError(e);
|
|
188
|
+
else {
|
|
189
|
+
this.error(e.message);
|
|
190
|
+
process.exit(1);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
error(e) {
|
|
194
|
+
if (typeof e === "string") console.error(`${"\x1B[31m"}ERROR: ${"\x1B[0m"}${e}`);
|
|
195
|
+
else console.error(`${"\x1B[31m"}ERROR: ${"\x1B[0m"}${e.message}`);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
function createWfApp(opts, wooks$1) {
|
|
199
|
+
return new WooksWf(opts, wooks$1);
|
|
184
200
|
}
|
|
185
201
|
|
|
186
|
-
|
|
187
|
-
exports.
|
|
188
|
-
exports.
|
|
189
|
-
exports.
|
|
190
|
-
exports.
|
|
191
|
-
exports.
|
|
192
|
-
exports.
|
|
202
|
+
//#endregion
|
|
203
|
+
exports.WooksWf = WooksWf
|
|
204
|
+
exports.createWfApp = createWfApp
|
|
205
|
+
exports.createWfContext = createWfContext
|
|
206
|
+
exports.resumeWfContext = resumeWfContext
|
|
207
|
+
exports.useWFContext = useWFContext
|
|
208
|
+
exports.useWfState = useWfState
|
|
209
|
+
exports.wfShortcuts = wfShortcuts
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as _wooksjs_event_core from '@wooksjs/event-core';
|
|
2
2
|
import { TEventOptions, TEmpty } from '@wooksjs/event-core';
|
|
3
|
-
import * as
|
|
3
|
+
import * as wooks from 'wooks';
|
|
4
|
+
import { Wooks, TWooksHandler, TWooksOptions, WooksAdapterBase } from 'wooks';
|
|
4
5
|
import { TConsoleBase } from '@prostojs/logger';
|
|
5
6
|
import { Workflow, Step, TWorkflowSpy, TStepHandler, TWorkflowSchema, TFlowOutput } from '@prostojs/wf';
|
|
6
7
|
export { TStepHandler, TWorkflowSchema } from '@prostojs/wf';
|
|
7
|
-
import { Wooks, TWooksHandler, TWooksOptions, WooksAdapterBase } from 'wooks';
|
|
8
8
|
|
|
9
9
|
declare function useWfState(): {
|
|
10
10
|
ctx: <T>() => T;
|
|
@@ -29,243 +29,7 @@ interface TWFContextStore {
|
|
|
29
29
|
|
|
30
30
|
declare function createWfContext(data: Omit<TWFEventData, 'type'>, options: TEventOptions): <T>(cb: (...a: any[]) => T) => T;
|
|
31
31
|
declare function resumeWfContext(data: Omit<TWFEventData, 'type'>, options: TEventOptions): <T>(cb: (...a: any[]) => T) => T;
|
|
32
|
-
|
|
33
|
-
* Wrapper on top of useEventContext that provides
|
|
34
|
-
* proper context types for WF event
|
|
35
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
36
|
-
*/
|
|
37
|
-
declare function useWFContext<T extends TEmpty>(): {
|
|
38
|
-
getCtx: () => TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>;
|
|
39
|
-
store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData> | keyof T>(key: K) => {
|
|
40
|
-
value: (TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
|
|
41
|
-
hook: <K2 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
|
|
42
|
-
value: Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
|
|
43
|
-
isDefined: boolean;
|
|
44
|
-
};
|
|
45
|
-
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];
|
|
46
|
-
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];
|
|
47
|
-
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] | undefined;
|
|
48
|
-
has: <K2_4 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_4) => boolean;
|
|
49
|
-
del: <K2_5 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2_5) => void;
|
|
50
|
-
entries: () => [string, unknown][];
|
|
51
|
-
clear: () => void;
|
|
52
|
-
};
|
|
53
|
-
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];
|
|
54
|
-
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;
|
|
55
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
56
|
-
hasParentCtx: () => boolean;
|
|
57
|
-
getParentCtx: <T2 = TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>() => {
|
|
58
|
-
getCtx: () => T2;
|
|
59
|
-
store: <K_3 extends keyof T2>(key: K_3) => {
|
|
60
|
-
value: T2[K_3];
|
|
61
|
-
hook: <K2_6 extends keyof Required<T2>[K_3]>(key2: K2_6) => {
|
|
62
|
-
value: Required<T2>[K_3][K2_6];
|
|
63
|
-
isDefined: boolean;
|
|
64
|
-
};
|
|
65
|
-
init: <K2_7 extends keyof Required<T2>[K_3]>(key2: K2_7, getter: () => Required<Required<T2>[K_3]>[K2_7]) => Required<Required<T2>[K_3]>[K2_7];
|
|
66
|
-
set: <K2_8 extends keyof Required<T2>[K_3]>(key2: K2_8, v: Required<T2[K_3]>[K2_8]) => Required<T2[K_3]>[K2_8];
|
|
67
|
-
get: <K2_9 extends keyof Required<T2>[K_3]>(key2: K2_9) => Required<T2>[K_3][K2_9] | undefined;
|
|
68
|
-
has: <K2_10 extends keyof Required<T2>[K_3]>(key2: K2_10) => boolean;
|
|
69
|
-
del: <K2_11 extends keyof Required<T2>[K_3]>(key2: K2_11) => void;
|
|
70
|
-
entries: () => [string, unknown][];
|
|
71
|
-
clear: () => void;
|
|
72
|
-
};
|
|
73
|
-
getStore: <K_4 extends keyof T2>(key: K_4) => T2[K_4];
|
|
74
|
-
setStore: <K_5 extends keyof T2>(key: K_5, v: T2[K_5]) => void;
|
|
75
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
76
|
-
hasParentCtx: () => boolean;
|
|
77
|
-
getParentCtx: <T2_1 = T2>() => {
|
|
78
|
-
getCtx: () => T2_1;
|
|
79
|
-
store: <K_6 extends keyof T2_1>(key: K_6) => {
|
|
80
|
-
value: T2_1[K_6];
|
|
81
|
-
hook: <K2_12 extends keyof Required<T2_1>[K_6]>(key2: K2_12) => {
|
|
82
|
-
value: Required<T2_1>[K_6][K2_12];
|
|
83
|
-
isDefined: boolean;
|
|
84
|
-
};
|
|
85
|
-
init: <K2_13 extends keyof Required<T2_1>[K_6]>(key2: K2_13, getter: () => Required<Required<T2_1>[K_6]>[K2_13]) => Required<Required<T2_1>[K_6]>[K2_13];
|
|
86
|
-
set: <K2_14 extends keyof Required<T2_1>[K_6]>(key2: K2_14, v: Required<T2_1[K_6]>[K2_14]) => Required<T2_1[K_6]>[K2_14];
|
|
87
|
-
get: <K2_15 extends keyof Required<T2_1>[K_6]>(key2: K2_15) => Required<T2_1>[K_6][K2_15] | undefined;
|
|
88
|
-
has: <K2_16 extends keyof Required<T2_1>[K_6]>(key2: K2_16) => boolean;
|
|
89
|
-
del: <K2_17 extends keyof Required<T2_1>[K_6]>(key2: K2_17) => void;
|
|
90
|
-
entries: () => [string, unknown][];
|
|
91
|
-
clear: () => void;
|
|
92
|
-
};
|
|
93
|
-
getStore: <K_7 extends keyof T2_1>(key: K_7) => T2_1[K_7];
|
|
94
|
-
setStore: <K_8 extends keyof T2_1>(key: K_8, v: T2_1[K_8]) => void;
|
|
95
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
96
|
-
hasParentCtx: () => boolean;
|
|
97
|
-
getParentCtx: <T2_2 = T2_1>() => {
|
|
98
|
-
getCtx: () => T2_2;
|
|
99
|
-
store: <K_9 extends keyof T2_2>(key: K_9) => {
|
|
100
|
-
value: T2_2[K_9];
|
|
101
|
-
hook: <K2_18 extends keyof Required<T2_2>[K_9]>(key2: K2_18) => {
|
|
102
|
-
value: Required<T2_2>[K_9][K2_18];
|
|
103
|
-
isDefined: boolean;
|
|
104
|
-
};
|
|
105
|
-
init: <K2_19 extends keyof Required<T2_2>[K_9]>(key2: K2_19, getter: () => Required<Required<T2_2>[K_9]>[K2_19]) => Required<Required<T2_2>[K_9]>[K2_19];
|
|
106
|
-
set: <K2_20 extends keyof Required<T2_2>[K_9]>(key2: K2_20, v: Required<T2_2[K_9]>[K2_20]) => Required<T2_2[K_9]>[K2_20];
|
|
107
|
-
get: <K2_21 extends keyof Required<T2_2>[K_9]>(key2: K2_21) => Required<T2_2>[K_9][K2_21] | undefined;
|
|
108
|
-
has: <K2_22 extends keyof Required<T2_2>[K_9]>(key2: K2_22) => boolean;
|
|
109
|
-
del: <K2_23 extends keyof Required<T2_2>[K_9]>(key2: K2_23) => void;
|
|
110
|
-
entries: () => [string, unknown][];
|
|
111
|
-
clear: () => void;
|
|
112
|
-
};
|
|
113
|
-
getStore: <K_10 extends keyof T2_2>(key: K_10) => T2_2[K_10];
|
|
114
|
-
setStore: <K_11 extends keyof T2_2>(key: K_11, v: T2_2[K_11]) => void;
|
|
115
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
116
|
-
hasParentCtx: () => boolean;
|
|
117
|
-
getParentCtx: <T2_3 = T2_2>() => {
|
|
118
|
-
getCtx: () => T2_3;
|
|
119
|
-
store: <K_12 extends keyof T2_3>(key: K_12) => {
|
|
120
|
-
value: T2_3[K_12];
|
|
121
|
-
hook: <K2_24 extends keyof Required<T2_3>[K_12]>(key2: K2_24) => {
|
|
122
|
-
value: Required<T2_3>[K_12][K2_24];
|
|
123
|
-
isDefined: boolean;
|
|
124
|
-
};
|
|
125
|
-
init: <K2_25 extends keyof Required<T2_3>[K_12]>(key2: K2_25, getter: () => Required<Required<T2_3>[K_12]>[K2_25]) => Required<Required<T2_3>[K_12]>[K2_25];
|
|
126
|
-
set: <K2_26 extends keyof Required<T2_3>[K_12]>(key2: K2_26, v: Required<T2_3[K_12]>[K2_26]) => Required<T2_3[K_12]>[K2_26];
|
|
127
|
-
get: <K2_27 extends keyof Required<T2_3>[K_12]>(key2: K2_27) => Required<T2_3>[K_12][K2_27] | undefined;
|
|
128
|
-
has: <K2_28 extends keyof Required<T2_3>[K_12]>(key2: K2_28) => boolean;
|
|
129
|
-
del: <K2_29 extends keyof Required<T2_3>[K_12]>(key2: K2_29) => void;
|
|
130
|
-
entries: () => [string, unknown][];
|
|
131
|
-
clear: () => void;
|
|
132
|
-
};
|
|
133
|
-
getStore: <K_13 extends keyof T2_3>(key: K_13) => T2_3[K_13];
|
|
134
|
-
setStore: <K_14 extends keyof T2_3>(key: K_14, v: T2_3[K_14]) => void;
|
|
135
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
136
|
-
hasParentCtx: () => boolean;
|
|
137
|
-
getParentCtx: <T2_4 = T2_3>() => {
|
|
138
|
-
getCtx: () => T2_4;
|
|
139
|
-
store: <K_15 extends keyof T2_4>(key: K_15) => {
|
|
140
|
-
value: T2_4[K_15];
|
|
141
|
-
hook: <K2_30 extends keyof Required<T2_4>[K_15]>(key2: K2_30) => {
|
|
142
|
-
value: Required<T2_4>[K_15][K2_30];
|
|
143
|
-
isDefined: boolean;
|
|
144
|
-
};
|
|
145
|
-
init: <K2_31 extends keyof Required<T2_4>[K_15]>(key2: K2_31, getter: () => Required<Required<T2_4>[K_15]>[K2_31]) => Required<Required<T2_4>[K_15]>[K2_31];
|
|
146
|
-
set: <K2_32 extends keyof Required<T2_4>[K_15]>(key2: K2_32, v: Required<T2_4[K_15]>[K2_32]) => Required<T2_4[K_15]>[K2_32];
|
|
147
|
-
get: <K2_33 extends keyof Required<T2_4>[K_15]>(key2: K2_33) => Required<T2_4>[K_15][K2_33] | undefined;
|
|
148
|
-
has: <K2_34 extends keyof Required<T2_4>[K_15]>(key2: K2_34) => boolean;
|
|
149
|
-
del: <K2_35 extends keyof Required<T2_4>[K_15]>(key2: K2_35) => void;
|
|
150
|
-
entries: () => [string, unknown][];
|
|
151
|
-
clear: () => void;
|
|
152
|
-
};
|
|
153
|
-
getStore: <K_16 extends keyof T2_4>(key: K_16) => T2_4[K_16];
|
|
154
|
-
setStore: <K_17 extends keyof T2_4>(key: K_17, v: T2_4[K_17]) => void;
|
|
155
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
156
|
-
hasParentCtx: () => boolean;
|
|
157
|
-
getParentCtx: <T2_5 = T2_4>() => {
|
|
158
|
-
getCtx: () => T2_5;
|
|
159
|
-
store: <K_18 extends keyof T2_5>(key: K_18) => {
|
|
160
|
-
value: T2_5[K_18];
|
|
161
|
-
hook: <K2_36 extends keyof Required<T2_5>[K_18]>(key2: K2_36) => {
|
|
162
|
-
value: Required<T2_5>[K_18][K2_36];
|
|
163
|
-
isDefined: boolean;
|
|
164
|
-
};
|
|
165
|
-
init: <K2_37 extends keyof Required<T2_5>[K_18]>(key2: K2_37, getter: () => Required<Required<T2_5>[K_18]>[K2_37]) => Required<Required<T2_5>[K_18]>[K2_37];
|
|
166
|
-
set: <K2_38 extends keyof Required<T2_5>[K_18]>(key2: K2_38, v: Required<T2_5[K_18]>[K2_38]) => Required<T2_5[K_18]>[K2_38];
|
|
167
|
-
get: <K2_39 extends keyof Required<T2_5>[K_18]>(key2: K2_39) => Required<T2_5>[K_18][K2_39] | undefined;
|
|
168
|
-
has: <K2_40 extends keyof Required<T2_5>[K_18]>(key2: K2_40) => boolean;
|
|
169
|
-
del: <K2_41 extends keyof Required<T2_5>[K_18]>(key2: K2_41) => void;
|
|
170
|
-
entries: () => [string, unknown][];
|
|
171
|
-
clear: () => void;
|
|
172
|
-
};
|
|
173
|
-
getStore: <K_19 extends keyof T2_5>(key: K_19) => T2_5[K_19];
|
|
174
|
-
setStore: <K_20 extends keyof T2_5>(key: K_20, v: T2_5[K_20]) => void;
|
|
175
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
176
|
-
hasParentCtx: () => boolean;
|
|
177
|
-
getParentCtx: <T2_6 = T2_5>() => {
|
|
178
|
-
getCtx: () => T2_6;
|
|
179
|
-
store: <K_21 extends keyof T2_6>(key: K_21) => {
|
|
180
|
-
value: T2_6[K_21];
|
|
181
|
-
hook: <K2_42 extends keyof Required<T2_6>[K_21]>(key2: K2_42) => {
|
|
182
|
-
value: Required<T2_6>[K_21][K2_42];
|
|
183
|
-
isDefined: boolean;
|
|
184
|
-
};
|
|
185
|
-
init: <K2_43 extends keyof Required<T2_6>[K_21]>(key2: K2_43, getter: () => Required<Required<T2_6>[K_21]>[K2_43]) => Required<Required<T2_6>[K_21]>[K2_43];
|
|
186
|
-
set: <K2_44 extends keyof Required<T2_6>[K_21]>(key2: K2_44, v: Required<T2_6[K_21]>[K2_44]) => Required<T2_6[K_21]>[K2_44];
|
|
187
|
-
get: <K2_45 extends keyof Required<T2_6>[K_21]>(key2: K2_45) => Required<T2_6>[K_21][K2_45] | undefined;
|
|
188
|
-
has: <K2_46 extends keyof Required<T2_6>[K_21]>(key2: K2_46) => boolean;
|
|
189
|
-
del: <K2_47 extends keyof Required<T2_6>[K_21]>(key2: K2_47) => void;
|
|
190
|
-
entries: () => [string, unknown][];
|
|
191
|
-
clear: () => void;
|
|
192
|
-
};
|
|
193
|
-
getStore: <K_22 extends keyof T2_6>(key: K_22) => T2_6[K_22];
|
|
194
|
-
setStore: <K_23 extends keyof T2_6>(key: K_23, v: T2_6[K_23]) => void;
|
|
195
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
196
|
-
hasParentCtx: () => boolean;
|
|
197
|
-
getParentCtx: <T2_7 = T2_6>() => {
|
|
198
|
-
getCtx: () => T2_7;
|
|
199
|
-
store: <K_24 extends keyof T2_7>(key: K_24) => {
|
|
200
|
-
value: T2_7[K_24];
|
|
201
|
-
hook: <K2_48 extends keyof Required<T2_7>[K_24]>(key2: K2_48) => {
|
|
202
|
-
value: Required<T2_7>[K_24][K2_48];
|
|
203
|
-
isDefined: boolean;
|
|
204
|
-
};
|
|
205
|
-
init: <K2_49 extends keyof Required<T2_7>[K_24]>(key2: K2_49, getter: () => Required<Required<T2_7>[K_24]>[K2_49]) => Required<Required<T2_7>[K_24]>[K2_49];
|
|
206
|
-
set: <K2_50 extends keyof Required<T2_7>[K_24]>(key2: K2_50, v: Required<T2_7[K_24]>[K2_50]) => Required<T2_7[K_24]>[K2_50];
|
|
207
|
-
get: <K2_51 extends keyof Required<T2_7>[K_24]>(key2: K2_51) => Required<T2_7>[K_24][K2_51] | undefined;
|
|
208
|
-
has: <K2_52 extends keyof Required<T2_7>[K_24]>(key2: K2_52) => boolean;
|
|
209
|
-
del: <K2_53 extends keyof Required<T2_7>[K_24]>(key2: K2_53) => void;
|
|
210
|
-
entries: () => [string, unknown][];
|
|
211
|
-
clear: () => void;
|
|
212
|
-
};
|
|
213
|
-
getStore: <K_25 extends keyof T2_7>(key: K_25) => T2_7[K_25];
|
|
214
|
-
setStore: <K_26 extends keyof T2_7>(key: K_26, v: T2_7[K_26]) => void;
|
|
215
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
216
|
-
hasParentCtx: () => boolean;
|
|
217
|
-
getParentCtx: <T2_8 = T2_7>() => {
|
|
218
|
-
getCtx: () => T2_8;
|
|
219
|
-
store: <K_27 extends keyof T2_8>(key: K_27) => {
|
|
220
|
-
value: T2_8[K_27];
|
|
221
|
-
hook: <K2_54 extends keyof Required<T2_8>[K_27]>(key2: K2_54) => {
|
|
222
|
-
value: Required<T2_8>[K_27][K2_54];
|
|
223
|
-
isDefined: boolean;
|
|
224
|
-
};
|
|
225
|
-
init: <K2_55 extends keyof Required<T2_8>[K_27]>(key2: K2_55, getter: () => Required<Required<T2_8>[K_27]>[K2_55]) => Required<Required<T2_8>[K_27]>[K2_55];
|
|
226
|
-
set: <K2_56 extends keyof Required<T2_8>[K_27]>(key2: K2_56, v: Required<T2_8[K_27]>[K2_56]) => Required<T2_8[K_27]>[K2_56];
|
|
227
|
-
get: <K2_57 extends keyof Required<T2_8>[K_27]>(key2: K2_57) => Required<T2_8>[K_27][K2_57] | undefined;
|
|
228
|
-
has: <K2_58 extends keyof Required<T2_8>[K_27]>(key2: K2_58) => boolean;
|
|
229
|
-
del: <K2_59 extends keyof Required<T2_8>[K_27]>(key2: K2_59) => void;
|
|
230
|
-
entries: () => [string, unknown][];
|
|
231
|
-
clear: () => void;
|
|
232
|
-
};
|
|
233
|
-
getStore: <K_28 extends keyof T2_8>(key: K_28) => T2_8[K_28];
|
|
234
|
-
setStore: <K_29 extends keyof T2_8>(key: K_29, v: T2_8[K_29]) => void;
|
|
235
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
236
|
-
hasParentCtx: () => boolean;
|
|
237
|
-
getParentCtx: <T2_9 = T2_8>() => {
|
|
238
|
-
getCtx: () => T2_9;
|
|
239
|
-
store: <K_30 extends keyof T2_9>(key: K_30) => {
|
|
240
|
-
value: T2_9[K_30];
|
|
241
|
-
hook: <K2_60 extends keyof Required<T2_9>[K_30]>(key2: K2_60) => {
|
|
242
|
-
value: Required<T2_9>[K_30][K2_60];
|
|
243
|
-
isDefined: boolean;
|
|
244
|
-
};
|
|
245
|
-
init: <K2_61 extends keyof Required<T2_9>[K_30]>(key2: K2_61, getter: () => Required<Required<T2_9>[K_30]>[K2_61]) => Required<Required<T2_9>[K_30]>[K2_61];
|
|
246
|
-
set: <K2_62 extends keyof Required<T2_9>[K_30]>(key2: K2_62, v: Required<T2_9[K_30]>[K2_62]) => Required<T2_9[K_30]>[K2_62];
|
|
247
|
-
get: <K2_63 extends keyof Required<T2_9>[K_30]>(key2: K2_63) => Required<T2_9>[K_30][K2_63] | undefined;
|
|
248
|
-
has: <K2_64 extends keyof Required<T2_9>[K_30]>(key2: K2_64) => boolean;
|
|
249
|
-
del: <K2_65 extends keyof Required<T2_9>[K_30]>(key2: K2_65) => void;
|
|
250
|
-
entries: () => [string, unknown][];
|
|
251
|
-
clear: () => void;
|
|
252
|
-
};
|
|
253
|
-
getStore: <K_31 extends keyof T2_9>(key: K_31) => T2_9[K_31];
|
|
254
|
-
setStore: <K_32 extends keyof T2_9>(key: K_32, v: T2_9[K_32]) => void;
|
|
255
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
256
|
-
hasParentCtx: () => boolean;
|
|
257
|
-
getParentCtx: <T2_10 = T2_9>() => any;
|
|
258
|
-
};
|
|
259
|
-
};
|
|
260
|
-
};
|
|
261
|
-
};
|
|
262
|
-
};
|
|
263
|
-
};
|
|
264
|
-
};
|
|
265
|
-
};
|
|
266
|
-
};
|
|
267
|
-
};
|
|
268
|
-
};
|
|
32
|
+
declare function useWFContext<T extends TEmpty>(): _wooksjs_event_core.TCtxHelpers<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>;
|
|
269
33
|
|
|
270
34
|
declare class WooksWorkflow<T, IR> extends Workflow<T, IR> {
|
|
271
35
|
protected wooks: Wooks;
|
|
@@ -295,8 +59,8 @@ declare class WooksWf<T = any, IR = any> extends WooksAdapterBase {
|
|
|
295
59
|
step<I = any>(id: string, opts: {
|
|
296
60
|
input?: I;
|
|
297
61
|
handler: string | TStepHandler<T, I, IR>;
|
|
298
|
-
}):
|
|
299
|
-
flow(id: string, schema: TWorkflowSchema<T>, prefix?: string, init?: () => void | Promise<void>):
|
|
62
|
+
}): wooks.TProstoRouterPathHandle<Record<string, string | string[]>>;
|
|
63
|
+
flow(id: string, schema: TWorkflowSchema<T>, prefix?: string, init?: () => void | Promise<void>): wooks.TProstoRouterPathHandle<Record<string, string | string[]>>;
|
|
300
64
|
start<I>(schemaId: string, inputContext: T, input?: I, spy?: TWorkflowSpy<T, I, IR>, cleanup?: () => void): Promise<TFlowOutput<T, I, IR>>;
|
|
301
65
|
resume<I>(state: {
|
|
302
66
|
schemaId: string;
|
|
@@ -307,12 +71,6 @@ declare class WooksWf<T = any, IR = any> extends WooksAdapterBase {
|
|
|
307
71
|
protected onError(e: Error): void;
|
|
308
72
|
protected error(e: string | Error): void;
|
|
309
73
|
}
|
|
310
|
-
/**
|
|
311
|
-
* Factory for WooksWf App
|
|
312
|
-
* @param opts TWooksWfOptions
|
|
313
|
-
* @param wooks Wooks | WooksAdapterBase
|
|
314
|
-
* @returns WooksWf
|
|
315
|
-
*/
|
|
316
74
|
declare function createWfApp<T>(opts?: TWooksWfOptions, wooks?: Wooks | WooksAdapterBase): WooksWf<T, any>;
|
|
317
75
|
|
|
318
76
|
export { type TWFContextStore, type TWFEventData, type TWooksWfOptions, WooksWf, createWfApp, createWfContext, resumeWfContext, useWFContext, useWfState, wfShortcuts };
|
package/dist/index.mjs
CHANGED
|
@@ -1,184 +1,179 @@
|
|
|
1
|
-
import { createAsyncEventContext, useAsyncEventContext } from
|
|
2
|
-
import { Workflow, createStep } from
|
|
3
|
-
import { WooksAdapterBase } from
|
|
1
|
+
import { createAsyncEventContext, useAsyncEventContext } from "@wooksjs/event-core";
|
|
2
|
+
import { Workflow, createStep } from "@prostojs/wf";
|
|
3
|
+
import { WooksAdapterBase } from "wooks";
|
|
4
4
|
|
|
5
|
+
//#region packages/event-wf/src/event-wf.ts
|
|
5
6
|
function createWfContext(data, options) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
return createAsyncEventContext({
|
|
8
|
+
event: {
|
|
9
|
+
...data,
|
|
10
|
+
type: "WF"
|
|
11
|
+
},
|
|
12
|
+
resume: false,
|
|
13
|
+
options
|
|
14
|
+
});
|
|
14
15
|
}
|
|
15
16
|
function resumeWfContext(data, options) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
return createAsyncEventContext({
|
|
18
|
+
event: {
|
|
19
|
+
...data,
|
|
20
|
+
type: "WF"
|
|
21
|
+
},
|
|
22
|
+
resume: true,
|
|
23
|
+
options
|
|
24
|
+
});
|
|
24
25
|
}
|
|
25
26
|
function useWFContext() {
|
|
26
|
-
|
|
27
|
+
return useAsyncEventContext("WF");
|
|
27
28
|
}
|
|
28
29
|
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region packages/event-wf/src/composables/wf-state.ts
|
|
29
32
|
function useWfState() {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
const { store, getCtx } = useWFContext();
|
|
34
|
+
const event = store("event");
|
|
35
|
+
return {
|
|
36
|
+
ctx: () => event.get("inputContext"),
|
|
37
|
+
input: () => event.get("input"),
|
|
38
|
+
schemaId: event.get("schemaId"),
|
|
39
|
+
stepId: () => event.get("stepId"),
|
|
40
|
+
indexes: () => event.get("indexes"),
|
|
41
|
+
resume: getCtx().resume
|
|
42
|
+
};
|
|
40
43
|
}
|
|
41
44
|
|
|
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
|
-
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region packages/event-wf/src/workflow.ts
|
|
47
|
+
var WooksWorkflow = class extends Workflow {
|
|
48
|
+
constructor(wooks) {
|
|
49
|
+
super([]);
|
|
50
|
+
this.wooks = wooks;
|
|
51
|
+
}
|
|
52
|
+
resolveStep(stepId) {
|
|
53
|
+
const stepIdNorm = `/${stepId}`.replace(/\/{2,}/gu, "/");
|
|
54
|
+
try {
|
|
55
|
+
const store = useWFContext().store("event");
|
|
56
|
+
const found = this.wooks.lookup("WF_STEP", stepIdNorm);
|
|
57
|
+
if (found.handlers?.length) {
|
|
58
|
+
store.set("stepId", stepIdNorm);
|
|
59
|
+
return found.handlers[0]();
|
|
60
|
+
}
|
|
61
|
+
} catch (error) {
|
|
62
|
+
const router = this.wooks.getRouter();
|
|
63
|
+
const found = router.lookup("WF_STEP", stepIdNorm);
|
|
64
|
+
if (found?.route?.handlers.length) return found.route.handlers[0]();
|
|
65
|
+
}
|
|
66
|
+
throw new Error(`Step "${stepIdNorm}" not found.`);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
67
69
|
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region packages/event-wf/src/wf-adapter.ts
|
|
68
72
|
const wfShortcuts = {
|
|
69
|
-
|
|
70
|
-
|
|
73
|
+
flow: "WF_FLOW",
|
|
74
|
+
step: "WF_STEP"
|
|
75
|
+
};
|
|
76
|
+
var WooksWf = class extends WooksAdapterBase {
|
|
77
|
+
logger;
|
|
78
|
+
wf;
|
|
79
|
+
constructor(opts, wooks) {
|
|
80
|
+
super(wooks, opts?.logger, opts?.router);
|
|
81
|
+
this.opts = opts;
|
|
82
|
+
this.logger = opts?.logger || this.getLogger(`${"\x1B[96m"}[wooks-wf]`);
|
|
83
|
+
this.wf = new WooksWorkflow(this.wooks);
|
|
84
|
+
}
|
|
85
|
+
attachSpy(fn) {
|
|
86
|
+
return this.wf.attachSpy(fn);
|
|
87
|
+
}
|
|
88
|
+
detachSpy(fn) {
|
|
89
|
+
this.wf.detachSpy(fn);
|
|
90
|
+
}
|
|
91
|
+
step(id, opts) {
|
|
92
|
+
const step = createStep(id, opts);
|
|
93
|
+
return this.on("WF_STEP", id, () => step);
|
|
94
|
+
}
|
|
95
|
+
flow(id, schema, prefix, init) {
|
|
96
|
+
this.wf.register(id, schema, prefix);
|
|
97
|
+
return this.on("WF_FLOW", id, () => ({
|
|
98
|
+
init,
|
|
99
|
+
id
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
start(schemaId, inputContext, input, spy, cleanup) {
|
|
103
|
+
return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
|
|
104
|
+
}
|
|
105
|
+
resume(state, input, spy, cleanup) {
|
|
106
|
+
return this._start(state.schemaId, state.context, state.indexes, input, spy, cleanup);
|
|
107
|
+
}
|
|
108
|
+
async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
|
|
109
|
+
const resume = !!indexes?.length;
|
|
110
|
+
const runInContext = (resume ? resumeWfContext : createWfContext)({
|
|
111
|
+
inputContext,
|
|
112
|
+
schemaId,
|
|
113
|
+
stepId: null,
|
|
114
|
+
indexes,
|
|
115
|
+
input
|
|
116
|
+
}, this.mergeEventOptions(this.opts?.eventOptions));
|
|
117
|
+
return runInContext(async () => {
|
|
118
|
+
const { handlers: foundHandlers } = this.wooks.lookup("WF_FLOW", `/${schemaId}`.replace(/^\/+/u, "/"));
|
|
119
|
+
const handlers = foundHandlers || this.opts?.onNotFound && [this.opts.onNotFound] || null;
|
|
120
|
+
if (handlers && handlers.length > 0) {
|
|
121
|
+
let result = {};
|
|
122
|
+
let firstStep = true;
|
|
123
|
+
const _spy = (...args) => {
|
|
124
|
+
if (spy) spy(...args);
|
|
125
|
+
if (firstStep && args[0] === "step") {
|
|
126
|
+
firstStep = false;
|
|
127
|
+
const { store } = useWFContext();
|
|
128
|
+
store("event").set("input", undefined);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
try {
|
|
132
|
+
for (const handler of handlers) {
|
|
133
|
+
const { id, init } = await handler();
|
|
134
|
+
if (init) await init();
|
|
135
|
+
if (resume) {
|
|
136
|
+
result = await this.wf.resume({
|
|
137
|
+
schemaId: id,
|
|
138
|
+
context: inputContext,
|
|
139
|
+
indexes
|
|
140
|
+
}, input, _spy);
|
|
141
|
+
break;
|
|
142
|
+
} else {
|
|
143
|
+
result = await this.wf.start(id, inputContext, input, _spy);
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
} catch (error) {
|
|
148
|
+
clean();
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
clean();
|
|
152
|
+
if (result.resume) result.resume = (_input) => this.resume(result.state, _input, spy, cleanup);
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
clean();
|
|
156
|
+
throw new Error(`Unknown schemaId: ${schemaId}`);
|
|
157
|
+
function clean() {
|
|
158
|
+
if (cleanup) cleanup();
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
onError(e) {
|
|
163
|
+
if (this.opts?.onError) this.opts.onError(e);
|
|
164
|
+
else {
|
|
165
|
+
this.error(e.message);
|
|
166
|
+
process.exit(1);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
error(e) {
|
|
170
|
+
if (typeof e === "string") console.error(`${"\x1B[31m"}ERROR: ${"\x1B[0m"}${e}`);
|
|
171
|
+
else console.error(`${"\x1B[31m"}ERROR: ${"\x1B[0m"}${e.message}`);
|
|
172
|
+
}
|
|
71
173
|
};
|
|
72
|
-
class WooksWf extends WooksAdapterBase {
|
|
73
|
-
constructor(opts, wooks) {
|
|
74
|
-
super(wooks, opts?.logger, opts?.router);
|
|
75
|
-
this.opts = opts;
|
|
76
|
-
this.logger = opts?.logger || this.getLogger(`${'[96m'}[wooks-wf]`);
|
|
77
|
-
this.wf = new WooksWorkflow(this.wooks);
|
|
78
|
-
}
|
|
79
|
-
attachSpy(fn) {
|
|
80
|
-
return this.wf.attachSpy(fn);
|
|
81
|
-
}
|
|
82
|
-
detachSpy(fn) {
|
|
83
|
-
this.wf.detachSpy(fn);
|
|
84
|
-
}
|
|
85
|
-
step(id, opts) {
|
|
86
|
-
const step = createStep(id, opts);
|
|
87
|
-
return this.on('WF_STEP', id, () => step);
|
|
88
|
-
}
|
|
89
|
-
flow(id, schema, prefix, init) {
|
|
90
|
-
this.wf.register(id, schema, prefix);
|
|
91
|
-
return this.on('WF_FLOW', id, () => ({
|
|
92
|
-
init,
|
|
93
|
-
id,
|
|
94
|
-
}));
|
|
95
|
-
}
|
|
96
|
-
start(schemaId, inputContext, input, spy, cleanup) {
|
|
97
|
-
return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
|
|
98
|
-
}
|
|
99
|
-
resume(state, input, spy, cleanup) {
|
|
100
|
-
return this._start(state.schemaId, state.context, state.indexes, input, spy, cleanup);
|
|
101
|
-
}
|
|
102
|
-
async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
|
|
103
|
-
const resume = !!indexes?.length;
|
|
104
|
-
const runInContext = (resume ? resumeWfContext : createWfContext)({
|
|
105
|
-
inputContext,
|
|
106
|
-
schemaId,
|
|
107
|
-
stepId: null,
|
|
108
|
-
indexes,
|
|
109
|
-
input,
|
|
110
|
-
}, this.mergeEventOptions(this.opts?.eventOptions));
|
|
111
|
-
return runInContext(async () => {
|
|
112
|
-
const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', `/${schemaId}`.replace(/^\/+/, '/'));
|
|
113
|
-
const handlers = foundHandlers || (this.opts?.onNotFound && [this.opts.onNotFound]) || null;
|
|
114
|
-
if (handlers && handlers.length > 0) {
|
|
115
|
-
let result = {};
|
|
116
|
-
let firstStep = true;
|
|
117
|
-
const _spy = (...args) => {
|
|
118
|
-
if (spy) {
|
|
119
|
-
spy(...args);
|
|
120
|
-
}
|
|
121
|
-
if (firstStep && args[0] === 'step') {
|
|
122
|
-
firstStep = false;
|
|
123
|
-
const { store } = useWFContext();
|
|
124
|
-
store('event').set('input', undefined);
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
try {
|
|
128
|
-
for (const handler of handlers) {
|
|
129
|
-
const { id, init } = (await handler());
|
|
130
|
-
if (init) {
|
|
131
|
-
await init();
|
|
132
|
-
}
|
|
133
|
-
if (resume) {
|
|
134
|
-
result = await this.wf.resume({ schemaId: id, context: inputContext, indexes }, input, _spy);
|
|
135
|
-
break;
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
result = await this.wf.start(id, inputContext, input, _spy);
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
catch (error) {
|
|
144
|
-
clean();
|
|
145
|
-
throw error;
|
|
146
|
-
}
|
|
147
|
-
clean();
|
|
148
|
-
if (result.resume) {
|
|
149
|
-
result.resume = (_input) => this.resume(result.state, _input, spy, cleanup);
|
|
150
|
-
}
|
|
151
|
-
return result;
|
|
152
|
-
}
|
|
153
|
-
clean();
|
|
154
|
-
throw new Error(`Unknown schemaId: ${schemaId}`);
|
|
155
|
-
function clean() {
|
|
156
|
-
if (cleanup) {
|
|
157
|
-
cleanup();
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
onError(e) {
|
|
163
|
-
if (this.opts?.onError) {
|
|
164
|
-
this.opts.onError(e);
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
this.error(e.message);
|
|
168
|
-
process.exit(1);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
error(e) {
|
|
172
|
-
if (typeof e === 'string') {
|
|
173
|
-
console.error(`${'[31m'}ERROR: ${'[0m'}${e}`);
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
console.error(`${'[31m'}ERROR: ${'[0m'}${e.message}`);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
174
|
function createWfApp(opts, wooks) {
|
|
181
|
-
|
|
175
|
+
return new WooksWf(opts, wooks);
|
|
182
176
|
}
|
|
183
177
|
|
|
184
|
-
|
|
178
|
+
//#endregion
|
|
179
|
+
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.5.
|
|
3
|
+
"version": "0.5.25",
|
|
4
4
|
"description": "@wooksjs/event-wf",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"author": "Artem Maltsev",
|
|
34
34
|
"license": "MIT",
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"
|
|
37
|
-
"
|
|
36
|
+
"@wooksjs/event-core": "^0.5.25",
|
|
37
|
+
"wooks": "^0.5.25"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@prostojs/logger": "^0.4.3",
|
|
@@ -43,5 +43,12 @@
|
|
|
43
43
|
"bugs": {
|
|
44
44
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
45
45
|
},
|
|
46
|
-
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-wf#readme"
|
|
47
|
-
|
|
46
|
+
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-wf#readme",
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"typescript": "^5.7.3",
|
|
49
|
+
"vitest": "^2.1.8"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "rolldown -c ../../rolldown.config.mjs"
|
|
53
|
+
}
|
|
54
|
+
}
|