pugjs / then-pug

**EXPERIMENTAL** Async promise based Jade
42 stars 5 forks source link

Support for streams #13

Open peterjwest opened 9 years ago

peterjwest commented 9 years ago

Any chance there could be support for streams as locals?

ForbesLindesay commented 9 years ago

You can already pass streams as locals, you just can't then pipe them to the output.

peterjwest commented 9 years ago

That's kinda what I meant, not much use having a stream in a jade template if you can't pump out the data.

ForbesLindesay commented 9 years ago

If you added a method to your stream that returns a promise you could then write something like:

- stream.on('data', buf.push);
- yield stream.wait();

in your template.

peterjwest commented 9 years ago

Would that wait until the whole stream has processed before outputting? I was hoping to stream data to the HTTP response as data comes into the input stream.

jeromew commented 9 years ago

The stream given by then-jade can be sent to the HTTP response. This is how I use it.

From what I understand, you are asking if you can interleave the jade stream with external streams coming via locals like

body
  div = << stream1 >>
body
  div = << stream2 >>

with

locals = { stream1: any Readable stream, stream2: any Readable stream }

I think that the solution given by @ForbesLindesay would work even though it would not handle backpressure on stream1 & stream2 (the on('data') will put them in flowing mode). As an answer to your question, yes, it would flush all the stream in the correct place.

This is untested code but you could probably implement his solution with barrage and a mixin.

var barrage = require('barrage');
locals = { 
    barrage: barrage,
    stream1: any Readable stream,
    stream2: any Readable stream
}

and then in your jade file

mixin streamit(stream)
  - var s = barrage(stream)
  - s.on('data', buf.push)
  - yield (s.wait())

body
  div
    +streamit(stream1)
  div
    +streamit(stream2)

Keep us posted !