diff --git a/dom/examples/todo/src/filter.rs b/dom/examples/todo/src/filter.rs index 279f9d717..e70a61e92 100644 --- a/dom/examples/todo/src/filter.rs +++ b/dom/examples/todo/src/filter.rs @@ -49,7 +49,7 @@ pub fn filter_link(to_set: Visibility) -> Li { mox! {
  • {% "{}", to_set } diff --git a/dom/examples/todo/src/item.rs b/dom/examples/todo/src/item.rs index 0bb45419d..c69866e7d 100644 --- a/dom/examples/todo/src/item.rs +++ b/dom/examples/todo/src/item.rs @@ -97,7 +97,8 @@ mod tests { pub async fn single_item() { let root = document().create_element("div"); crate::App::boot_fn(&[Todo::new("weeeee")], root.clone(), || { - let todo = &illicit::expect::>>()[0]; + let todos_key = &illicit::expect::>>(); + let todo = &todos_key.commit_at_root()[0]; todo_item(todo) }); diff --git a/src/lib.rs b/src/lib.rs index 270667912..2e4df8029 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -291,15 +291,15 @@ where /// assert!(!track_wakes.is_woken(), "no updates yet"); /// /// first_key.set(0); // this is a no-op -/// assert_eq!(**first_key.commit_at_root(), 0, "no updates yet"); +/// assert_eq!(*first_key.commit_at_root(), 0, "no updates yet"); /// assert!(!track_wakes.is_woken(), "no updates yet"); /// /// first_key.set(1); -/// assert_eq!(**first_key.commit_at_root(), 0, "update only enqueued, not yet committed"); +/// assert_eq!(*first_key.commit_at_root(), 0, "update only enqueued, not yet committed"); /// assert!(track_wakes.is_woken()); /// /// let (second_commit, second_key) = rt.run_once(); // this commits the pending update -/// assert_eq!(**second_key.commit_at_root(), 1); +/// assert_eq!(*second_key.commit_at_root(), 1); /// assert_eq!(*second_commit, 1); /// assert_eq!(*first_commit, 0, "previous value still held by previous pointer"); /// assert!(!track_wakes.is_woken(), "wakes only come from updating state vars"); @@ -336,15 +336,15 @@ where /// assert!(!track_wakes.is_woken(), "no updates yet"); /// /// first_key.set(0); // this is a no-op -/// assert_eq!(**first_key.commit_at_root(), 0, "no updates yet"); +/// assert_eq!(*first_key.commit_at_root(), 0, "no updates yet"); /// assert!(!track_wakes.is_woken(), "no updates yet"); /// /// first_key.set(1); -/// assert_eq!(**first_key.commit_at_root(), 0, "update only enqueued, not yet committed"); +/// assert_eq!(*first_key.commit_at_root(), 0, "update only enqueued, not yet committed"); /// assert!(track_wakes.is_woken()); /// /// let (second_commit, second_key) = rt.run_once(); // this commits the pending update -/// assert_eq!(**second_key.commit_at_root(), 1); +/// assert_eq!(*second_key.commit_at_root(), 1); /// assert_eq!(*second_commit, 1); /// assert_eq!(*first_commit, 0, "previous value still held by previous pointer"); /// assert!(!track_wakes.is_woken(), "wakes only come from updating state vars"); @@ -361,15 +361,15 @@ where /// assert!(!track_wakes.is_woken()); /// /// third_key.set(2); -/// assert_eq!(**third_key.commit_at_root(), 2); +/// assert_eq!(*third_key.commit_at_root(), 2); /// assert!(!track_wakes.is_woken()); /// /// third_key.set(3); -/// assert_eq!(**third_key.commit_at_root(), 2); +/// assert_eq!(*third_key.commit_at_root(), 2); /// assert!(track_wakes.is_woken()); /// /// let (fourth_commit, fourth_key) = rt.run_once(); -/// assert_eq!(**fourth_key.commit_at_root(), 3); +/// assert_eq!(*fourth_key.commit_at_root(), 3); /// assert_eq!(*fourth_commit, 3); /// assert_eq!(*third_commit, 2); /// assert!(!track_wakes.is_woken()); @@ -679,7 +679,6 @@ where /// See [`state`] and [`cache_state`] for examples. pub struct Key { id: CallId, - commit_at_root: Commit, var: Arc>>, } @@ -690,8 +689,8 @@ impl Key { } /// Returns the `Commit` of the current `Revision` - pub fn commit_at_root(&self) -> &Commit { - &self.commit_at_root + pub fn commit_at_root(&self) -> Commit { + self.var.lock().current_commit().clone() } /// Runs `updater` with a reference to the state variable's latest value, @@ -726,15 +725,15 @@ impl Key { /// assert!(!track_wakes.is_woken(), "no updates yet"); /// /// first_key.update(|_| None); // this is a no-op - /// assert_eq!(**first_key.commit_at_root(), 0, "no updates yet"); + /// assert_eq!(*first_key.commit_at_root(), 0, "no updates yet"); /// assert!(!track_wakes.is_woken(), "no updates yet"); /// /// first_key.update(|prev| Some(prev + 1)); - /// assert_eq!(**first_key.commit_at_root(), 0, "update only enqueued, not yet committed"); + /// assert_eq!(*first_key.commit_at_root(), 0, "update only enqueued, not yet committed"); /// assert!(track_wakes.is_woken()); /// /// let (second_commit, second_key) = rt.run_once(); // this commits the pending update - /// assert_eq!(**second_key.commit_at_root(), 1); + /// assert_eq!(*second_key.commit_at_root(), 1); /// assert_eq!(*second_commit, 1); /// assert_eq!(*first_commit, 0, "previous value still held by previous pointer"); /// assert!(!track_wakes.is_woken(), "wakes only come from updating state vars"); @@ -780,16 +779,7 @@ where impl Clone for Key { fn clone(&self) -> Self { - Self { id: self.id, commit_at_root: self.commit_at_root.clone(), var: self.var.clone() } - } -} - -// TODO(#197) delete this and remove the Deref impl -impl Deref for Key { - type Target = State; - - fn deref(&self) -> &Self::Target { - self.commit_at_root.deref() + Self { id: self.id, var: self.var.clone() } } } @@ -798,7 +788,7 @@ where State: Debug, { fn fmt(&self, f: &mut Formatter) -> FmtResult { - self.commit_at_root.fmt(f) + self.commit_at_root().fmt(f) } } @@ -807,7 +797,7 @@ where State: Display, { fn fmt(&self, f: &mut Formatter) -> FmtResult { - self.commit_at_root.fmt(f) + self.commit_at_root().fmt(f) } } diff --git a/src/runtime/var.rs b/src/runtime/var.rs index a0a799fbd..c1123aede 100644 --- a/src/runtime/var.rs +++ b/src/runtime/var.rs @@ -48,7 +48,7 @@ impl Var { (var.id, var.current.clone()) }; - (commit_at_root.clone(), Key { id, commit_at_root, var }) + (commit_at_root, Key { id, var }) } /// Returns a reference to the latest value, pending or committed. @@ -60,6 +60,11 @@ impl Var { .unwrap_or(&self.current) } + /// + pub fn current_commit(&self) -> &Commit { + &self.current + } + /// Initiate a commit to the state variable. The commit will actually /// complete asynchronously when the state variable is next rooted in a /// topological function, flushing the pending commit.