Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for streams #13

Open
peterjwest opened this issue Nov 18, 2014 · 5 comments
Open

Support for streams #13

peterjwest opened this issue Nov 18, 2014 · 5 comments

Comments

@peterjwest
Copy link

Any chance there could be support for streams as locals?

@ForbesLindesay
Copy link
Member

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

@peterjwest
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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
Copy link
Contributor

jeromew commented Nov 19, 2014

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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants