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

add send_stream method for Scope #2619

Merged
merged 5 commits into from Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/yew/Cargo.toml
Expand Up @@ -92,6 +92,7 @@ hydration = ["csr"]
trace_hydration = ["hydration"]
doc_test = ["csr", "hydration", "ssr"]
wasm_test = ["csr", "hydration", "ssr"]
nightly = ["futures"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

futures::stream is not nightly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But AsyncIterator is. Does someone know about the relation of the futures library to the std lib and if their traits will somehow magically migrate to std::async_iter::AsyncIterator or be compatible?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a future release of futures, they may choose to either impose a blank implementation of Stream for any type that implements AsyncIterator or make it a type alias.

But this does not matter at the moment as both tokio and futures point to futures::Stream and you need StreamExt to poll a stream.

default = []

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
Expand Down
34 changes: 34 additions & 0 deletions packages/yew/src/html/component/scope.rs
Expand Up @@ -592,6 +592,40 @@ mod feat_hydration {
#[cfg(feature = "csr")]
pub(crate) use feat_csr::*;

#[cfg_attr(
documenting,
doc(cfg(all(feature = "nightly", any(target_arch = "wasm32", feature = "tokio"))))
)]
#[cfg(all(feature = "nightly", any(target_arch = "wasm32", feature = "tokio")))]
mod feat_nightly {
use super::*;
use crate::io_coop::spawn_local;
use futures::{Stream, StreamExt};

impl<COMP: BaseComponent> Scope<COMP> {
/// This method asynchronously awaits a [Stream] that returns a series of messages and sends
/// them to the linked component.
///
/// # Panics
/// If the stream panics, then the promise will not resolve, and will leak.
pub fn send_stream<S, M>(&self, stream: S)
where
M: Into<COMP::Message>,
S: Stream<Item = M> + 'static,
futursolo marked this conversation as resolved.
Show resolved Hide resolved
{
let link = self.clone();
let js_future = async move {
futures::pin_mut!(stream);
while let Some(msg) = stream.next().await {
let message: COMP::Message = msg.into();
link.send_message(message);
futursolo marked this conversation as resolved.
Show resolved Hide resolved
}
};
spawn_local(js_future);
}
}
}

#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))]
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
mod feat_io {
Expand Down