JS StateRewind
Simple state management with the ability to undo, redo & squash history.
Want to skip to a full working example? https://jsfiddle.net/p4rebqw2/15/
Install
npm install state-rewind
Usage
Init a state object
const state = ;
init with console logs for debug
const state = log: true ;
Set the state
state;
or exec
to run/execute a function at the same time that can later be undone or redone
state;
State can be anything, strings, numbers, arrays, objects, anything, e.g. (works with set
& exec
)
state;
Get the current state
state;
Undo/rewind state change
state;
& then redo/fast-forward current state
state;
You can also check if undo or redo are available with:
state; // booleanstate; // boolean
Get all recorded state changes
state;
Callbacks
Optionally listen in for changes to the state:
state;
Each set/exec can pass a forward and backward callback e.g. to handle the changes visually etc.
You can also set a default function that's given a direction of either forward or backward and the change where you can instruct how it should handle this. e.g.
state;
Editing history
Squash history, e.g. to remove duplicates or squash down similar objects such as changes to text, if the same elements text changes multiple times you might want to squash that down to just the latest change
state;// or for an objectstate;
or just run squash against the last set value
state;
The squash functions can both also take a 2nd callback to modify the data as it squashes down, e.g. if these are text changes to the same thing, you'd probably want the original "from text", but the latest "to text".
state;
Remove specific entries from the history.
state; // index => starting at 0
Clear/reset all history.
stateclear;
Starting from stored data
You can load in initial data with the load() command:
state;
This can be used in combination with setDefaultForwardBackwardCallback to have it auto run the callbacks such as visually changing the page to adapt to the loaded history.
state; state;
Initial locked states
You can also start the state with an initial value. By default it starts as undefined
but you can set initialState
as an option when creating the StateRewind
instance. This lets you have a default state available via get() that cannot be undone and will always show at the start of the getAll() array.
See the test.js
file for a unit test showing this in use & many other example uses.
Tips
Keyboard shortcuts
You may want to setup keyboard shortcuts for undo & redo.
This could be done like so, for ctrl+z (undo) & ctrl+y (redo)
document;
Debounce
Depending on how you're saving data, if it's user based such as on input, you may want to use this with a debounce function to not save constantly, find out more here or here's a package for debounce on npm.
Chaining
Almost all functions are chainable (except get, getAll, canUndo and canRedo).
E.g.
state
Change events
You may want to hook into the change event of the state, we expose a onChange() function for this.
This is especially useful for setting up undo and redo buttons, e.g.
UndoRedo
let $undoBtn = document;let $redoBtn = document;state;$undoBtn;$redoBtn;
Example workflow
E.g. here's an example where you could track changes to elements on a page with timestamps:
const state = ; // first changestate; // another changestate; state; state; state; state; state; // should only show first and last due to the `squashLast` replacing the last 2
Local development
Run tests
npm test
Publish new version
Update the package.json
's version
, commit, push and then:
npm publish
Contributing
Please run the tests locally and add new tests for new features/options added. Thank you.