├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | npm-debug.log 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | v0.2.0 - Thu, 30 Oct 2014 19:51:14 GMT 2 | -------------------------------------- 3 | 4 | - [c56b189](../../commit/c56b189) [changed] upgraded immutable to v3.0.1 and their API changed a ton 5 | - [ca9a52a](../../commit/ca9a52a) [added] added readme because reading is good 6 | - [de3457c](../../commit/de3457c) [added] added license DONT SUE US PLZ 7 | 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Kuali Co. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # immutable-history 2 | 3 | immutable-history is a library for easily interacting with cursors to 4 | [Immutable.js](https://github.com/facebook/immutable-js) collections while 5 | maintaining a stack of previous states. This is useful for implementing undo 6 | among other things. 7 | 8 | You need to understand the Immutable.js api for cursors and collections for 9 | this to be useful to you. 10 | 11 | It is inspired by ideas from [om](https://github.com/swannodette/om) and works 12 | great with [React](https://github.com/facebook/react) but you can use it for 13 | `(ノ◕ヮ◕)ノ*:・゚✧ A N Y T H I N G (ಥ﹏ಥ)`! 14 | 15 | 16 | ## Installation 17 | 18 | ```bash 19 | npm i --save immutable-history 20 | ``` 21 | 22 | ## Usage 23 | 24 | ```JavaScript 25 | var History = require('immutable-history'); 26 | 27 | // called with the updated cursor whenever the history changes 28 | function render(cursor) { 29 | // pass subcursors from the cursor down through your app hierachy and watch 30 | // the changes magically propogate 31 | console.log('rendering'); 32 | setTimeout(function() { 33 | var powers = cursor.get(['powers']); 34 | if (powers.size < 5) { 35 | powers.update(function(oldValue) { 36 | return oldValue.push('EVEN MORE MUSCLES'); 37 | }); 38 | } else { 39 | // go back to the previous state of the cursor 40 | history.undo() 41 | } 42 | }, 500); 43 | console.log(cursor); 44 | } 45 | 46 | var history = new History({name: 'Jamison', powers: ['flight', 'telekinesis', 'the power to move you']}, render); 47 | ``` 48 | 49 | This creates a history object with an initial state, and a render function that 50 | is called whenever the state is updated. The history object stores a stack 51 | of all previous states, so you can undo back through the previous states. 52 | 53 | Any changes to the data in the cursor will trigger the callback again. This 54 | is useful with react, where you need to re-render some component heirarchy 55 | when your app state changes. 56 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Immutable = require('immutable'); 2 | var Cursor = require('immutable/contrib/cursor'); 3 | var events = require('events') 4 | 5 | var EVENT_CHANGE = 'change' 6 | 7 | function isImmutable(obj) { 8 | return (obj instanceof Immutable.Seq); 9 | } 10 | 11 | function History(immutableCollection, changed) { 12 | 13 | // if constructor wasn't called with new, call it with new! 14 | if (!this instanceof History) { 15 | return new History(immutableCollection, changed); 16 | } 17 | 18 | // accept an immutablejs object or js object 19 | if (!isImmutable(immutableCollection)) { 20 | immutableCollection = Immutable.fromJS(immutableCollection); 21 | } 22 | 23 | // Immutable.List will coerce other data types to a list, and will 24 | // silently fail to wrap a List in another list, so we do it ourselves 25 | this.history = Immutable.List([immutableCollection]); 26 | this.emitter = new events.EventEmitter(); 27 | this.changed = changed; 28 | var self = this; 29 | 30 | this._onChange = function(newData, oldData, path) { 31 | self.history = self.history.push(newData); 32 | self.cursor = Cursor.from(newData, [], self._onChange); 33 | self._emitChange() 34 | } 35 | 36 | // allows this to be passed around 37 | this.onChange = this.onChange.bind(this) 38 | 39 | this.cursor = Cursor.from(immutableCollection, [], self._onChange); 40 | this._emitChange() 41 | } 42 | 43 | History.prototype._emitChange = function() { 44 | this.changed(this.cursor); 45 | this.emitter.emit(EVENT_CHANGE, this.cursor) 46 | } 47 | 48 | 49 | History.prototype.at = function(index) { 50 | return this.history.get(this.history.count() + index - 1); 51 | }; 52 | 53 | History.prototype.previousVersion = function() { 54 | return this.at(-1); 55 | } 56 | 57 | History.prototype.undoUntilData = function(data) { 58 | this.history = this.history.takeWhile(function(v) { 59 | return v != data; 60 | }).toList().push(data); 61 | var newData = data; 62 | this.cursor = Cursor.from(data, [], this._onChange); 63 | self._emitChange() 64 | return data; 65 | } 66 | 67 | History.prototype.undo = function() { 68 | return this.undoUntilData(this.previousVersion()); 69 | } 70 | 71 | History.prototype.onChange = function(handler) { 72 | return this.emitter.on(EVENT_CHANGE, handler); 73 | } 74 | 75 | module.exports = History; 76 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "immutable-history", 3 | "version": "0.2.0", 4 | "author": "Jamison Dance (http://jamisondance.com)", 5 | "repository": "https://github.com/kualico/immutable-history", 6 | "contributors": [ 7 | "Sean Hess (http://seanhess.github.io/)" 8 | ], 9 | "description": "History tracking wrapper for Immutable.js", 10 | "main": "index.js", 11 | "scripts": { 12 | "test": "mocha test/*" 13 | }, 14 | "license": "MIT", 15 | "dependencies": { 16 | "immutable": "3.0.1" 17 | }, 18 | "devDependencies": { 19 | "mocha": "^2.0.1" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var History = require('../'); 3 | var Immutable = require('immutable'); 4 | 5 | describe('History', function() { 6 | it('accepts a js object or an Immutable sequence', function() { 7 | var h = new History([1,2,3,4], function() {}); 8 | var hImmutable = new History(Immutable.fromJS([1,2,3,4]), function(){}); 9 | }); 10 | 11 | it('exposes a cursor to the data', function() { 12 | var data = Immutable.fromJS([1,2,3,4]); 13 | var h = new History(data, function() {}); 14 | var state = h.cursor; 15 | assert(state.deref() == data); 16 | }); 17 | 18 | it('calls the callback with a cursor', function(done) { 19 | var data = Immutable.fromJS([1,2,3,4]); 20 | var h = new History(data, function(c) { 21 | assert.equal(c.deref(), data); 22 | done(); 23 | }); 24 | }); 25 | 26 | describe('when updating the cursor', function() { 27 | it('calls the callback passed in to the constructor', function(done) { 28 | var data = Immutable.fromJS([1,2,3,4]); 29 | var i = 0; 30 | var h = new History(data, function(c) { 31 | 32 | // only do it the second time around, b/c the constructor calls 33 | // this callback 34 | if (i == 1) { 35 | assert.equal(c.deref().get(0), 100); 36 | done(); 37 | } 38 | i++; 39 | }); 40 | h.cursor.update([0], function(old) { 41 | return 100; 42 | }); 43 | }); 44 | }); 45 | 46 | describe('.history', function() { 47 | it('contains the original data when the history starts', function() { 48 | var data = Immutable.fromJS([1,2,3,4]); 49 | var h = new History(data, function() {}); 50 | assert.equal(h.history.get(0), data); 51 | assert.equal(h.history.count(), 1); 52 | }); 53 | }); 54 | 55 | it('appends to this.history when the cursor is changed', function() { 56 | 57 | }); 58 | 59 | it('emits an update event', function(done) { 60 | var data = Immutable.fromJS({key:"value"}) 61 | var h = new History(data, function() {}); 62 | h.onChange(function(cursor) { 63 | assert.equal(cursor.get('key'), "newValue"); 64 | done(); 65 | }) 66 | h.cursor.cursor('key').update(function(v) { 67 | return "newValue"; 68 | }) 69 | }); 70 | }); 71 | 72 | //var data = Immutable.fromJS({a: 1, b: 2, c: [1,2,3]}); 73 | 74 | //var history = new History(data, render); 75 | //var state = history.cursor; 76 | 77 | //var hist = history(data, render) 78 | //hist = history.undo(hist) 79 | 80 | //function render(cursor) { 81 | //console.log(cursor.deref()); 82 | //} 83 | 84 | //state.cursor(['c']).update(function(val) { 85 | //return Immutable.fromJS([1,2]); 86 | //}); 87 | 88 | //history.undo() 89 | --------------------------------------------------------------------------------