mevatron
mevatron

Reputation: 14011

Chaining Node.js streams with run-time logic

I have been working with Node.js streams for the past 6 months or so, and I have been really happy with them so far. All of the problems I have encountered thus far, I have been able to solve using the standard template of:

A.pipe(B).pipe(C);

However, my current problems requires chaining different stream "pipelines" based on run-time logic. For example, what I'd like to do is something like the following:

var basePipeline = A.pipe(B).pipe(C);
if(flowRate > 0.0) {
    basePipeline.pipe(D).pipe(E).pipe(F);
} else {
    basePipeline.pipe(G).pipe(H).pipe(I);
}

Is the above possible? Or, do I need to create both streams separately in a complete fashion:

if(flowRate > 0.0) {
    A.pipe(B).pipe(C).pipe(D).pipe(E).pipe(F);
} else {
    A.pipe(B).pipe(C).pipe(G).pipe(H).pipe(I);
}

Thanks for the advice!

Upvotes: 0

Views: 151

Answers (1)

Ygalbel
Ygalbel

Reputation: 5519

What you can do is always pass over all streams, But you keep an array that store if this step have to run or skip. For Example:

if your pipe is like this A -> B -> C -> D -> E -> F -> H

And you have an hash

A:0
B:1
C:0
D:1
...
...

that mean you will run only pipes B and D.

On begin of pipe you check if current pipe is in the hash

// step B
pipe(function(data)){
    if(steps['B'] === 1){
       // do something
    }
}

With this approach, you have fixed pipes, but you can on the fly change the flow.

Upvotes: 1

Related Questions