wait until response is flushed before unsubscribing from broker

I am not sure why this works, but it seems to. I have been trying to
enable the scaffolder and found that when I view a task in progress or
complete that the task just sits with the text "Waiting for logs...".

This is a hunch.

Signed-off-by: Brian Fletcher <brian@roadie.io>
This commit is contained in:
Brian Fletcher
2021-07-13 20:38:17 +01:00
parent 474cb76c88
commit 11b07616fa
@@ -233,17 +233,19 @@ export async function createRouter(
);
}
let shouldUnsubscribe = false;
for (const event of events) {
res.write(
`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`,
);
if (event.type === 'completion') {
unsubscribe();
shouldUnsubscribe = true;
// Closing the event stream here would cause the frontend
// to automatically reconnect because it lost connection.
}
}
res.flush();
if (shouldUnsubscribe) unsubscribe();
},
);
// When client closes connection we update the clients list