n8n-nodes-while-loop 0.1.2 → 0.1.4

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.
@@ -37,15 +37,15 @@ class WhileLoop {
37
37
  typeOptions: { minValue: 1, maxValue: 100000 },
38
38
  description: "Safety cap to prevent infinite loops",
39
39
  },
40
- {
41
- displayName: "Condition Expression",
42
- name: "conditionExpression",
43
- type: "string",
44
- typeOptions: { rows: 2 },
45
- default: "={{$json.counter < 5}}",
46
- description: "n8n expression that must evaluate to true to continue looping. You can use {{$loop.iteration}}.",
47
- displayOptions: { show: { mode: ["expression"] } },
48
- },
40
+ {
41
+ displayName: "Condition Expression",
42
+ name: "conditionExpression",
43
+ type: "string",
44
+ typeOptions: { rows: 2 },
45
+ default: "={{$json.loopIteration < 5}}",
46
+ description: "n8n expression that must be true to continue. Available: $json.loopIteration, $json.loopFirst, $json.loopMax.",
47
+ displayOptions: { show: { mode: ["expression"] } },
48
+ },
49
49
  {
50
50
  displayName: "Add Loop Metadata",
51
51
  name: "addMeta",
@@ -64,10 +64,9 @@ class WhileLoop {
64
64
  const conditionExpression = mode === "expression"
65
65
  ? this.getNodeParameter("conditionExpression", 0, "")
66
66
  : "";
67
- const state = this.getWorkflowStaticData("execution");
68
- const nodeKey = this.getNode().id;
69
- state.__whileLoop = state.__whileLoop || {};
70
- const loopState = state.__whileLoop[nodeKey] || { iteration: 0 };
67
+ const state = this.getWorkflowStaticData("node"); // safe context in n8n 2.x
68
+ state.loopState = state.loopState || { iteration: 0 };
69
+ const loopState = state.loopState;
71
70
  const continueItems = [];
72
71
  const doneItems = [];
73
72
  for (let i = 0; i < items.length; i++) {
@@ -78,18 +77,21 @@ class WhileLoop {
78
77
  }
79
78
  else {
80
79
  try {
81
- const originalLoop = items[i].json?.$loop;
82
- items[i].json = items[i].json || {};
83
- items[i].json.$loop = { iteration };
84
- const exprResult = this.evaluateExpression(conditionExpression, i, items);
80
+ const injected = {
81
+ loopIteration: iteration,
82
+ loopFirst: iteration === 0,
83
+ loopMax: maxIterations,
84
+ };
85
+ const originalJson = items[i].json || {};
86
+ const tempItem = {
87
+ ...items[i],
88
+ json: {
89
+ ...originalJson,
90
+ ...injected,
91
+ },
92
+ };
93
+ const exprResult = this.evaluateExpression(conditionExpression, i, undefined, undefined, tempItem);
85
94
  shouldContinue = Boolean(exprResult) && iteration < maxIterations;
86
- // restore
87
- if (originalLoop === undefined) {
88
- delete items[i].json.$loop;
89
- }
90
- else {
91
- items[i].json.$loop = originalLoop;
92
- }
93
95
  }
94
96
  catch (error) {
95
97
  throw new NodeApiError(this.getNode(), error);
@@ -110,15 +112,15 @@ class WhileLoop {
110
112
  }
111
113
  else {
112
114
  // reset state for next execution
113
- delete state.__whileLoop[nodeKey];
115
+ state.loopState = { iteration: 0 };
114
116
  doneItems.push(items[i]);
115
117
  }
116
118
  }
117
119
  if (continueItems.length) {
118
- state.__whileLoop[nodeKey] = loopState;
120
+ state.loopState = loopState;
119
121
  }
120
122
  else {
121
- delete state.__whileLoop[nodeKey];
123
+ state.loopState = { iteration: 0 };
122
124
  }
123
125
  return [continueItems, doneItems];
124
126
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-while-loop",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Simple while-loop control node for n8n with counter or expression conditions",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",