coffeekup
coffeekup copied to clipboard
Streaming able ?
Correct me if I'm wrong.
As far as I can tell CoffeeKup will never be able to stream the template results because to be able to return the results the top level function must return, and that happens only when all the template finished computing.
Is there anything that can be done to enable streaming support? Will CoffeeKup ever be able to stream results?
Having said that, my main attraction to CoffeeKup is using it as a "helper language" inside sam's Eco meaning Eco does the templateing (streamable) and CK does view helping.
Rails
<%= content_tag 'div', class: 'some_class' ... %>
CoffeeKup
<%=ck -> div class: 'some_class' ... %>
I'm not really familiar with template streaming, but I don't think there should be a problem. No, we don't have to wait for the whole thing to return to get the output; as each function is executed it writes to a buffer. At this moment we might as well fire an event with the chunk, don't we?
I couldn't find any examples on streaming with Eco, nor support for it in its source code, but if you could point me in the right direction I'd be glad to study it and see what can be done in CoffeeKup.
Indeed you are right. That's awesome. I don't think Eco does streaming at this point, I just thought that it's definitively possible, since Rails 3.1 (ERB) did it and ERB and Eco are similar.
OK! So, I think this could make it into 0.3.0 final. What do you think of this API (completely stolen from dustjs):
compiled_template(foo: 'bar', options: {stream: yes})
.on('data', (data) -> console.log data)
.on('end', -> console.log "c'est fini!")
.on('error', (err) -> console.log 'doh!')
Looks good, conforms to node de facto conventions. I wander if the buffer object can also be given as an option (optional).
compiled_template(foo: 'bar', options: {stream: yes, buffer: res})
and this way have the results sent directly to a http.ServerResponse
object, no additional plumbing. Ofc. res should respond to data, end and error.
How would that work exactly? We'd call res.write
and res.end
directly?
Yes that's what I was thinking. The interface for the buffer should be http://nodejs.org/docs/v0.5.1/api/streams.html#writable_Stream mainly the stream.write(...)
and stream.end(...)
.
If a buffer isn't explicitly given use something internally, so it works both on server and client.