valtio-history / Exports
Ƭ History<T
>: Object
Name |
---|
T |
Name | Type | Description |
---|---|---|
index |
number |
the history index of the current snapshot |
nodes |
HistoryNode <T >[] |
the nodes of the history for each change |
wip |
Snapshot <T > | typeof EMPTY_WIP |
field for holding sandbox changes; used to avoid infinite loops |
packages/history-utility/src/index.ts:23
Ƭ HistoryNode<T
>: Object
Name |
---|
T |
Name | Type | Description |
---|---|---|
createdAt |
Date |
The date when the node was created |
snapshot |
Snapshot <T > |
The snapshot being tracked |
updatedAt? |
Date |
The date when the node was updated. Will be undefined if the node was never updated. |
packages/history-utility/src/index.ts:5
Ƭ HistoryOptions: Object
Name | Type | Description |
---|---|---|
skipSubscribe? |
boolean |
determines if the internal subscribe behaviour should be skipped. |
packages/history-utility/src/index.ts:40
▸ proxyWithHistory<V
>(initialValue
, options?
): Object
This creates a new proxy with history support (ProxyHistoryObject).
It includes following main properties:
- value: any value (does not have to be an object)
- history: an object holding the history of snapshots and other metadata
- history.index: the history index of the current snapshot
- history.nodes: the nodes of the history for each change
- history.wip: field for holding sandbox changes; used to avoid infinite loops
- history.index: the history index of the current snapshot
Notes:
- Suspense/promise is not supported.
Name |
---|
V |
Name | Type | Description |
---|---|---|
initialValue |
V |
any value to be tracked |
options? |
boolean | HistoryOptions |
use to configure the proxyWithHistory utility. |
Object
proxyObject
Name | Type | Description |
---|---|---|
canRedo |
() => boolean |
a function to return true if redo is available Deprecated See #10 |
canUndo |
() => boolean |
a function to return true if undo is available Deprecated See #10 |
clone |
<T>(obj : T , getRefSet? : () => WeakSet <object >) => T |
utility to clone a snapshot |
getCurrentChangeDate |
() => undefined | Date |
get the date when a node was entered into history. Deprecated See #10 |
getNode |
(index : number ) => undefined | HistoryNode <V > |
utility method to get a history node. The snapshot within this node is already cloned and will not affect the original value if updated. |
goTo |
(index : number ) => void |
a function to go to a specific index in history |
history |
History <V > & { $$valtioSnapshot : History <V > } |
an object holding the history of snapshots and other metadata - history.index: the history index to the current snapshot - history.nodes: the nodes of the history for each change - history.wip: field for holding sandbox changes; used to avoid infinite loops |
redo |
() => void |
a function to go forward in history |
remove |
(index : number ) => undefined | HistoryNode <V > |
The remove method is only invoked when there are more than one nodes and when a valid index is provided. If the current index is removed, An index greater than the current index will be preferred as the next value. |
replace |
(index : number , value : Snapshot <V >) => void |
utility to replace a value in history. The history changes will not be affected, only the value to be replaced. If a base value is needed to operate on, the getNode utility can be used to retrieve a cloned historyNode. Notes: - No operations are done on the value provided to this utility. - This is an advanced method, please ensure the value provided is a snapshot of the same type of the value being tracked. |
saveHistory |
() => void |
a function to execute saving history when changes are made to value |
shouldSaveHistory |
(ops : Op []) => boolean |
a function that returns true when the history should be updated |
subscribe |
() => () => void |
a function to subscribe to changes made to value |
undo |
() => void |
a function to go back in history |
value |
V |
any value to be tracked (does not have to be an object) |
get currentChangeDate() |
undefined | Date |
- |
get currentIndex() |
number |
- |
get historyNodeCount() |
number |
- |
get isRedoEnabled() |
boolean |
- |
get isUndoEnabled() |
boolean |
- |
Example
import { proxyWithHistory } from 'valtio-history'
const state = proxyWithHistory({
count: 1,
})