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

Add read untracked to signals #1593

Merged
merged 5 commits into from
Dec 7, 2023
Merged
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
23 changes: 21 additions & 2 deletions packages/signals/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ impl<T: 'static> Signal<T> {
self.inner.origin_scope()
}

/// Get the current value of the signal. This will subscribe the current scope to the signal.
/// Get the current value of the signal. This will subscribe the current scope to the signal. If you would like to read the signal without subscribing to it, you can use [`Self::peek`] instead.
///
/// If the signal has been dropped, this will panic.
#[track_caller]
pub fn read(&self) -> GenerationalRef<T> {
Expand Down Expand Up @@ -233,7 +234,16 @@ impl<T: 'static> Signal<T> {
GenerationalRef::map(inner, |v| &v.value)
}

/// Get the current value of the signal. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
///
/// If the signal has been dropped, this will panic.
pub fn peek(&self) -> GenerationalRef<T> {
let inner = self.inner.read();
GenerationalRef::map(inner, |v| &v.value)
}

/// Get a mutable reference to the signal's value.
///
/// If the signal has been dropped, this will panic.
#[track_caller]
pub fn write(&self) -> Write<T> {
Expand Down Expand Up @@ -418,12 +428,21 @@ impl<T: 'static> ReadOnlySignal<T> {
self.inner.origin_scope()
}

/// Get the current value of the signal. This will subscribe the current scope to the signal.
/// Get the current value of the signal. This will subscribe the current scope to the signal. If you would like to read the signal without subscribing to it, you can use [`Self::peek`] instead.
///
/// If the signal has been dropped, this will panic.
#[track_caller]
pub fn read(&self) -> GenerationalRef<T> {
self.inner.read()
}

/// Get the current value of the signal. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
///
/// If the signal has been dropped, this will panic.
pub fn peek(&self) -> GenerationalRef<T> {
self.inner.peek()
}

/// Run a closure with a reference to the signal's value.
#[track_caller]
pub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O {
Expand Down