7.7 KiB
@theatre/dataverse / Atom
Class: Atom<State>
Wraps an object whose (sub)properties can be individually tracked.
Type parameters
Name |
---|
State |
Implements
Table of contents
Constructors
Properties
Methods
- _checkUpdates
- _getIn
- _getOrCreateScopeForPath
- _onPointerValueChange
- get
- getByPointer
- pointerToPrism
- reduce
- reduceByPointer
- set
- setByPointer
Constructors
constructor
• new Atom<State
>(initialState
)
Type parameters
Name |
---|
State |
Parameters
Name | Type |
---|---|
initialState |
State |
Defined in
Properties
_currentState
• Private
_currentState: State
Defined in
_rootScope
• Private
Readonly
_rootScope: Scope
Defined in
pointer
• Readonly
pointer: Pointer
<State
>
Convenience property that gives you a pointer to the root of the atom.
Remarks
Equivalent to pointer({ root: thisAtom, path: [] })
.
Defined in
prism
• Readonly
prism: Prism
<State
>
Defined in
Methods
_checkUpdates
▸ Private
_checkUpdates(scope
, oldState
, newState
): void
Parameters
Name | Type |
---|---|
scope |
Scope |
oldState |
unknown |
newState |
unknown |
Returns
void
Defined in
_getIn
▸ Private
_getIn(path
): unknown
Gets the state of the atom at path
.
Parameters
Name | Type |
---|---|
path |
(string | number )[] |
Returns
unknown
Defined in
_getOrCreateScopeForPath
▸ Private
_getOrCreateScopeForPath(path
): Scope
Parameters
Name | Type |
---|---|
path |
(string | number )[] |
Returns
Scope
Defined in
_onPointerValueChange
▸ Private
_onPointerValueChange<P
>(pointer
, cb
): () => void
Type parameters
Name |
---|
P |
Parameters
Name | Type |
---|---|
pointer |
Pointer <P > |
cb |
(v : P ) => void |
Returns
fn
▸ (): void
Returns
void
Defined in
get
▸ get(): State
Returns
State
Defined in
getByPointer
▸ getByPointer<S
>(pointerOrFn
): S
Returns the value at the given pointer
Type parameters
Name |
---|
S |
Parameters
Name | Type | Description |
---|---|---|
pointerOrFn |
Pointer <S > | (p : Pointer <State >) => Pointer <S > |
A pointer to the desired path. Could also be a function returning a pointer Example ts const atom = atom({ a: { b: 1 } }) atom.getByPointer(atom.pointer.a.b) // 1 atom.getByPointer((p) => p.a.b) // 1 |
Returns
S
Defined in
pointerToPrism
▸ pointerToPrism<P
>(pointer
): Prism
<P
>
Returns a new prism of the value at the provided path.
Type parameters
Name |
---|
P |
Parameters
Name | Type | Description |
---|---|---|
pointer |
Pointer <P > |
The path to create the prism at. ts const pr = atom({ a: { b: 1 } }).pointerToPrism(atom.pointer.a.b) pr.getValue() // 1 |
Returns
Prism
<P
>
Implementation of
PointerToPrismProvider.pointerToPrism
Defined in
reduce
▸ reduce(fn
): void
Parameters
Name | Type |
---|---|
fn |
(state : State ) => State |
Returns
void
Defined in
reduceByPointer
▸ reduceByPointer<S
>(pointerOrFn
, reducer
): void
Reduces the value at the given pointer
Type parameters
Name |
---|
S |
Parameters
Name | Type | Description |
---|---|---|
pointerOrFn |
Pointer <S > | (p : Pointer <State >) => Pointer <S > |
A pointer to the desired path. Could also be a function returning a pointer Example ts const atom = atom({ a: { b: 1 } }) atom.reduceByPointer(atom.pointer.a.b, (b) => b + 1) // atom.get().a.b === 2 atom.reduceByPointer((p) => p.a.b, (b) => b + 1) // atom.get().a.b === 2 |
reducer |
(s : S ) => S |
- |
Returns
void
Defined in
set
▸ set(newState
): void
Sets the state of the atom.
Parameters
Name | Type | Description |
---|---|---|
newState |
State |
The new state of the atom. |
Returns
void
Defined in
setByPointer
▸ setByPointer<S
>(pointerOrFn
, val
): void
Sets the value at the given pointer
Type parameters
Name |
---|
S |
Parameters
Name | Type | Description |
---|---|---|
pointerOrFn |
Pointer <S > | (p : Pointer <State >) => Pointer <S > |
A pointer to the desired path. Could also be a function returning a pointer Example ts const atom = atom({ a: { b: 1 } }) atom.setByPointer(atom.pointer.a.b, 2) // atom.get().a.b === 2 atom.setByPointer((p) => p.a.b, 2) // atom.get().a.b === 2 |
val |
S |
- |
Returns
void