Skip to content

Commit

Permalink
Add set_rc and set_rc_silent methods to Signal
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Mar 21, 2022
1 parent 8eecc8f commit a3bc95c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/next/advanced/noderef.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A `NodeRef` can be created by using `create_node_ref`. This can be assigned, in
using the `ref` property in the `view!` macro.

```rust
let node_ref = create_node_ref(cx, );
let node_ref = create_node_ref(cx);

view! { cx,
p(ref=node_ref) { "Hello World!" }
Expand Down
37 changes: 34 additions & 3 deletions packages/sycamore-reactive/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,46 @@ impl<T> Signal<T> {
/// # });
/// ```
pub fn set(&self, value: T) {
*self.0.value.borrow_mut() = Rc::new(value);
self.set_silent(value);
self.0.emitter.trigger_subscribers();
}

/// Set the current value of the state wrapped in a [`Rc`]. Unlike [`Signal::set()`], this
/// method accepts the value wrapped in a [`Rc`] because the underlying storage is already using
/// [`Rc`], thus preventing an unnecessary clone.
///
/// This will notify and update any effects and memos that depend on this value.
///
/// # Example
/// ```
/// # use sycamore_reactive::*;
/// # create_scope_immediate(|cx| {
/// let state = create_signal(cx, 0);
/// assert_eq!(*state.get(), 0);
///
/// state.set_rc(Rc::new(1));
/// assert_eq!(*state.get(), 1);
/// # });
/// ```
pub fn set_rc(&self, value: Rc<T>) {
self.set_rc_silent(value);
self.0.emitter.trigger_subscribers();
}

/// Set the current value of the state _without_ triggering subscribers.
///
/// Make sure you know what you are doing because this can make state inconsistent.
pub fn set_silent(&self, value: T) {
*self.0.value.borrow_mut() = Rc::new(value);
self.set_rc_silent(Rc::new(value));
}

/// Set the current value of the state wrapped in a [`Rc`] _without_ triggering subscribers.
///
/// See the documentation for [`Signal::set_rc()`] for more information.
///
/// Make sure you know what you are doing because this can make state inconsistent.
pub fn set_rc_silent(&self, value: Rc<T>) {
*self.0.value.borrow_mut() = value;
}

/// Split a signal into getter and setter handles.
Expand Down Expand Up @@ -316,7 +347,7 @@ pub fn create_signal<T>(cx: Scope, value: T) -> &Signal<T> {
/// rc_state.set(1);
/// assert_eq!(*double.get(), 2);
///
/// // This isn't possible with simply create_signal(cx, )
/// // This isn't possible with simply create_signal(_)
/// outer = Some(rc_state);
/// });
/// ```
Expand Down
2 changes: 1 addition & 1 deletion packages/sycamore/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ impl<'a, G: GenericNode, F: FnOnce(Scope<'a>) -> G + 'a> ElementBuilder<'a, G, F
/// # use sycamore::builder::prelude::*;
/// # use sycamore::prelude::*;
/// # fn _test<G: GenericNode>(cx: Scope) -> View<G> {
/// let node_ref = create_node_ref(cx, );
/// let node_ref = create_node_ref(cx);
/// h(input).bind_ref(node_ref.clone())
/// # .view(cx) }
/// ```
Expand Down

0 comments on commit a3bc95c

Please sign in to comment.