Skip to content

Commit

Permalink
No need for nightly, fmt, updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza1311 committed Aug 28, 2022
1 parent 8b70346 commit 7239dd6
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions packages/yew/src/html/component/scope.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -699,48 +730,6 @@ mod feat_hydration {
}
}

#[cfg(all(nightly_yew, any(target_arch = "wasm32", feature = "tokio")))]
mod feat_nightly {
use super::*;
use crate::platform::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.
///
/// # 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.
///
/// [`StreamExt::chain`]: https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html#method.chain
/// [`stream::once`]: https://docs.rs/futures/latest/futures/stream/fn.once.html
/// [`StreamExt::map`]: https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html#method.map
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);
}
}
}

/// Defines a message type that can be sent to a component.
/// Used for the return value of closure given to
/// [Scope::batch_callback](struct.Scope.html#method.batch_callback).
Expand Down

0 comments on commit 7239dd6

Please sign in to comment.