#Sharing streams

19 messages · Page 1 of 1 (latest)

delicate rain
#

So I have a small project where I want to use streams. But now I need two different transform streams on the same input

const file = await Deno.open(path);

const processingStream1 = file.pipeThrough(transform1);
for await (const line of processingStream1) console.log(line);


const processingStream2 = file.pipeThrough(transform2);
for await (const line of processingStream2) console.log(line);
#

What I want to try and do is let processingStream2 continue from where processStream1 left off

tranquil mica
#

a transform can only stop in the middle if it throws an error, otherwise it will consume all of its input

delicate rain
#

Even if .cancel() is used on the controller?

tranquil mica
#

oh

#

I'm not actually sure about that

delicate rain
#

Cuz right now that is done, but the readable stream that feeds into the transformstream is still locked

#

Which means that I cannot reuse it :/

tranquil mica
#

I don't think you can do that. But I don't think that's a bad thing.

delicate rain
#

Shit....

#

That really sucks for data that needs different processing...

tranquil mica
#

To explain why, consider this implementation of TextLineStream:

new TransformStream({
  lineSoFar: "",
  transform(chunk, controller) {
    chunk = this.lineSoFar + chunk;
    const lines = chunk.split("\n");
    this.lineSoFar = lines.pop();
    for (const line of lines) {
      controller.enqueue(line + "\n");
    }
  },
  flush(controller) {
    if (this.lineSoFar !== "") {
      controller.enqueue(this.lineSoFar);
    }
  }
});
#

there's data coming from previous chunks that hasn't been enqueued yet

#

so if you could get back to the original stream, you'd lose that data

delicate rain
#

Hmmm. I guess I'll just use a reader and loop with that

#

It's just so annoying that a stream can't be reused or cloned from a certaint part

#

I guess I'll figure out another solution...

#

Cuz 1.5m nop fn calls are quite expensive...

#

Well thanks for the insight and help @tranquil mica. I'm going to bed thinking of how to do this