I’m not sure, but since the return of renderToPipeableStream returns a stream, which is an eventEmitter…
Maybe this also emits an event allReady?
If it does, you could just manually reigster an eventHandler to the stream listening for that specific event, after you constructed the stream.
type opts = {onAllFinished: unit => unit}
type stream
@new @module("stream")
external makePassThrough: unit => stream = "PassThrough"
@send
external pipe: (stream, stream) => unit = "pipe"
@module("react-server")
external renderToPipeableStream: opts => stream = "renderToPipeableStream"
let stream = ref(None)
stream :=
renderToPipeableStream({
onAllFinished: () =>
stream.contents
->Belt.Option.map(stream => stream->pipe(makePassThrough()))
->Belt.Option.getWithDefault(),
})->Some
The compiled js isn’t really beautiful but looks like what you are trying to do?
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Stream from "stream";
import * as Belt_Option from "./stdlib/belt_Option.js";
import * as Caml_option from "./stdlib/caml_option.js";
import * as ReactServer from "react-server";
var stream = {
contents: undefined
};
stream.contents = Caml_option.some(ReactServer.renderToPipeableStream({
onAllFinished: (function (param) {
Belt_Option.getWithDefault(Belt_Option.map(stream.contents, (function (stream) {
stream.pipe(new Stream.PassThrough());
})), undefined);
})
}));
export {
stream ,
}
/* Not a pure module */