Skip to content
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

Polled SSR Stream #2824

Merged
merged 32 commits into from
Sep 10, 2022
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5bf1ae8
Switch to pinned channels.
futursolo Aug 4, 2022
fb2603e
Fix ServerRenderer so it's not blocked until the result is resolved.
futursolo Aug 5, 2022
892b759
Fix tests.
futursolo Aug 5, 2022
29a46bd
Remove unused SendError.
futursolo Aug 5, 2022
20fc4a3
Merge branch 'master' into pinned-channels
futursolo Aug 7, 2022
8a41c3e
Implement a stream to be polled alongside rendering.
futursolo Aug 11, 2022
8688045
Update Buffer Size.
futursolo Aug 11, 2022
3bd9e0a
Make Send renderer work.
futursolo Aug 11, 2022
ed36760
Remove pinned channels.
futursolo Aug 11, 2022
2799a22
Unified Naming.
futursolo Aug 11, 2022
02a838d
Optimise code.
futursolo Aug 12, 2022
3de0770
Restore capacity.
futursolo Aug 12, 2022
689980e
merge 'master' into polled-stream
futursolo Aug 12, 2022
cbbd765
Remove unused profile.
futursolo Aug 12, 2022
2045c49
Merge branch 'master' into polled-stream
futursolo Aug 12, 2022
a470862
Default to separate resolver.
futursolo Aug 12, 2022
3ac02fd
Reduce allocations on string.
futursolo Aug 12, 2022
345b745
Adjust API.
futursolo Aug 13, 2022
d1a6ab3
Remove duplicate trait bound.
futursolo Aug 13, 2022
f0863a4
Update docs.
futursolo Aug 13, 2022
7dcaf6e
Remove capacity setting.
futursolo Aug 13, 2022
ced12ac
Merge branch 'master' into polled-stream
futursolo Aug 14, 2022
475f52e
Merge branch 'master' into polled-stream
futursolo Aug 16, 2022
e757d35
Unsafe?
futursolo Aug 16, 2022
0a399ba
Separate files.
futursolo Aug 16, 2022
6ae5a65
Adjust inlining.
futursolo Aug 16, 2022
f310575
Merge branch 'master' into polled-stream
futursolo Aug 29, 2022
e18e0fd
Fix test.
futursolo Aug 29, 2022
67348d3
Update notice.
futursolo Aug 29, 2022
f491d6c
Update documentation.
futursolo Sep 4, 2022
2a300fe
Merge branch 'master' into polled-stream
futursolo Sep 4, 2022
a78f0b3
Fix tests.
futursolo Sep 4, 2022
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
Prev Previous commit
Next Next commit
Merge branch 'master' into polled-stream
# Conflicts:
#	packages/yew/src/server_renderer.rs
futursolo committed Aug 29, 2022
commit f31057501915f44f2bd9ffb76387a39530446768
101 changes: 69 additions & 32 deletions packages/yew/src/server_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use std::fmt;
use std::future::Future;

use futures::pin_mut;
use futures::stream::{Stream, StreamExt};
use tracing::Instrument;

use crate::html::{BaseComponent, Scope};
use crate::platform::fmt::BufStream;
use crate::platform::{run_pinned, spawn_local};
use crate::platform::{LocalHandle, Runtime};

/// A Yew Server-side Renderer that renders on the current thread.
///
/// # Note
///
/// This renderer does not spawn its own runtime and can only be used when:
///
/// - `wasm-bindgen` is selected as the backend of Yew runtime.
/// - `wasm-bindgen-futures` is selected as the backend of Yew runtime.
/// - running within a [`Runtime`](crate::platform::Runtime).
/// - running within a tokio [`LocalSet`](tokio::task::LocalSet).
/// - running within a tokio [`LocalSet`](struct@tokio::task::LocalSet).
#[cfg(feature = "ssr")]
#[derive(Debug)]
pub struct LocalServerRenderer<COMP>
@@ -74,11 +75,10 @@ where

/// Renders Yew Application.
pub async fn render(self) -> String {
let mut s = String::new();

self.render_to_string(&mut s).await;
let s = self.render_stream();
futures::pin_mut!(s);

s
s.collect().await
}

/// Renders Yew Application to a String.
@@ -115,7 +115,7 @@ where

/// A Yew Server-side Renderer.
///
/// This renderer spawns the rendering task to an internal worker pool and receives result when
/// This renderer spawns the rendering task to a Yew [`Runtime`]. and receives result when
/// the rendering process has finished.
///
/// See [`yew::platform`] for more information.
@@ -126,6 +126,7 @@ where
{
create_props: Box<dyn Send + FnOnce() -> COMP::Properties>,
hydratable: bool,
rt: Option<Runtime>,
}

impl<COMP> fmt::Debug for ServerRenderer<COMP>
@@ -175,9 +176,17 @@ where
Self {
create_props: Box::new(create_props),
hydratable: true,
rt: None,
}
}

/// Sets the runtime the ServerRenderer will run the rendering task with.
pub fn with_runtime(mut self, rt: Runtime) -> Self {
self.rt = Some(rt);

self
}

/// Sets whether an the rendered result is hydratable.
///
/// Defaults to `true`.
@@ -192,11 +201,26 @@ where

/// Renders Yew Application.
pub async fn render(self) -> String {
let mut s = String::new();
let Self {
create_props,
hydratable,
rt,
} = self;

self.render_to_string(&mut s).await;
let (tx, rx) = futures::channel::oneshot::channel();
let create_task = move || async move {
let props = create_props();
let s = LocalServerRenderer::<COMP>::with_props(props)
.hydratable(hydratable)
.render()
.await;

s
let _ = tx.send(s);
};

Self::spawn_rendering_task(rt, create_task);

rx.await.expect("failed to receive.")
}

/// Renders Yew Application to a String.
@@ -208,34 +232,47 @@ where
}
}

/// Renders Yew Applications into a string Stream.
///
/// # Note
///
/// Unlike [`LocalServerRenderer::render_stream`], this method is `async fn`.
pub async fn render_stream(self) -> impl Stream<Item = String> {
#[inline]
fn spawn_rendering_task<F, Fut>(rt: Option<Runtime>, create_task: F)
where
F: 'static + Send + FnOnce() -> Fut,
Fut: Future<Output = ()> + 'static,
{
match rt {
// If a runtime is specified, spawn to the specified runtime.
Some(m) => m.spawn_pinned(create_task),
None => match LocalHandle::try_current() {
// If within a Yew Runtime, spawn to the current runtime.
Some(m) => m.spawn_local(create_task()),
// Outside of Yew Runtime, spawn to the default runtime.
None => Runtime::default().spawn_pinned(create_task),
},
}
}

/// Renders Yew Application into a string Stream.
pub fn render_stream(self) -> impl Send + Stream<Item = String> {
let Self {
create_props,
hydratable,
rt,
} = self;

run_pinned(move || async move {
let (tx, rx) = futures::channel::mpsc::unbounded();
let (tx, rx) = futures::channel::mpsc::unbounded();
let create_task = move || async move {
let props = create_props();
let s = LocalServerRenderer::<COMP>::with_props(props)
.hydratable(hydratable)
.render_stream();
pin_mut!(s);

spawn_local(async move {
let props = create_props();
let s = LocalServerRenderer::<COMP>::with_props(props)
.hydratable(hydratable)
.render_stream();
pin_mut!(s);
while let Some(m) = s.next().await {
let _ = tx.unbounded_send(m);
}
};

while let Some(m) = s.next().await {
let _ = tx.unbounded_send(m);
}
});
Self::spawn_rendering_task(rt, create_task);

rx
})
.await
rx
}
}
You are viewing a condensed version of this merge commit. You can view the full changes here.