4

I try to figure out why .unpipe call is no-op if the stream has been piped after .pause:

b.on('data', console.log.bind(console, "on b -> "));
c.on('data', console.log.bind(console, "on c -> "));

a.pause();

a.pipe(b);
a.pipe(c);

a.resume();

a.unpipe(b);
a.unpipe(c);
a.write("foo");

prints:

//on b ->  foo
//on c ->  foo

It seems to be because of .pause() call. But it's important to keep the stream paused 'till all streams are piped. How can I workaround this issue in an elegant way?

There is opportunity to attach 'data' event listener on source stream as destination stream's write function.

b.on('data', console.log.bind(console, "on b -> "));
c.on('data', console.log.bind(console, "on c -> "));

writeToB = b.write.bind(b);
writeToC = c.write.bind(c);


a.pause();

a.on('data', writeToB);
a.on('data', writeToC);

a.resume();

a.removeListener('data',writeToB);
a.removeListener('data',writeToC);

a.write("foo");

but this approach feels like bad idea.

ZigGreen
  • 168
  • 1
  • 9

1 Answers1

1

Try hooking up the data events after you pipe.

Hooking the data event causes the pipe to start flowing.

a.pipe(b);
a.pipe(c);

b.on('data', console.log.bind(console, "on b -> "));
c.on('data', console.log.bind(console, "on c -> "));

a.unpipe(b);
a.unpipe(c);
a.write("foo");