codehooks-js 1.3.9 → 1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codehooks-js",
3
- "version": "1.3.9",
3
+ "version": "1.3.11",
4
4
  "type": "module",
5
5
  "description": "Codehooks.io official library - provides express.JS like syntax",
6
6
  "main": "index.js",
package/types/index.d.ts CHANGED
@@ -1113,7 +1113,11 @@ export type WorkflowEvents = {
1113
1113
  /**
1114
1114
  * Definition of a workflow step function
1115
1115
  */
1116
- export type WorkflowDefinition = Record<string, (state: any, callback: (nextStep: string | string[] | null, newState: any, options?: any) => void) => Promise<void>>;
1116
+ export type WorkflowDefinition = Record<string, (
1117
+ state: any,
1118
+ callback: (nextStep: string | string[] | null, newState: any, options?: any) => void,
1119
+ waiterFunction?: (waiter?: any) => void
1120
+ ) => Promise<void>>;
1117
1121
 
1118
1122
  /**
1119
1123
  * Configuration options for a workflow step
@@ -254,13 +254,25 @@ class Workflow extends EventEmitter {
254
254
  const func = this.getDefinition(stepsName, nextStep);
255
255
 
256
256
  // Wrap the callback in a Promise to ensure proper async handling
257
- await new Promise(async (resolve, reject) => {
257
+ return await new Promise(async (resolve, reject) => {
258
258
  // check if the step count is greater than the max step count
259
259
  if (newState.stepCount[nextStep].visits > this.getMaxStepCount()) {
260
260
  reject(new Error(`Step ${nextStep} has been executed ${newState.stepCount[nextStep].visits} times, which is greater than the maximum step count of ${this.getMaxStepCount()}`));
261
261
  return;
262
262
  }
263
263
  try {
264
+ const waiterFunction = async (state) => {
265
+ if (state) {
266
+ console.debug('waiter', state);
267
+ await connection.updateOne(this.#collectionName,
268
+ { _id: instanceId },
269
+ { $set: { ...state, updatedAt: new Date().toISOString() } });
270
+ resolve();
271
+ return;
272
+ } else {
273
+ resolve(); // no state update
274
+ }
275
+ }
264
276
  await func({...newState, instanceId: newState._id}, async (nextStep, userState, options) => {
265
277
  try {
266
278
  // Protect system-level properties
@@ -370,7 +382,8 @@ class Workflow extends EventEmitter {
370
382
  console.error('error', error.message);
371
383
  reject(error);
372
384
  }
373
- });
385
+ }, waiterFunction);
386
+ //resolve();
374
387
  } catch (error) {
375
388
  console.error('Error executing step function:', error);
376
389
  reject(error);
@@ -432,6 +445,8 @@ class Workflow extends EventEmitter {
432
445
  const { stepsName, goto, state, instanceId, options } = req.body.payload;
433
446
  console.debug('dequeue step', stepName, instanceId);
434
447
  const qid = await this.handleNextStep(stepsName, goto, state, instanceId, options);
448
+ //console.debug('Worker qid', qid);
449
+ //res.end();
435
450
  } catch (error) {
436
451
  const { stepsName, goto, state, instanceId, options } = req.body.payload;
437
452
  const connection = await Datastore.open();
@@ -441,6 +456,7 @@ class Workflow extends EventEmitter {
441
456
  console.error('Error in function: ' + stepName, error);
442
457
  this.emit('error', error);
443
458
  } finally {
459
+ //console.debug('Worker res.end', stepName);
444
460
  res.end();
445
461
  }
446
462
  });
@@ -549,7 +565,7 @@ class Workflow extends EventEmitter {
549
565
  { _id: instanceId },
550
566
  { $set: { ...state, updatedAt: new Date().toISOString() } });
551
567
  if (options.continue) {
552
- await this.continue(stepsName, instanceId);
568
+ await this.continue(stepsName, instanceId, false);
553
569
  }
554
570
  resolve({ ...doc });
555
571
  });