-
Notifications
You must be signed in to change notification settings - Fork 14
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
Allow passing async function to injectToStream #40
Comments
This is actually something I've been wondering. As soon as the stream provided by React closes, the wrapper stream (created by Passing a promise or an async function to I wonder if there is another (more reliable?) way to tell |
How about this: const stream = useStream()
const makeClosableAgain = stream.doNotClose()
// ...
stream.injectToStream(someChunk)
// ...
makeClosableAgain() |
I think it's not good and unpredictable for my use case because it can send the chunk too late. We need to "pause" the stream and respect the order of Example issue with this: const stream = useStream()
const makeClosableAgain = stream.doNotClose()
const someChunk = await something()
stream.injectToStream(someChunk)
makeClosableAgain() Call 2: const stream = useStream()
stream.injectToStream("something that depends on Call 1") Output: Passing an async function allows us to pause the stream until it resolves and respect the order of injectToStream calls: stream.injectToStream(async () => {
const someChunk = await something()
return someChunk;
}) Call 2: stream.injectToStream("something that depends on Call 1") Output:
I think this is ok. I think passing an async function and |
Makes sense, let me see if I can implement chunks as async functions. |
Both implemented and released. |
You can now pass a chunk promise. It isn't exactly what you wanted (you cannot pass an async function) because I think passing chunk promises is more accurate, but let me know if it doesn't work out. |
Example:
It would "block" the stream until the async function is resolved, while queuing other chunks in the meantime.
When the async function is resolved, it would write the resolved value to the stream, then continue with the queued chunks.
The following is not the same, the stream can close while we are waiting for
something
It would be nice for vikejs/vike-react#125, I think this is the only missing piece for the PR. It's working as-is, but can't send the last chunk for hydration, because the stream is closed.
With this implemented, I could mimic this
await
behavior: https://github.com/apollographql/apollo-client-nextjs/blob/e7a59cb26716a77bbfac659f435f89f5af8eff61/packages/client-react-streaming/src/stream-utils/createInjectionTransformStream.tsx#L104Why
await
is needed in the linked code, is a different issue: apollographql/apollo-client-nextjs#325The text was updated successfully, but these errors were encountered: