Observer
TheObserver<T> type defines the callback interface for observing changes to an atom’s value. It follows the RxJS observer pattern.
Type Definition
Properties
next
Called whenever the atom emits a new value.
Type:
error
Called when an error occurs during the atom’s operation.
Type:
complete
Called when the atom completes and will no longer emit values.
Type:
Usage Examples
Basic observer with next callback
Full observer with all callbacks
Simplified function syntax
Thesubscribe method also accepts a simpler syntax where you can pass the callbacks as separate function arguments:
With error and complete handlers
Type-safe observer
Conditional observer logic
Observer Pattern
The Observer type follows the standard observer pattern used in reactive programming:- next: Handles the normal flow of data
- error: Handles any errors that occur
- complete: Handles the completion of the observable stream
next callback, as atoms are long-lived and don’t usually complete or error in the traditional RxJS sense.
Related Types
- Subscription - The object returned when subscribing with an observer
- Atom - The writable atom type that can be observed
- ReadonlyAtom - The read-only atom type that can be observed