@@ -2,7 +2,7 @@ use std::future::Future;
22use wasm_bindgen_futures:: spawn_local;
33use yew:: {
44 agent:: { Agent , AgentLink } ,
5- Component , ComponentLink ,
5+ Callback , Component , ComponentLink ,
66} ;
77
88/// Trait that allows you to use `ComponentLink` and `AgentLink` to register futures.
@@ -16,12 +16,25 @@ pub trait LinkFuture {
1616 /// # Panics
1717 /// If the future panics, then the promise will not resolve, and
1818 /// will leak.
19- fn callback_future < FN , FU , IN , M > ( & self , function : FN ) -> yew :: Callback < IN >
19+ fn callback_future < FN , FU , IN , M > ( & self , function : FN ) -> Callback < IN >
2020 where
2121 M : Into < Self :: Message > ,
2222 FU : Future < Output = M > + ' static ,
2323 FN : Fn ( IN ) -> FU + ' static ;
2424
25+ /// This method creates a `Callback` from `FnOnce` which returns a Future
26+ /// which returns a message to be sent back to the component's event
27+ /// loop.
28+ ///
29+ /// # Panics
30+ /// If the future panics, then the promise will not resolve, and
31+ /// will leak.
32+ fn callback_future_once < FN , FU , IN , M > ( & self , function : FN ) -> Callback < IN >
33+ where
34+ M : Into < Self :: Message > ,
35+ FU : Future < Output = M > + ' static ,
36+ FN : FnOnce ( IN ) -> FU + ' static ;
37+
2538 /// This method processes a Future that returns a message and sends it back to the component's
2639 /// loop.
2740 ///
@@ -43,7 +56,7 @@ pub trait LinkFuture {
4356impl < COMP : Component > LinkFuture for ComponentLink < COMP > {
4457 type Message = COMP :: Message ;
4558
46- fn callback_future < FN , FU , IN , M > ( & self , function : FN ) -> yew :: Callback < IN >
59+ fn callback_future < FN , FU , IN , M > ( & self , function : FN ) -> Callback < IN >
4760 where
4861 M : Into < Self :: Message > ,
4962 FU : Future < Output = M > + ' static ,
@@ -59,6 +72,22 @@ impl<COMP: Component> LinkFuture for ComponentLink<COMP> {
5972 closure. into ( )
6073 }
6174
75+ fn callback_future_once < FN , FU , IN , M > ( & self , function : FN ) -> Callback < IN >
76+ where
77+ M : Into < Self :: Message > ,
78+ FU : Future < Output = M > + ' static ,
79+ FN : FnOnce ( IN ) -> FU + ' static ,
80+ {
81+ let link = self . clone ( ) ;
82+
83+ let closure = move |input : IN | {
84+ let future: FU = function ( input) ;
85+ link. send_future ( future) ;
86+ } ;
87+
88+ Callback :: once ( closure)
89+ }
90+
6291 fn send_future < F , M > ( & self , future : F )
6392 where
6493 M : Into < Self :: Message > ,
@@ -88,7 +117,7 @@ impl<COMP: Component> LinkFuture for ComponentLink<COMP> {
88117impl < AGN : Agent > LinkFuture for AgentLink < AGN > {
89118 type Message = AGN :: Message ;
90119
91- fn callback_future < FN , FU , IN , M > ( & self , function : FN ) -> yew :: Callback < IN >
120+ fn callback_future < FN , FU , IN , M > ( & self , function : FN ) -> Callback < IN >
92121 where
93122 M : Into < Self :: Message > ,
94123 FU : Future < Output = M > + ' static ,
@@ -104,6 +133,22 @@ impl<AGN: Agent> LinkFuture for AgentLink<AGN> {
104133 closure. into ( )
105134 }
106135
136+ fn callback_future_once < FN , FU , IN , M > ( & self , function : FN ) -> Callback < IN >
137+ where
138+ M : Into < Self :: Message > ,
139+ FU : Future < Output = M > + ' static ,
140+ FN : FnOnce ( IN ) -> FU + ' static ,
141+ {
142+ let link = self . clone ( ) ;
143+
144+ let closure = move |input : IN | {
145+ let future: FU = function ( input) ;
146+ link. send_future ( future) ;
147+ } ;
148+
149+ Callback :: once ( closure)
150+ }
151+
107152 fn send_future < F , M > ( & self , future : F )
108153 where
109154 M : Into < Self :: Message > ,
0 commit comments