@wooksjs/event-wf 0.4.25 → 0.4.27

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/README.md CHANGED
@@ -78,4 +78,4 @@ Contributions to the `@wooksjs/event-wf` project are welcome. If you find any bu
78
78
 
79
79
  ## License
80
80
 
81
- `@wooksjs/event-wf` is licensed under the [MIT license](https://github.com/wooksjs/wooksjs/blob/main/LICENSE).
81
+ `@wooksjs/event-wf` is licensed under the [MIT license](https://github.com/wooksjs/wooksjs/blob/main/LICENSE).
package/dist/index.cjs CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var eventCore = require('@wooksjs/event-core');
4
- var wooks = require('wooks');
5
4
  var wf = require('@prostojs/wf');
5
+ var wooks = require('wooks');
6
6
 
7
7
  function createWfContext(data, options) {
8
8
  return eventCore.createEventContext({
@@ -28,24 +28,36 @@ function useWFContext() {
28
28
  return eventCore.useEventContext('WF');
29
29
  }
30
30
 
31
+ function useWfState() {
32
+ const { store, getCtx } = useWFContext();
33
+ const event = store('event');
34
+ return {
35
+ ctx: () => event.get('inputContext'),
36
+ input: () => event.get('input'),
37
+ schemaId: event.get('schemaId'),
38
+ indexes: () => event.get('indexes'),
39
+ resume: getCtx().resume,
40
+ };
41
+ }
42
+
31
43
  class WooksWorkflow extends wf.Workflow {
32
44
  constructor(wooks) {
33
45
  super([]);
34
46
  this.wooks = wooks;
35
47
  }
36
48
  resolveStep(stepId) {
37
- const stepIdNorm = ('/' + stepId).replace(/\/\/+/g, '/');
49
+ const stepIdNorm = `/${stepId}`.replace(/\/\/+/g, '/');
38
50
  try {
39
51
  useWFContext();
40
52
  const found = this.wooks.lookup('WF_STEP', stepIdNorm);
41
- if (found?.handlers?.length) {
53
+ if (found.handlers?.length) {
42
54
  return found.handlers[0]();
43
55
  }
44
56
  }
45
- catch (e) {
57
+ catch (error) {
46
58
  const router = this.wooks.getRouter();
47
59
  const found = router.lookup('WF_STEP', stepIdNorm);
48
- if (found?.route?.handlers?.length) {
60
+ if (found?.route?.handlers.length) {
49
61
  return found.route.handlers[0]();
50
62
  }
51
63
  }
@@ -68,7 +80,7 @@ class WooksWf extends wooks.WooksAdapterBase {
68
80
  return this.wf.attachSpy(fn);
69
81
  }
70
82
  detachSpy(fn) {
71
- return this.wf.detachSpy(fn);
83
+ this.wf.detachSpy(fn);
72
84
  }
73
85
  step(id, opts) {
74
86
  const step = wf.createStep(id, opts);
@@ -76,27 +88,28 @@ class WooksWf extends wooks.WooksAdapterBase {
76
88
  }
77
89
  flow(id, schema, prefix, init) {
78
90
  this.wf.register(id, schema, prefix);
79
- return this.on('WF_FLOW', id, () => ({ init, id }));
91
+ return this.on('WF_FLOW', id, () => ({
92
+ init,
93
+ id,
94
+ }));
80
95
  }
81
- start(schemaId, inputContext, input, spy, cleanup) {
96
+ async start(schemaId, inputContext, input, spy, cleanup) {
82
97
  return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
83
98
  }
84
- resume(state, input, spy, cleanup) {
99
+ async resume(state, input, spy, cleanup) {
85
100
  return this._start(state.schemaId, state.context, state.indexes, input, spy, cleanup);
86
101
  }
87
102
  async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
88
- const resume = !!indexes?.length;
103
+ const resume = Boolean(indexes?.length);
89
104
  const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
90
105
  inputContext,
91
106
  schemaId,
92
107
  indexes,
93
108
  input,
94
109
  }, this.mergeEventOptions(this.opts?.eventOptions));
95
- const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
96
- const handlers = foundHandlers ||
97
- (this.opts?.onNotFound && [this.opts.onNotFound]) ||
98
- null;
99
- if (handlers && handlers.length) {
110
+ const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', `/${schemaId}`);
111
+ const handlers = foundHandlers || (this.opts?.onNotFound && [this.opts.onNotFound]) || null;
112
+ if (handlers && handlers.length > 0) {
100
113
  let result = {};
101
114
  let firstStep = true;
102
115
  const _spy = (...args) => {
@@ -120,7 +133,7 @@ class WooksWf extends wooks.WooksAdapterBase {
120
133
  }
121
134
  restoreCtx();
122
135
  if (resume) {
123
- result = await this.wf.resume({ schemaId: id, context: inputContext, indexes }, input, _spy);
136
+ result = await this.wf.resume({ schemaId: id, context: inputContext, indexes: indexes }, input, _spy);
124
137
  break;
125
138
  }
126
139
  else {
@@ -129,9 +142,9 @@ class WooksWf extends wooks.WooksAdapterBase {
129
142
  }
130
143
  }
131
144
  }
132
- catch (e) {
145
+ catch (error) {
133
146
  clean();
134
- throw e;
147
+ throw error;
135
148
  }
136
149
  clean();
137
150
  clearCtx();
@@ -145,7 +158,7 @@ class WooksWf extends wooks.WooksAdapterBase {
145
158
  }
146
159
  }
147
160
  clearCtx();
148
- throw new Error('Unknown schemaId: ' + schemaId);
161
+ throw new Error(`Unknown schemaId: ${schemaId}`);
149
162
  }
150
163
  onError(e) {
151
164
  if (this.opts?.onError) {
@@ -158,10 +171,10 @@ class WooksWf extends wooks.WooksAdapterBase {
158
171
  }
159
172
  error(e) {
160
173
  if (typeof e === 'string') {
161
- console.error('' + 'ERROR: ' + '' + e);
174
+ console.error(`${''}ERROR: ${''}${e}`);
162
175
  }
163
176
  else {
164
- console.error('' + 'ERROR: ' + '' + e.message);
177
+ console.error(`${''}ERROR: ${''}${e.message}`);
165
178
  }
166
179
  }
167
180
  }
@@ -169,18 +182,6 @@ function createWfApp(opts, wooks) {
169
182
  return new WooksWf(opts, wooks);
170
183
  }
171
184
 
172
- function useWfState() {
173
- const { store, getCtx } = useWFContext();
174
- const event = store('event');
175
- return {
176
- ctx: () => event.get('inputContext'),
177
- input: () => event.get('input'),
178
- schemaId: event.get('schemaId'),
179
- indexes: () => event.get('indexes'),
180
- resume: getCtx().resume,
181
- };
182
- }
183
-
184
185
  exports.WooksWf = WooksWf;
185
186
  exports.createWfApp = createWfApp;
186
187
  exports.createWfContext = createWfContext;
package/dist/index.d.ts CHANGED
@@ -1,10 +1,18 @@
1
1
  import * as _wooksjs_event_core from '@wooksjs/event-core';
2
2
  import { TEventOptions, TEmpty } from '@wooksjs/event-core';
3
3
  import * as _prostojs_router from '@prostojs/router';
4
- import { Wooks, TWooksHandler, TWooksOptions, WooksAdapterBase } from 'wooks';
5
4
  import { TConsoleBase } from '@prostojs/logger';
6
5
  import { Workflow, Step, TWorkflowSpy, TStepHandler, TWorkflowSchema, TFlowOutput } from '@prostojs/wf';
7
6
  export { TStepHandler, TWorkflowSchema } from '@prostojs/wf';
7
+ import { Wooks, TWooksHandler, TWooksOptions, WooksAdapterBase } from 'wooks';
8
+
9
+ declare function useWfState(): {
10
+ ctx: <T>() => T;
11
+ input: <I>() => I | undefined;
12
+ schemaId: string;
13
+ indexes: () => number[] | undefined;
14
+ resume: boolean;
15
+ };
8
16
 
9
17
  interface TWFEventData {
10
18
  schemaId: string;
@@ -22,7 +30,7 @@ declare function createWfContext(data: Omit<TWFEventData, 'type'>, options: TEve
22
30
  restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
23
31
  clearCtx: () => null;
24
32
  store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K) => {
25
- value: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
33
+ value: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K] | undefined;
26
34
  hook: <K2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
27
35
  value: Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
28
36
  isDefined: boolean;
@@ -43,7 +51,7 @@ declare function resumeWfContext(data: Omit<TWFEventData, 'type'>, options: TEve
43
51
  restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
44
52
  clearCtx: () => null;
45
53
  store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData>>(key: K) => {
46
- value: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
54
+ value: (TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K] | undefined;
47
55
  hook: <K2 extends keyof Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
48
56
  value: Required<TWFContextStore & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
49
57
  isDefined: boolean;
@@ -69,7 +77,7 @@ declare function useWFContext<T extends TEmpty>(): {
69
77
  restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
70
78
  clearCtx: () => null;
71
79
  store: <K extends "resume" | keyof _wooksjs_event_core.TGenericContextStore<TWFEventData> | keyof T>(key: K) => {
72
- value: (TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K];
80
+ value: (TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>)[K] | undefined;
73
81
  hook: <K2 extends keyof Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K]>(key2: K2) => {
74
82
  value: Required<TWFContextStore & T & _wooksjs_event_core.TGenericContextStore<TWFEventData>>[K][K2];
75
83
  isDefined: boolean;
@@ -97,8 +105,8 @@ declare const wfShortcuts: {
97
105
  step: string;
98
106
  };
99
107
  interface TWooksWfOptions {
100
- onError?(e: Error): void;
101
- onNotFound?: TWooksHandler<unknown>;
108
+ onError?: (e: Error) => void;
109
+ onNotFound?: TWooksHandler;
102
110
  onUnknownFlow?: (schemaId: string, raiseError: () => void) => unknown;
103
111
  logger?: TConsoleBase;
104
112
  eventOptions?: TEventOptions;
@@ -134,12 +142,4 @@ declare class WooksWf<T = any, IR = any> extends WooksAdapterBase {
134
142
  */
135
143
  declare function createWfApp<T>(opts?: TWooksWfOptions, wooks?: Wooks | WooksAdapterBase): WooksWf<T, any>;
136
144
 
137
- declare function useWfState(): {
138
- ctx: <T>() => T;
139
- input: <I>() => I | undefined;
140
- schemaId: string;
141
- indexes: () => number[] | undefined;
142
- resume: boolean;
143
- };
144
-
145
145
  export { type TWFContextStore, type TWFEventData, type TWooksWfOptions, WooksWf, createWfApp, createWfContext, resumeWfContext, useWFContext, useWfState, wfShortcuts };
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createEventContext, useEventContext } from '@wooksjs/event-core';
2
- import { WooksAdapterBase } from 'wooks';
3
2
  import { Workflow, createStep } from '@prostojs/wf';
3
+ import { WooksAdapterBase } from 'wooks';
4
4
 
5
5
  function createWfContext(data, options) {
6
6
  return createEventContext({
@@ -26,24 +26,36 @@ function useWFContext() {
26
26
  return useEventContext('WF');
27
27
  }
28
28
 
29
+ function useWfState() {
30
+ const { store, getCtx } = useWFContext();
31
+ const event = store('event');
32
+ return {
33
+ ctx: () => event.get('inputContext'),
34
+ input: () => event.get('input'),
35
+ schemaId: event.get('schemaId'),
36
+ indexes: () => event.get('indexes'),
37
+ resume: getCtx().resume,
38
+ };
39
+ }
40
+
29
41
  class WooksWorkflow extends Workflow {
30
42
  constructor(wooks) {
31
43
  super([]);
32
44
  this.wooks = wooks;
33
45
  }
34
46
  resolveStep(stepId) {
35
- const stepIdNorm = ('/' + stepId).replace(/\/\/+/g, '/');
47
+ const stepIdNorm = `/${stepId}`.replace(/\/\/+/g, '/');
36
48
  try {
37
49
  useWFContext();
38
50
  const found = this.wooks.lookup('WF_STEP', stepIdNorm);
39
- if (found?.handlers?.length) {
51
+ if (found.handlers?.length) {
40
52
  return found.handlers[0]();
41
53
  }
42
54
  }
43
- catch (e) {
55
+ catch (error) {
44
56
  const router = this.wooks.getRouter();
45
57
  const found = router.lookup('WF_STEP', stepIdNorm);
46
- if (found?.route?.handlers?.length) {
58
+ if (found?.route?.handlers.length) {
47
59
  return found.route.handlers[0]();
48
60
  }
49
61
  }
@@ -66,7 +78,7 @@ class WooksWf extends WooksAdapterBase {
66
78
  return this.wf.attachSpy(fn);
67
79
  }
68
80
  detachSpy(fn) {
69
- return this.wf.detachSpy(fn);
81
+ this.wf.detachSpy(fn);
70
82
  }
71
83
  step(id, opts) {
72
84
  const step = createStep(id, opts);
@@ -74,27 +86,28 @@ class WooksWf extends WooksAdapterBase {
74
86
  }
75
87
  flow(id, schema, prefix, init) {
76
88
  this.wf.register(id, schema, prefix);
77
- return this.on('WF_FLOW', id, () => ({ init, id }));
89
+ return this.on('WF_FLOW', id, () => ({
90
+ init,
91
+ id,
92
+ }));
78
93
  }
79
- start(schemaId, inputContext, input, spy, cleanup) {
94
+ async start(schemaId, inputContext, input, spy, cleanup) {
80
95
  return this._start(schemaId, inputContext, undefined, input, spy, cleanup);
81
96
  }
82
- resume(state, input, spy, cleanup) {
97
+ async resume(state, input, spy, cleanup) {
83
98
  return this._start(state.schemaId, state.context, state.indexes, input, spy, cleanup);
84
99
  }
85
100
  async _start(schemaId, inputContext, indexes, input, spy, cleanup) {
86
- const resume = !!indexes?.length;
101
+ const resume = Boolean(indexes?.length);
87
102
  const { restoreCtx, clearCtx } = (resume ? resumeWfContext : createWfContext)({
88
103
  inputContext,
89
104
  schemaId,
90
105
  indexes,
91
106
  input,
92
107
  }, this.mergeEventOptions(this.opts?.eventOptions));
93
- const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', '/' + schemaId);
94
- const handlers = foundHandlers ||
95
- (this.opts?.onNotFound && [this.opts.onNotFound]) ||
96
- null;
97
- if (handlers && handlers.length) {
108
+ const { handlers: foundHandlers } = this.wooks.lookup('WF_FLOW', `/${schemaId}`);
109
+ const handlers = foundHandlers || (this.opts?.onNotFound && [this.opts.onNotFound]) || null;
110
+ if (handlers && handlers.length > 0) {
98
111
  let result = {};
99
112
  let firstStep = true;
100
113
  const _spy = (...args) => {
@@ -118,7 +131,7 @@ class WooksWf extends WooksAdapterBase {
118
131
  }
119
132
  restoreCtx();
120
133
  if (resume) {
121
- result = await this.wf.resume({ schemaId: id, context: inputContext, indexes }, input, _spy);
134
+ result = await this.wf.resume({ schemaId: id, context: inputContext, indexes: indexes }, input, _spy);
122
135
  break;
123
136
  }
124
137
  else {
@@ -127,9 +140,9 @@ class WooksWf extends WooksAdapterBase {
127
140
  }
128
141
  }
129
142
  }
130
- catch (e) {
143
+ catch (error) {
131
144
  clean();
132
- throw e;
145
+ throw error;
133
146
  }
134
147
  clean();
135
148
  clearCtx();
@@ -143,7 +156,7 @@ class WooksWf extends WooksAdapterBase {
143
156
  }
144
157
  }
145
158
  clearCtx();
146
- throw new Error('Unknown schemaId: ' + schemaId);
159
+ throw new Error(`Unknown schemaId: ${schemaId}`);
147
160
  }
148
161
  onError(e) {
149
162
  if (this.opts?.onError) {
@@ -156,10 +169,10 @@ class WooksWf extends WooksAdapterBase {
156
169
  }
157
170
  error(e) {
158
171
  if (typeof e === 'string') {
159
- console.error('' + 'ERROR: ' + '' + e);
172
+ console.error(`${''}ERROR: ${''}${e}`);
160
173
  }
161
174
  else {
162
- console.error('' + 'ERROR: ' + '' + e.message);
175
+ console.error(`${''}ERROR: ${''}${e.message}`);
163
176
  }
164
177
  }
165
178
  }
@@ -167,16 +180,4 @@ function createWfApp(opts, wooks) {
167
180
  return new WooksWf(opts, wooks);
168
181
  }
169
182
 
170
- function useWfState() {
171
- const { store, getCtx } = useWFContext();
172
- const event = store('event');
173
- return {
174
- ctx: () => event.get('inputContext'),
175
- input: () => event.get('input'),
176
- schemaId: event.get('schemaId'),
177
- indexes: () => event.get('indexes'),
178
- resume: getCtx().resume,
179
- };
180
- }
181
-
182
183
  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.25",
3
+ "version": "0.4.27",
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
- "wooks": "0.4.25",
37
- "@wooksjs/event-core": "0.4.25"
36
+ "wooks": "0.4.27",
37
+ "@wooksjs/event-core": "0.4.27"
38
38
  },
39
39
  "dependencies": {
40
40
  "@prostojs/logger": "^0.4.0",