@wooksjs/event-wf 0.3.9 → 0.3.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 CHANGED
@@ -98,13 +98,13 @@ class WooksWf extends wooks.WooksAdapterBase {
98
98
  this.wf.register(id, schema);
99
99
  return this.on('WF_FLOW', id, () => ({ init, id }));
100
100
  }
101
- start(schemaId, inputContext, input) {
102
- return this._start(schemaId, inputContext, undefined, input);
101
+ start(schemaId, inputContext, input, cleanup) {
102
+ return this._start(schemaId, inputContext, undefined, input, cleanup);
103
103
  }
104
- resume(schemaId, inputContext, indexes, input) {
105
- return this._start(schemaId, inputContext, indexes, input);
104
+ resume(schemaId, inputContext, indexes, input, cleanup) {
105
+ return this._start(schemaId, inputContext, indexes, input, cleanup);
106
106
  }
107
- _start(schemaId, inputContext, indexes, input) {
107
+ _start(schemaId, inputContext, indexes, input, cleanup) {
108
108
  var _a, _b;
109
109
  return __awaiter(this, void 0, void 0, function* () {
110
110
  const resume = !!(indexes === null || indexes === void 0 ? void 0 : indexes.length);
@@ -120,25 +120,39 @@ class WooksWf extends wooks.WooksAdapterBase {
120
120
  null;
121
121
  if (handlers && handlers.length) {
122
122
  let result = {};
123
- for (const handler of handlers) {
124
- restoreCtx();
125
- const { id, init } = (yield handler());
126
- if (init) {
127
- yield init();
123
+ try {
124
+ for (const handler of handlers) {
128
125
  restoreCtx();
126
+ const { id, init } = (yield handler());
127
+ if (init) {
128
+ yield init();
129
+ restoreCtx();
130
+ }
131
+ if (resume) {
132
+ result = yield this.wf.resume(id, { context: inputContext, indexes }, input);
133
+ break;
134
+ }
135
+ else {
136
+ result = yield this.wf.start(id, inputContext, input);
137
+ break;
138
+ }
129
139
  }
130
- if (resume) {
131
- result = yield this.wf.resume(id, { context: inputContext, indexes }, input);
132
- break;
133
- }
134
- else {
135
- result = yield this.wf.start(id, inputContext, input);
136
- break;
137
- }
138
140
  }
141
+ catch (e) {
142
+ clean();
143
+ throw e;
144
+ }
145
+ clean();
139
146
  clearCtx();
140
147
  return result;
141
148
  }
149
+ clean();
150
+ function clean() {
151
+ if (cleanup) {
152
+ restoreCtx();
153
+ cleanup();
154
+ }
155
+ }
142
156
  clearCtx();
143
157
  throw new Error('Unknown schemaId: ' + schemaId);
144
158
  });
package/dist/index.d.ts CHANGED
@@ -140,9 +140,9 @@ export declare class WooksWf<T> extends WooksAdapterBase {
140
140
  handler: string | TStepHandler<T, I, D>;
141
141
  }): TProstoRouterPathHandle<Record<string, string | string[]>>;
142
142
  flow(id: string, schema: TWorkflowSchema<T>, init?: () => void | Promise<void>): TProstoRouterPathHandle<Record<string, string | string[]>>;
143
- start<I>(schemaId: string, inputContext: T, input?: I): Promise<TFlowOutput<T, I>>;
144
- resume<I>(schemaId: string, inputContext: T, indexes: number[], input?: I): Promise<TFlowOutput<T, I>>;
145
- protected _start<I>(schemaId: string, inputContext: T, indexes?: number[], input?: I): Promise<TFlowOutput<T, I>>;
143
+ start<I>(schemaId: string, inputContext: T, input?: I, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
144
+ resume<I>(schemaId: string, inputContext: T, indexes: number[], input?: I, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
145
+ protected _start<I>(schemaId: string, inputContext: T, indexes?: number[], input?: I, cleanup?: () => void): Promise<TFlowOutput<T, I>>;
146
146
  protected onError(e: Error): void;
147
147
  protected error(e: string | Error): void;
148
148
  }
package/dist/index.mjs CHANGED
@@ -96,13 +96,13 @@ class WooksWf extends WooksAdapterBase {
96
96
  this.wf.register(id, schema);
97
97
  return this.on('WF_FLOW', id, () => ({ init, id }));
98
98
  }
99
- start(schemaId, inputContext, input) {
100
- return this._start(schemaId, inputContext, undefined, input);
99
+ start(schemaId, inputContext, input, cleanup) {
100
+ return this._start(schemaId, inputContext, undefined, input, cleanup);
101
101
  }
102
- resume(schemaId, inputContext, indexes, input) {
103
- return this._start(schemaId, inputContext, indexes, input);
102
+ resume(schemaId, inputContext, indexes, input, cleanup) {
103
+ return this._start(schemaId, inputContext, indexes, input, cleanup);
104
104
  }
105
- _start(schemaId, inputContext, indexes, input) {
105
+ _start(schemaId, inputContext, indexes, input, cleanup) {
106
106
  var _a, _b;
107
107
  return __awaiter(this, void 0, void 0, function* () {
108
108
  const resume = !!(indexes === null || indexes === void 0 ? void 0 : indexes.length);
@@ -118,25 +118,39 @@ class WooksWf extends WooksAdapterBase {
118
118
  null;
119
119
  if (handlers && handlers.length) {
120
120
  let result = {};
121
- for (const handler of handlers) {
122
- restoreCtx();
123
- const { id, init } = (yield handler());
124
- if (init) {
125
- yield init();
121
+ try {
122
+ for (const handler of handlers) {
126
123
  restoreCtx();
124
+ const { id, init } = (yield handler());
125
+ if (init) {
126
+ yield init();
127
+ restoreCtx();
128
+ }
129
+ if (resume) {
130
+ result = yield this.wf.resume(id, { context: inputContext, indexes }, input);
131
+ break;
132
+ }
133
+ else {
134
+ result = yield this.wf.start(id, inputContext, input);
135
+ break;
136
+ }
127
137
  }
128
- if (resume) {
129
- result = yield this.wf.resume(id, { context: inputContext, indexes }, input);
130
- break;
131
- }
132
- else {
133
- result = yield this.wf.start(id, inputContext, input);
134
- break;
135
- }
136
138
  }
139
+ catch (e) {
140
+ clean();
141
+ throw e;
142
+ }
143
+ clean();
137
144
  clearCtx();
138
145
  return result;
139
146
  }
147
+ clean();
148
+ function clean() {
149
+ if (cleanup) {
150
+ restoreCtx();
151
+ cleanup();
152
+ }
153
+ }
140
154
  clearCtx();
141
155
  throw new Error('Unknown schemaId: ' + schemaId);
142
156
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-wf",
3
- "version": "0.3.9",
3
+ "version": "0.3.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.3.9",
29
- "@wooksjs/event-core": "0.3.9"
28
+ "wooks": "0.3.11",
29
+ "@wooksjs/event-core": "0.3.11"
30
30
  },
31
31
  "dependencies": {
32
32
  "@prostojs/logger": "^0.3.6",