Expand description
A series of traits to implement the behavior of reactive primitive, especially signals.
§Principles
- Composition: Most of the traits are implemented as combinations of more primitive base traits, and blanket implemented for all types that implement those traits.
- Fallibility: Most traits includes a
try_
variant, which returnsNone
if the method fails (e.g., if signals are arena allocated and this can’t be found, or if anRwLock
is poisoned).
§Metadata Traits
DefinedAt
is used for debugging in the case of errors and should be implemented for all signal types.IsDisposed
checks whether a signal is currently accessible.
§Base Traits
Trait | Mode | Description |
---|---|---|
Track | — | Tracks changes to this value, adding it as a source of the current reactive observer. |
Notify | — | Notifies subscribers that this value has changed. |
ReadUntracked | Guard | Gives immutable access to the value of this signal. |
Write | Guard | Gives mutable access to the value of this signal. |
§Derived Traits
§Access
Trait | Mode | Composition | Description |
---|---|---|---|
WithUntracked | fn(&T) -> U | ReadUntracked | Applies closure to the current value of the signal and returns result. |
With | fn(&T) -> U | ReadUntracked + Track | Applies closure to the current value of the signal and returns result, with reactive tracking. |
GetUntracked | T | WithUntracked + Clone | Clones the current value of the signal. |
Get | T | GetUntracked + Track | Clones the current value of the signal, with reactive tracking. |
§Update
Trait | Mode | Composition | Description |
---|---|---|---|
UpdateUntracked | fn(&mut T) | Write | Applies closure to the current value to update it, but doesn’t notify subscribers. |
Update | fn(&mut T) | UpdateUntracked + Notify | Applies closure to the current value to update it, and notifies subscribers. |
Set | T | Update | Sets the value to a new value, and notifies subscribers. |
§Using the Traits
These traits are designed so that you can implement as few as possible, and the rest will be implemented automatically.
For example, if you have a struct for which you can implement ReadUntracked
and Track
, then
WithUntracked
and With
will be implemented automatically (as will GetUntracked
and
Get
for Clone
types). But if you cannot implement ReadUntracked
(because, for example,
there isn’t an RwLock
so you can’t wrap in a ReadGuard
,
but you can still implement WithUntracked
and Track
, the same traits will still be implemented.
Traits§
- Describes where the signal was defined. This is used for diagnostic warnings and is purely a debug-mode tool.
- Allows disposing an arena-allocated signal before its owner has been disposed.
- Helper trait to implement flatten() on
Option<&Option<T>>
. - Allows creating a signal from an async
Stream
. - Clones the value of the signal, without tracking the value reactively. and subscribes the active reactive observer (an effect or computed) to changes in its value.
- Clones the value of the signal, without tracking the value reactively.
- A variation of the
Get
trait that provides a signposted “always-non-reactive” API. E.g. forStoredValue
. - Checks whether a signal has already been disposed.
- Notifies subscribers of a change in this signal.
- Give read-only access to a signal’s value by reference through a guard type, and subscribes the active reactive observer (an effect or computed) to changes in its value.
- An alternative
Read
trait that works withOption<Readable>
types. - Give read-only access to a signal’s value by reference through a guard type, without tracking the value reactively.
- An alternative
ReadUntracked
trait that works withOption<Readable>
types. - A variation of the
Read
trait that provides a signposted “always-non-reactive” API. E.g. forStoredValue
. - Updates the value of the signal by replacing it.
- A variation of the
Set
trait that provides a signposted “always-non-reactive” API. E.g. forStoredValue
. - Allows converting a signal into an async
Stream
. - Allows tracking the value of some reactive data.
- A reactive, mutable guard that can be untracked to prevent it from notifying subscribers when it is dropped.
- Updates the value of a signal by applying a function that updates it in place, notifying its subscribers that the value has changed.
- Updates the value of a signal by applying a function that updates it in place, without notifying subscribers.
- A variation of the
Update
trait that provides a signposted “always-non-reactive” API. E.g. forStoredValue
. - Give read-only access to a signal’s value by reference inside a closure, and subscribes the active reactive observer (an effect or computed) to changes in its value.
- An alternative
With
trait that works withOption<Withable>
types. - Give read-only access to a signal’s value by reference inside a closure, without tracking the value reactively.
- An alternative
WithUntracked
trait that works withOption<Withable>
types. - A variation of the
With
trait that provides a signposted “always-non-reactive” API. E.g. forStoredValue
. - Gives mutable access to a signal’s value through a guard type. When the guard is dropped, the signal’s subscribers will be notified.
- A variation of the
Write
trait that provides a signposted “always-non-reactive” API. E.g. forStoredValue
.