mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
Add send_stream method for Scope (#2619)
* add send_stream method for Scope * add eof stream note * map stream item type * No need for nightly, fmt, updates Co-authored-by: Muhammad Hamza <muhammadhamza1311@gmail.com>
This commit is contained in:
parent
7438cd3059
commit
fc965435e8
@ -7,6 +7,8 @@ use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
use std::{fmt, iter};
|
||||
|
||||
use futures::{Stream, StreamExt};
|
||||
|
||||
#[cfg(any(feature = "csr", feature = "ssr"))]
|
||||
use super::lifecycle::ComponentState;
|
||||
use super::BaseComponent;
|
||||
@ -236,6 +238,35 @@ impl<COMP: BaseComponent> Scope<COMP> {
|
||||
spawn_local(js_future);
|
||||
}
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// This method will not notify the component when the stream has been fully exhausted. If
|
||||
/// you want this feature, you can add an EOF message variant for your component and use
|
||||
/// [`StreamExt::chain`] and [`stream::once`] to chain an EOF message to the original stream.
|
||||
/// If your stream is produced by another crate, you can use [`StreamExt::map`] to transform
|
||||
/// the stream's item type to the component message type.
|
||||
pub fn send_stream<S, M>(&self, stream: S)
|
||||
where
|
||||
M: Into<COMP::Message>,
|
||||
S: Stream<Item = M> + 'static,
|
||||
{
|
||||
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);
|
||||
}
|
||||
};
|
||||
spawn_local(js_future);
|
||||
}
|
||||
|
||||
/// Returns the linked component if available
|
||||
pub fn get_component(&self) -> Option<impl Deref<Target = COMP> + '_> {
|
||||
self.arch_get_component()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user