-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Redirect Comp. * Fix router behaviour. * Fix output. * Fix pr-flow. * Remove Redirect. * Readd 77b46bf. * Add use_bridge hook. * Add documentation.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
use crate::*; | ||
use yew::prelude::*; | ||
|
||
/// State handle for [`use_bridge`] hook | ||
pub struct UseBridgeHandle<T> | ||
where | ||
T: Bridged, | ||
{ | ||
inner: Rc<RefCell<Box<dyn Bridge<T>>>>, | ||
} | ||
|
||
impl<T> UseBridgeHandle<T> | ||
where | ||
T: Bridged, | ||
{ | ||
/// Send a message to an agent. | ||
pub fn send(&self, msg: T::Input) { | ||
let mut bridge = self.inner.borrow_mut(); | ||
bridge.send(msg); | ||
} | ||
} | ||
|
||
/// A hook to bridge to an Agent. | ||
/// | ||
/// This hooks will only bridge the agent once over the entire component lifecycle. | ||
/// | ||
/// Takes a callback as the only argument. The callback will be updated on every render to make | ||
/// sure captured values (if any) are up to date. | ||
pub fn use_bridge<T, F>(on_output: F) -> UseBridgeHandle<T> | ||
where | ||
T: Bridged, | ||
F: Fn(T::Output) + 'static, | ||
{ | ||
let on_output = Rc::new(on_output); | ||
|
||
let on_output_clone = on_output.clone(); | ||
let on_output_ref = use_ref(move || on_output_clone); | ||
|
||
// Refresh the callback on every render. | ||
{ | ||
let mut on_output_ref = on_output_ref.borrow_mut(); | ||
*on_output_ref = on_output; | ||
} | ||
|
||
let bridge = use_ref(move || { | ||
T::bridge(Callback::from(move |output| { | ||
let on_output = on_output_ref.borrow().clone(); | ||
on_output(output); | ||
})) | ||
}); | ||
|
||
UseBridgeHandle { inner: bridge } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters