Comparing version 0.0.3 to 0.1.0
@@ -115,3 +115,9 @@ (function() { | ||
} | ||
merge(__state, state); | ||
// Poor man's inefficient but strict & safe change check. I know. | ||
var changed = Object.keys(state).some(function(prop) { | ||
return !__state.hasOwnProperty(prop) || | ||
state[prop] !== __state[prop]; | ||
}); | ||
if (!changed) return; | ||
__state = merge({}, __state, state); | ||
__listeners.forEach(function(listener) { | ||
@@ -118,0 +124,0 @@ listener(__state); |
{ | ||
"name": "docbrown", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Flux experiment.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -169,3 +169,3 @@ DocBrown | ||
A working demo is available in the `demo/` directory in this repository. | ||
A working demo is available in the `demo/` directory in this repository and [on JSBin](http://jsbin.com/meholi/2/edit). | ||
@@ -172,0 +172,0 @@ Install |
81
test.js
@@ -284,3 +284,3 @@ var DocBrown = require("./"); | ||
it("should notify subscribers", function() { | ||
it("should notify subscribers if state has actually changed", function() { | ||
var Store = DocBrown.createStore({ | ||
@@ -303,2 +303,58 @@ actions: [Actions], | ||
}); | ||
it("should not notify subscribers if state hasn't changed", function() { | ||
var Store = DocBrown.createStore({ | ||
actions: [Actions], | ||
getInitialState: function() {return {foo: 42};} | ||
}); | ||
var store = new Store(); | ||
var subscriber = sinon.spy(); | ||
store.subscribe(subscriber); | ||
store.setState({foo: 42}); | ||
sinon.assert.notCalled(subscriber); | ||
}); | ||
describe("state", function() { | ||
function setup(currentState, newState) { | ||
var Store = DocBrown.createStore({ | ||
actions: [Actions], | ||
getInitialState: function() { | ||
return currentState; | ||
} | ||
}); | ||
var store = new Store(); | ||
var subscriber = sinon.spy(); | ||
store.subscribe(subscriber); | ||
store.setState(newState); | ||
return subscriber.getCall(0); | ||
} | ||
function expectNotUpdated(currentState, newState) { | ||
if (!!setup(currentState, newState)) { | ||
throw new Error("state updated"); | ||
} | ||
} | ||
describe("should not be updated when", function() { | ||
it("a number prop hasn't changed", function() { | ||
expectNotUpdated({a: 1}, {a: 1}); | ||
}); | ||
it("a boolean prop hasn't changed", function() { | ||
expectNotUpdated({a: false}, {a: false}); | ||
}); | ||
it("a float prop hasn't changed", function() { | ||
expectNotUpdated({a: 0.2 + 0.1}, {a: 0.2 + 0.1}); | ||
}); | ||
it("a string prop hasn't changed", function() { | ||
expectNotUpdated({a: "1"}, {a: "1"}); | ||
}); | ||
it("state objects don't differ", function() { | ||
expectNotUpdated({a: 1, b: 2}, {b: 2, a: 1}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -314,18 +370,21 @@ | ||
it("should register a new change listener", function(done) { | ||
store.subscribe(function() { | ||
done(); | ||
}); | ||
it("should register a new change listener", function() { | ||
var spy = sinon.spy(); | ||
store.setState({}); | ||
store.subscribe(spy); | ||
store.setState({a: 1}); | ||
sinon.assert.calledOnce(spy); | ||
}); | ||
it("should notify subscribers with new state", function(done) { | ||
it("should notify subscribers with new state", function() { | ||
var newState = {foo: "bar"}; | ||
store.subscribe(function(state) { | ||
expect(state === newState); | ||
done(); | ||
}); | ||
var spy = sinon.spy(); | ||
store.subscribe(spy); | ||
store.setState(newState); | ||
sinon.assert.calledWithExactly(spy, newState); | ||
}); | ||
@@ -332,0 +391,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27369
554