Skip to content

Commit

Permalink
Added callback_future_once in yewtil (#1696) (#1712)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraillt authored Jan 23, 2021
1 parent aef2ee5 commit e5eda4e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/yew-functional/src/hooks/use_reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ where
struct UseReducerState<State> {
current_state: Rc<State>,
}
impl<T> Hook for UseReducerState<T> {};
impl<T> Hook for UseReducerState<T> {}
let init = Box::new(init);
let reducer = Rc::new(reducer);
use_hook(
Expand Down
53 changes: 49 additions & 4 deletions packages/yewtil/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::future::Future;
use wasm_bindgen_futures::spawn_local;
use yew::{
agent::{Agent, AgentLink},
Component, ComponentLink,
Callback, Component, ComponentLink,
};

/// Trait that allows you to use `ComponentLink` and `AgentLink` to register futures.
Expand All @@ -16,12 +16,25 @@ pub trait LinkFuture {
/// # Panics
/// If the future panics, then the promise will not resolve, and
/// will leak.
fn callback_future<FN, FU, IN, M>(&self, function: FN) -> yew::Callback<IN>
fn callback_future<FN, FU, IN, M>(&self, function: FN) -> Callback<IN>
where
M: Into<Self::Message>,
FU: Future<Output = M> + 'static,
FN: Fn(IN) -> FU + 'static;

/// This method creates a `Callback` from `FnOnce` which returns a Future
/// which returns a message to be sent back to the component's event
/// loop.
///
/// # Panics
/// If the future panics, then the promise will not resolve, and
/// will leak.
fn callback_future_once<FN, FU, IN, M>(&self, function: FN) -> Callback<IN>
where
M: Into<Self::Message>,
FU: Future<Output = M> + 'static,
FN: FnOnce(IN) -> FU + 'static;

/// This method processes a Future that returns a message and sends it back to the component's
/// loop.
///
Expand All @@ -43,7 +56,7 @@ pub trait LinkFuture {
impl<COMP: Component> LinkFuture for ComponentLink<COMP> {
type Message = COMP::Message;

fn callback_future<FN, FU, IN, M>(&self, function: FN) -> yew::Callback<IN>
fn callback_future<FN, FU, IN, M>(&self, function: FN) -> Callback<IN>
where
M: Into<Self::Message>,
FU: Future<Output = M> + 'static,
Expand All @@ -59,6 +72,22 @@ impl<COMP: Component> LinkFuture for ComponentLink<COMP> {
closure.into()
}

fn callback_future_once<FN, FU, IN, M>(&self, function: FN) -> Callback<IN>
where
M: Into<Self::Message>,
FU: Future<Output = M> + 'static,
FN: FnOnce(IN) -> FU + 'static,
{
let link = self.clone();

let closure = move |input: IN| {
let future: FU = function(input);
link.send_future(future);
};

Callback::once(closure)
}

fn send_future<F, M>(&self, future: F)
where
M: Into<Self::Message>,
Expand Down Expand Up @@ -88,7 +117,7 @@ impl<COMP: Component> LinkFuture for ComponentLink<COMP> {
impl<AGN: Agent> LinkFuture for AgentLink<AGN> {
type Message = AGN::Message;

fn callback_future<FN, FU, IN, M>(&self, function: FN) -> yew::Callback<IN>
fn callback_future<FN, FU, IN, M>(&self, function: FN) -> Callback<IN>
where
M: Into<Self::Message>,
FU: Future<Output = M> + 'static,
Expand All @@ -104,6 +133,22 @@ impl<AGN: Agent> LinkFuture for AgentLink<AGN> {
closure.into()
}

fn callback_future_once<FN, FU, IN, M>(&self, function: FN) -> Callback<IN>
where
M: Into<Self::Message>,
FU: Future<Output = M> + 'static,
FN: FnOnce(IN) -> FU + 'static,
{
let link = self.clone();

let closure = move |input: IN| {
let future: FU = function(input);
link.send_future(future);
};

Callback::once(closure)
}

fn send_future<F, M>(&self, future: F)
where
M: Into<Self::Message>,
Expand Down

0 comments on commit e5eda4e

Please sign in to comment.