@fractal-solutions/qflow 1.0.1 → 1.0.2
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 +138 -105
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -100,26 +100,36 @@ Chaining multiple nodes together to form a basic workflow.
|
|
|
100
100
|
```javascript
|
|
101
101
|
import { Node, Flow } from '@fractal-solutions/qflow';
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
exec(input) { return input + 5; }
|
|
105
|
-
}
|
|
103
|
+
let count = 0;
|
|
106
104
|
|
|
107
|
-
class
|
|
108
|
-
|
|
105
|
+
class MessageNode extends Node {
|
|
106
|
+
exec() {
|
|
107
|
+
count++;
|
|
108
|
+
console.log(`New Message ${count}`);
|
|
109
|
+
return `default`;
|
|
110
|
+
}
|
|
109
111
|
}
|
|
110
112
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
+
class TimeNode extends Node {
|
|
114
|
+
exec() {
|
|
115
|
+
console.log(`Time ${Date.now()}`);
|
|
116
|
+
return `default`;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
113
119
|
|
|
114
|
-
|
|
115
|
-
|
|
120
|
+
const m1 = new MessageNode();
|
|
121
|
+
const t1 = new TimeNode();
|
|
122
|
+
const m2 = new MessageNode();
|
|
116
123
|
|
|
117
|
-
|
|
118
|
-
|
|
124
|
+
m1.next(t1);
|
|
125
|
+
t1.next(m2);
|
|
119
126
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
127
|
+
const flow = new Flow(m1);
|
|
128
|
+
flow.run({});
|
|
129
|
+
// Expected output (approximate):
|
|
130
|
+
// New Message 1
|
|
131
|
+
// Time <timestamp>
|
|
132
|
+
// New Message 2
|
|
123
133
|
```
|
|
124
134
|
|
|
125
135
|
### 3. Conditional Flow
|
|
@@ -129,40 +139,54 @@ Using `transition()` for dynamic branching based on an action.
|
|
|
129
139
|
```javascript
|
|
130
140
|
import { Node, Flow } from '@fractal-solutions/qflow';
|
|
131
141
|
|
|
132
|
-
class
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
142
|
+
class ConditionalNode extends Node {
|
|
143
|
+
constructor(shouldGoLeft) {
|
|
144
|
+
super();
|
|
145
|
+
this.shouldGoLeft = shouldGoLeft;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
exec() {
|
|
149
|
+
if (this.shouldGoLeft) {
|
|
150
|
+
console.log('ConditionalNode: Going left');
|
|
151
|
+
return 'left';
|
|
136
152
|
} else {
|
|
137
|
-
|
|
153
|
+
console.log('ConditionalNode: Going right');
|
|
154
|
+
return 'right';
|
|
138
155
|
}
|
|
139
156
|
}
|
|
140
157
|
}
|
|
141
158
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
159
|
+
// Helper node for conditional transition test
|
|
160
|
+
function MessageNode(message) {
|
|
161
|
+
return new (class extends Node {
|
|
162
|
+
exec() {
|
|
163
|
+
console.log(message);
|
|
164
|
+
return 'default';
|
|
165
|
+
}
|
|
166
|
+
})();
|
|
148
167
|
}
|
|
149
168
|
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
// Chain the nodes with conditional transitions
|
|
155
|
-
check.transition('greater').to(greater);
|
|
156
|
-
check.transition('lessOrEqual').to(lessOrEqual);
|
|
157
|
-
|
|
158
|
-
// Create the flow, starting with the 'check' node
|
|
159
|
-
const flow = new Flow(check);
|
|
169
|
+
const conditionalNode = new ConditionalNode(true);
|
|
170
|
+
const leftNode = MessageNode('Went Left');
|
|
171
|
+
const rightNode = MessageNode('Went Right');
|
|
160
172
|
|
|
161
|
-
|
|
162
|
-
|
|
173
|
+
conditionalNode.next(leftNode, 'left');
|
|
174
|
+
conditionalNode.next(rightNode, 'right');
|
|
163
175
|
|
|
164
|
-
|
|
165
|
-
|
|
176
|
+
const conditionalFlow = new Flow(conditionalNode);
|
|
177
|
+
conditionalFlow.run({});
|
|
178
|
+
// Expected output:
|
|
179
|
+
// ConditionalNode: Going left
|
|
180
|
+
// Went Left
|
|
181
|
+
|
|
182
|
+
const conditionalNode2 = new ConditionalNode(false);
|
|
183
|
+
conditionalNode2.next(leftNode, 'left');
|
|
184
|
+
conditionalNode2.next(rightNode, 'right');
|
|
185
|
+
const conditionalFlow2 = new Flow(conditionalNode2);
|
|
186
|
+
conditionalFlow2.run({});
|
|
187
|
+
// Expected output:
|
|
188
|
+
// ConditionalNode: Going right
|
|
189
|
+
// Went Right
|
|
166
190
|
```
|
|
167
191
|
|
|
168
192
|
### 4. Asynchronous Flow
|
|
@@ -172,41 +196,26 @@ Handling asynchronous operations within a flow.
|
|
|
172
196
|
```javascript
|
|
173
197
|
import { AsyncNode, AsyncFlow } from '@fractal-solutions/qflow';
|
|
174
198
|
|
|
175
|
-
class
|
|
176
|
-
async execAsync(
|
|
177
|
-
console.log(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
class ProcessDataNode extends AsyncNode {
|
|
184
|
-
async execAsync(data) {
|
|
185
|
-
console.log(`Processing: ${data}`);
|
|
186
|
-
return data.toUpperCase();
|
|
199
|
+
class MyAsyncNode extends AsyncNode {
|
|
200
|
+
async execAsync() {
|
|
201
|
+
console.log('AsyncNode: Starting...');
|
|
202
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
203
|
+
console.log('AsyncNode: Finished!');
|
|
204
|
+
return 'default';
|
|
187
205
|
}
|
|
188
206
|
}
|
|
189
207
|
|
|
190
|
-
const
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
// Chain the nodes
|
|
194
|
-
fetchNode.next(processNode);
|
|
195
|
-
|
|
196
|
-
// Create the async flow, starting with the 'fetchNode'
|
|
197
|
-
const asyncFlow = new AsyncFlow(fetchNode);
|
|
198
|
-
|
|
199
|
-
async function runAsyncFlow() {
|
|
200
|
-
// Run the flow, passing the URL as an initial parameter to the first node
|
|
201
|
-
const result = await asyncFlow._orchAsync(null, { params: 'https://api.example.com/data' });
|
|
202
|
-
console.log('Async Flow Result:', result);
|
|
203
|
-
}
|
|
208
|
+
const asyncNode1 = new MyAsyncNode();
|
|
209
|
+
const asyncNode2 = new MyAsyncNode();
|
|
210
|
+
asyncNode1.next(asyncNode2);
|
|
204
211
|
|
|
205
|
-
|
|
212
|
+
const asyncFlow = new AsyncFlow(asyncNode1);
|
|
213
|
+
await asyncFlow.runAsync({});
|
|
206
214
|
// Expected output:
|
|
207
|
-
//
|
|
208
|
-
//
|
|
209
|
-
//
|
|
215
|
+
// AsyncNode: Starting...
|
|
216
|
+
// AsyncNode: Finished!
|
|
217
|
+
// AsyncNode: Starting...
|
|
218
|
+
// AsyncNode: Finished!
|
|
210
219
|
```
|
|
211
220
|
|
|
212
221
|
### 5. Batch Processing
|
|
@@ -214,26 +223,50 @@ runAsyncFlow();
|
|
|
214
223
|
Processing multiple items through a flow.
|
|
215
224
|
|
|
216
225
|
```javascript
|
|
217
|
-
import { Node, BatchFlow } from '@fractal-solutions/qflow';
|
|
226
|
+
import { Node, BatchFlow, AsyncParallelBatchFlow, AsyncNode } from '@fractal-solutions/qflow';
|
|
227
|
+
|
|
228
|
+
// Synchronous Batch Flow
|
|
229
|
+
console.log('\n--- Running Synchronous Batch Flow ---');
|
|
218
230
|
|
|
219
|
-
class
|
|
220
|
-
exec(
|
|
221
|
-
|
|
231
|
+
class MyBatchNode extends Node {
|
|
232
|
+
exec() {
|
|
233
|
+
console.log(`BatchNode: Processing item ${this.params.item}`);
|
|
234
|
+
return 'default';
|
|
222
235
|
}
|
|
223
236
|
}
|
|
224
237
|
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
// Define the items to process. For BatchFlow, the prep method should return an array.
|
|
231
|
-
batchFlow.prep = () => ['itemA', 'itemB', 'itemC'];
|
|
232
|
-
|
|
233
|
-
const results = batchFlow.run({}); // Run the batch flow
|
|
234
|
-
console.log('Batch Flow Results:', results);
|
|
238
|
+
const batchNode = new MyBatchNode();
|
|
239
|
+
const batchFlow = new BatchFlow(batchNode);
|
|
240
|
+
batchFlow.prep = () => [ { item: 1 }, { item: 2 }, { item: 3 } ];
|
|
241
|
+
batchFlow.run({});
|
|
235
242
|
// Expected output:
|
|
236
|
-
//
|
|
243
|
+
// BatchNode: Processing item 1
|
|
244
|
+
// BatchNode: Processing item 2
|
|
245
|
+
// BatchNode: Processing item 3
|
|
246
|
+
|
|
247
|
+
// Asynchronous Parallel Batch Flow
|
|
248
|
+
console.log('\n--- Running Asynchronous Parallel Batch Flow ---');
|
|
249
|
+
|
|
250
|
+
class MyAsyncParallelBatchNode extends AsyncNode {
|
|
251
|
+
async execAsync() {
|
|
252
|
+
console.log(`AsyncParallelBatchNode: Starting item ${this.params.item}`);
|
|
253
|
+
await new Promise(resolve => setTimeout(resolve, Math.random() * 100));
|
|
254
|
+
console.log(`AsyncParallelBatchNode: Finished item ${this.params.item}`);
|
|
255
|
+
return 'default';
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const asyncParallelBatchNode = new MyAsyncParallelBatchNode();
|
|
260
|
+
const asyncParallelBatchFlow = new AsyncParallelBatchFlow(asyncParallelBatchNode);
|
|
261
|
+
asyncParallelBatchFlow.prepAsync = async () => [ { item: 1 }, { item: 2 }, { item: 3 }, { item: 4 }, { item: 5 } ];
|
|
262
|
+
await asyncParallelBatchFlow.runAsync({});
|
|
263
|
+
// Expected output (order may vary due to parallel execution):
|
|
264
|
+
// AsyncParallelBatchNode: Starting item 1
|
|
265
|
+
// AsyncParallelBatchNode: Starting item 2
|
|
266
|
+
// ...
|
|
267
|
+
// AsyncParallelBatchNode: Finished item 1
|
|
268
|
+
// AsyncParallelBatchNode: Finished item 2
|
|
269
|
+
// ...
|
|
237
270
|
```
|
|
238
271
|
|
|
239
272
|
### 6. Retry Mechanism
|
|
@@ -241,37 +274,37 @@ console.log('Batch Flow Results:', results);
|
|
|
241
274
|
Configuring a node to retry on failure.
|
|
242
275
|
|
|
243
276
|
```javascript
|
|
244
|
-
import { Node } from '@fractal-solutions/qflow';
|
|
277
|
+
import { Node, Flow } from '@fractal-solutions/qflow';
|
|
245
278
|
|
|
246
|
-
let
|
|
247
|
-
class
|
|
279
|
+
let retryCount = 0;
|
|
280
|
+
class RetryNode extends Node {
|
|
248
281
|
constructor() {
|
|
249
|
-
super(3, 0.1); //
|
|
282
|
+
super(3, 0.1); // 3 retries, 0.1s wait
|
|
250
283
|
}
|
|
251
284
|
|
|
252
|
-
exec(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
throw new Error('
|
|
285
|
+
exec() {
|
|
286
|
+
retryCount++;
|
|
287
|
+
if (retryCount < 3) {
|
|
288
|
+
console.log(`RetryNode: Failing, attempt ${retryCount}`);
|
|
289
|
+
throw new Error('Failed!');
|
|
290
|
+
} else {
|
|
291
|
+
console.log('RetryNode: Succeeded!');
|
|
292
|
+
return 'default';
|
|
257
293
|
}
|
|
258
|
-
return `Success on attempt ${attempt}`;
|
|
259
294
|
}
|
|
260
295
|
|
|
261
296
|
execFallback(prepRes, error) {
|
|
262
|
-
console.
|
|
263
|
-
return 'Fallback result due to persistent failure';
|
|
297
|
+
console.log('RetryNode: Fallback executed');
|
|
264
298
|
}
|
|
265
299
|
}
|
|
266
300
|
|
|
267
|
-
const
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
// Expected output
|
|
271
|
-
//
|
|
272
|
-
//
|
|
273
|
-
//
|
|
274
|
-
// Flaky Node Result: Success on attempt 3
|
|
301
|
+
const retryNode = new RetryNode();
|
|
302
|
+
const retryFlow = new Flow(retryNode);
|
|
303
|
+
retryFlow.run({});
|
|
304
|
+
// Expected output:
|
|
305
|
+
// RetryNode: Failing, attempt 1
|
|
306
|
+
// RetryNode: Failing, attempt 2
|
|
307
|
+
// RetryNode: Succeeded!
|
|
275
308
|
```
|
|
276
309
|
|
|
277
310
|
## Built-in Nodes
|
|
@@ -330,4 +363,4 @@ runScrapeFlow();
|
|
|
330
363
|
|
|
331
364
|
We welcome contributions! Please see our GitHub repository for more details on how to contribute.
|
|
332
365
|
|
|
333
|
-
---
|
|
366
|
+
---
|
package/package.json
CHANGED