Skip to content

Commit

Permalink
Fix rendering stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Aug 2, 2022
1 parent c802167 commit 38a1fed
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions packages/yew/src/virtual_dom/vlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ mod test {

#[cfg(feature = "ssr")]
mod feat_ssr {
use futures::stream::{FuturesOrdered, StreamExt};
use futures::future;
use futures::stream::{FuturesUnordered, StreamExt};

use super::*;
use crate::html::AnyScope;
Expand All @@ -176,26 +177,42 @@ mod feat_ssr {
}
_ => {
let buf_capacity = w.capacity();
let mut children_streams = Vec::with_capacity(self.children.len());
let mut children_furs = FuturesUnordered::new();

let mut children = self.children.iter();
let first_child = children.next().expect("first child should exist!");

// Concurrently render all children.
let mut children: FuturesOrdered<_> = self
.children
.iter()
.map(|m| async move {
let (mut w, r) = io::buffer(buf_capacity);

m.render_into_stream(&mut w, parent_scope, hydratable).await;
drop(w);

r
})
.collect();

while let Some(mut r) = children.next().await {
while let Some(next_chunk) = r.next().await {
w.write(next_chunk.into());
}
for child in children {
let (mut w, r) = io::buffer(buf_capacity);

children_furs.push(async move {
child
.render_into_stream(&mut w, parent_scope, hydratable)
.await;
});

children_streams.push(r);
}

// Concurrently resolve all futures.
let resolve_fur = async move { while children_furs.next().await.is_some() {} };

// Transfer results to parent writer.
let transfer_fur = async move {
first_child
.render_into_stream(w, parent_scope, hydratable)
.await;

for mut r in children_streams.into_iter() {
while let Some(next_chunk) = r.next().await {
w.write(next_chunk.into());
}
}
};

future::join(resolve_fur, transfer_fur).await;
}
}
}
Expand Down

0 comments on commit 38a1fed

Please sign in to comment.