Skip to content

Commit

Permalink
Merge pull request #1646 from nicksenger/feat/dependency-in-lazy
Browse files Browse the repository at this point in the history
Provide `&Dependency` to `Lazy` widget view fn
  • Loading branch information
hecrj authored Jan 12, 2023
2 parents ca337b8 + 58bcc44 commit 902131e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
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

0 comments on commit 902131e

Please sign in to comment.