forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor use ref hooks (yewstack#2093)
* Refactor use ref hooks `use_ref` has been renamed to `use_mut_ref` and `use_ref` has become a similar hook for immutable reference. This is different from React but I think lines up nicely with Rust as a reference is immutable unless specified to be a mut ref. * fix CI Co-authored-by: Hamza <muhammadhamza1311@gmail.com> (cherry picked from commit 071f1b2)
- Loading branch information
1 parent
81437fd
commit 4efd980
Showing
7 changed files
with
353 additions
and
35 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,64 @@ | ||
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_mut_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_mut_ref(move || { | ||
T::bridge(Callback::from(move |output| { | ||
let on_output = on_output_ref.borrow().clone(); | ||
on_output(output); | ||
})) | ||
}); | ||
|
||
UseBridgeHandle { inner: bridge } | ||
} | ||
|
||
impl<T: Agent> Clone for UseBridgeHandle<T> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
inner: self.inner.clone(), | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.