Fix #2506 and improve some related documentation (#2507)

This commit is contained in:
WorldSEnder 2022-03-10 15:37:12 +01:00 committed by GitHub
parent 51238fb0e3
commit 889c6ba74e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -402,13 +402,11 @@ mod feat_io {
use crate::io_coop::spawn_local; use crate::io_coop::spawn_local;
impl<COMP: BaseComponent> Scope<COMP> { impl<COMP: BaseComponent> Scope<COMP> {
/// This method creates a [`Callback`] which returns a Future which /// This method creates a [`Callback`] which, when emitted, asynchronously awaits the
/// returns a message to be sent back to the component's event /// message returned from the passed function before sending it to the linked component.
/// loop.
/// ///
/// # Panics /// # Panics
/// If the future panics, then the promise will not resolve, and /// If the future panics, then the promise will not resolve, and will leak.
/// will leak.
pub fn callback_future<FN, FU, IN, M>(&self, function: FN) -> Callback<IN> pub fn callback_future<FN, FU, IN, M>(&self, function: FN) -> Callback<IN>
where where
M: Into<COMP::Message>, M: Into<COMP::Message>,
@ -425,8 +423,8 @@ mod feat_io {
closure.into() closure.into()
} }
/// This method processes a Future that returns a message and sends it back to the component's /// This method asynchronously awaits a [Future] that returns a message and sends it
/// loop. /// to the linked component.
/// ///
/// # Panics /// # Panics
/// If the future panics, then the promise will not resolve, and will leak. /// If the future panics, then the promise will not resolve, and will leak.
@ -443,17 +441,19 @@ mod feat_io {
spawn_local(js_future); spawn_local(js_future);
} }
/// Registers a Future that resolves to multiple messages. /// Asynchronously send a batch of messages to a component. This asynchronously awaits the
/// passed [Future], before sending the message batch to the linked component.
///
/// # Panics /// # Panics
/// If the future panics, then the promise will not resolve, and will leak. /// If the future panics, then the promise will not resolve, and will leak.
pub fn send_future_batch<F>(&self, future: F) pub fn send_future_batch<F>(&self, future: F)
where where
F: Future<Output = Vec<COMP::Message>> + 'static, F: Future + 'static,
F::Output: SendAsMessage<COMP>,
{ {
let link = self.clone(); let link = self.clone();
let js_future = async move { let js_future = async move {
let messages: Vec<COMP::Message> = future.await; future.await.send(&link);
link.send_message_batch(messages);
}; };
spawn_local(js_future); spawn_local(js_future);
} }