js-undoable
A primitive class which lets you track changes made to it's typed value.
Notes
This class uses JS native strict equality checking to prevent duplicate change entries. This means that assigning the same value (or class instance) in succession will not count as a change. When exported to a buffer, it tries to create dictionary of unique instances of changed values being tracked, in order to unpack with correct references later on. This project is still under development and might have breaking changes. Don't use in production environment until more testing can be done.
Installation
npm install --save js-undoable
Usage Examples
Including in Typescript:
;
Including in NodeJS:
const Undoable = ;
An "undoable" number (in Typescript):
; ; undoNum.current = 26;undoNum.current = 27;undoNum.current = 28;undoNum.current = 55; console.logundoNum.changeCount === 4; // trueconsole.logundoNum.current === 55; // true undoNum.undo2; // returns diffs undone console.logundoNum.changeCount === 2; // trueconsole.logundoNum.current === 27; // true
Export and load from Buffer (in Typescript):
; ; undoNum.current = 26;undoNum.current = 27;undoNum.current = 28;undoNum.current = 55; ;// exported now contains a Buffer object ;// imported is now almost identitical to undoNum
Commit first change to snapshot (in Typescript):
; ; undoNum.current = 26;undoNum.current = 27; // replace snapshot with "26" change, "25" is gone forever.undoNum.commit1; console.logundoNum.changeCount === 1; // trueconsole.logundoNum.current === 27; // true undoNum.undo1; console.logundoNum.changeCount === 0; // trueconsole.logundoNum.current === 26; // true
Credits
Author: Hans Doller <kryo2k@gmail.com