state
Path Description String (path
)
path
)Noflux provides a way to traverse any JavaScript object, the "path description string". By using path description string separated with the dot (.
) you can specific any child of the object as easy as accessing the object directly. For example:
path = ''
locates to the root node,{ a: { b: [1, 2, 3] } }
。path ='a'
locates to{ b: [1, 2, 3] }
。path = 'a.b'
locates to[1, 2, 3]
。Path description string supports array index.
path = 'a.b.1'
locates to3
。In particular, for paths that contain
.
or~
, a similar way to JSON Pointer is used..
is escaped to~0
and~
is escaped to~1
.
In the next document, the
path
in arguments and variables refer to the path description string.
state.get([path = ''])
state.get([path = ''])
state.get
receive an optional path description string path
whose default value is ''
, which represents the root node of the state tree.
As shown in the last line of code the state.get
always return undefined
for non-existent paths instead of throwing an exception. This makes it unnecessary to do so much repetitive initialization and judgment when developing.
state.get
is the recommended way to get the internal state tree. Although using .
directly also works but may loss performance. (See more here: Partial Connecting)
state.set([path = ''], value)
state.set([path = ''], value)
As same as state.get
,state.set
receive an optional path description string path
. In addition, state.set
receive a value
and "override" the state on the path.
@noflux/state
implements the Copy-on-write in JavaScript
. So all operations that modify the state
will make a clone.
This means that state
is immutable and can be used to implement snapshots, hot reloading, and so on.
It is important to note that state.set
is the recommended method of changing the state. Using .
directly, like state.get().a.b = 1
will cause the component can not re-rendering as well.
state.update([path = ''], callback)
state.update([path = ''], callback)
The update
method provides a more functional way to update state. The callback
function receives the old value and should return the new value.
state.cursor([path = ''])
state.cursor([path = ''])
If you need to read and write specific path of state
, you can use state.cursor
to make the code clearer.
State.cursor
returns an instance of State
, that can be get
, set
, and even cursor
to implement chain call.
Array methods
Because methods like Array.prototype.push
will modify the original array and make state
mutable. state
wrapped these methods so they always return a clone of array.
Inlucding:
state.push(...items)
state.pop()
state.unshift(...items)
state.shift()
state.fill(value)
state.reverse()
state.splice(start, deleteCount, ...items)
Array methods don't support the path description string
path
. You can use it withstate.cursor
. For example:
Snapshots
Thanks to immutable and copy-on-write state
, it can be a lower cost to implement snapshots。
Calling state.snapshot ()
will save the current state and call the following method to "undo" or "redo":
state.canUndo()
state.undo()
state.canRedo()
state.redo()
Note that it is important to use canUndo
orcanRedo
before calling undo
or redo
otherwise exceeding the range of snapshots will throw an exception: RangeError: no more snapshot available
。
Last updated