Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

End asyncStream later in the process (before piping) #185

Merged
merged 2 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ module.exports = function processRequest(options, request, response) {
} = options;

const asyncStream = new AsyncStream();
asyncStream.once('plugged', () => {
asyncStream.end();
});

const contextPromise = fetchContext(request).catch(err => {
this.emit('context:error', request, err);
return {};
Expand Down Expand Up @@ -195,9 +199,6 @@ module.exports = function processRequest(options, request, response) {
});

resultStream.once('finish', () => {
// Flush the async stream only after stringifer stream is finished
// This guarentees the async fragments are flushed to the client at last
asyncStream.end();
const statusCode = response.statusCode || 200;
if (shouldWriteHead) {
shouldWriteHead = false;
Expand Down
2 changes: 2 additions & 0 deletions lib/streams/stringifier-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = class StringifierStream extends stream.Transform {
}
let st = this.queue.shift();
if (st instanceof stream) {
st.emit('plugged');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would only emit this if its a instance of async stream though. Totally useless for other fragment streams.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently yes, but I didn't want to mix up knowledge of async stream into the stringifier stream

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case we would need alternative way of handling for this particular instance.


st.setMaxListeners(st.getMaxListeners() + 1);
this.isBusy = true;

Expand Down