Skip to content

Commit

Permalink
Removed Deref implementation for Key;
Browse files Browse the repository at this point in the history
Removed `commit_at_root` field of the `Key` bc of it being out of date.
  • Loading branch information
zetanumbers committed Dec 29, 2020
1 parent 4dfb658 commit 149d004
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dom/examples/todo/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn filter_link(to_set: Visibility) -> Li {
mox! {
<li>
<a style="cursor: pointer;"
class={if **visibility.commit_at_root() == to_set { "selected" } else { "" }}
class={if *visibility.commit_at_root() == to_set { "selected" } else { "" }}
onclick={move |_| visibility.set(to_set)}>
{% "{}", to_set }
</a>
Expand Down
3 changes: 2 additions & 1 deletion dom/examples/todo/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Key<Vec<Todo>>>()[0];
let todos_key = &illicit::expect::<Key<Vec<Todo>>>();
let todo = &todos_key.commit_at_root()[0];
todo_item(todo)
});

Expand Down
44 changes: 17 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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());
Expand Down Expand Up @@ -679,7 +679,6 @@ where
/// See [`state`] and [`cache_state`] for examples.
pub struct Key<State> {
id: CallId,
commit_at_root: Commit<State>,
var: Arc<Mutex<Var<State>>>,
}

Expand All @@ -690,8 +689,8 @@ impl<State> Key<State> {
}

/// Returns the `Commit` of the current `Revision`
pub fn commit_at_root(&self) -> &Commit<State> {
&self.commit_at_root
pub fn commit_at_root(&self) -> Commit<State> {
self.var.lock().current_commit().clone()
}

/// Runs `updater` with a reference to the state variable's latest value,
Expand Down Expand Up @@ -726,15 +725,15 @@ impl<State> Key<State> {
/// 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");
Expand Down Expand Up @@ -780,16 +779,7 @@ where

impl<State> Clone for Key<State> {
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<State> Deref for Key<State> {
type Target = State;

fn deref(&self) -> &Self::Target {
self.commit_at_root.deref()
Self { id: self.id, var: self.var.clone() }
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/runtime/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<State> Var<State> {
(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.
Expand All @@ -60,6 +60,11 @@ impl<State> Var<State> {
.unwrap_or(&self.current)
}

///
pub fn current_commit(&self) -> &Commit<State> {
&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.
Expand Down

0 comments on commit 149d004

Please sign in to comment.