Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide &Dependency to Lazy widget view fn #1646

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/lazy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Sandbox for App {
}

fn view(&self) -> Element<Message> {
let options = lazy(self.version, || {
let options = lazy(self.version, |_| {
let mut items: Vec<_> = self.items.iter().cloned().collect();

items.sort_by(|a, b| match self.order {
Expand Down
12 changes: 8 additions & 4 deletions lazy/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::rc::Rc;
#[allow(missing_debug_implementations)]
pub struct Lazy<'a, Message, Renderer, Dependency, View> {
dependency: Dependency,
view: Box<dyn Fn() -> View + 'a>,
view: Box<dyn Fn(&Dependency) -> View + 'a>,
element: RefCell<
Option<Rc<RefCell<Option<Element<'static, Message, Renderer>>>>>,
>,
Expand All @@ -28,7 +28,10 @@ where
Dependency: Hash + 'a,
View: Into<Element<'static, Message, Renderer>>,
{
pub fn new(dependency: Dependency, view: impl Fn() -> View + 'a) -> Self {
pub fn new(
dependency: Dependency,
view: impl Fn(&Dependency) -> View + 'a,
) -> Self {
Self {
dependency,
view: Box::new(view),
Expand Down Expand Up @@ -88,7 +91,8 @@ where
self.dependency.hash(&mut hasher);
let hash = hasher.finish();

let element = Rc::new(RefCell::new(Some((self.view)().into())));
let element =
Rc::new(RefCell::new(Some((self.view)(&self.dependency).into())));

(*self.element.borrow_mut()) = Some(element.clone());

Expand All @@ -109,7 +113,7 @@ where
if current.hash != new_hash {
current.hash = new_hash;

let element = (self.view)().into();
let element = (self.view)(&self.dependency).into();
current.element = Rc::new(RefCell::new(Some(element)));

(*self.element.borrow_mut()) = Some(current.element.clone());
Expand Down
2 changes: 1 addition & 1 deletion lazy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use std::hash::Hash;

pub fn lazy<'a, Message, Renderer, Dependency, View>(
dependency: Dependency,
view: impl Fn() -> View + 'a,
view: impl Fn(&Dependency) -> View + 'a,
) -> Lazy<'a, Message, Renderer, Dependency, View>
where
Dependency: Hash + 'a,
Expand Down