Skip to content
Permalink
Browse files

fix(store): fixed can not reducing pattern.

  • Loading branch information...
rymizuki committed Mar 25, 2016
1 parent 2982b5b commit 637355ee9c256d4eca0ad0f59605cb15bf193792
Showing with 67 additions and 16 deletions.
  1. +9 −8 script/syntagme/store.js
  2. +8 −7 syntagme.js
  3. +50 −1 test/syntagme-reducer-spec.js
@@ -20,8 +20,8 @@ function includes (array, element) {
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
if (element === currentElement ||
(element !== element && currentElement !== currentElement)) { // NaN !== NaN
return true;
}
k++;
@@ -48,15 +48,16 @@ export default class Store {
this.listeners.push(fn)
}
handle (payload) {
var state = null
var current_state = null
for (let i = 0; i < this.reducers.length; i++) {
let previous_state = state || this.state
state = this.reducers[i](payload, previous_state)
let previous_state = current_state || this.state
let state = this.reducers[i](payload, previous_state)
if (state) current_state = state
}
if (this.state != state) {
this.state = state
if (this.state != current_state) {
this.state = current_state
for (let i = 0; i < this.listeners.length; i++) {
this.listeners[i](state)
this.listeners[i](current_state)
}
}
}
@@ -231,7 +231,7 @@ return /******/ (function(modules) { // webpackBootstrap
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement || searchElement !== searchElement && currentElement !== currentElement) {
if (element === currentElement || element !== element && currentElement !== currentElement) {
// NaN !== NaN
return true;
}
@@ -266,15 +266,16 @@ return /******/ (function(modules) { // webpackBootstrap
}, {
key: 'handle',
value: function handle(payload) {
var state = null;
var current_state = null;
for (var i = 0; i < this.reducers.length; i++) {
var previous_state = state || this.state;
state = this.reducers[i](payload, previous_state);
var previous_state = current_state || this.state;
var state = this.reducers[i](payload, previous_state);
if (state) current_state = state;
}
if (this.state != state) {
this.state = state;
if (this.state != current_state) {
this.state = current_state;
for (var _i = 0; _i < this.listeners.length; _i++) {
this.listeners[_i](state);
this.listeners[_i](current_state);
}
}
}
@@ -37,7 +37,56 @@ describe('syntagme.reducer', function () {
})
})
})
describe('multi reduce', function () {
describe('multiple reduce', function () {
var subscriber
beforeEach(function () {
subscriber = sinon.stub()
this.syntagme.subscribe(subscriber)
this.syntagme.reducer(function (payload, previous) {
if (payload.action.type !== 'TEST_1') return
previous || (previous = {})
var data = previous
data.reducer1 = true
data.count = 1
return data
}),
this.syntagme.reducer(function (payload, previous) {
if (payload.action.type !== 'TEST_2') return
previous || (previous = {})
var data = previous
data.reducer2 = true
data.count = 2
return data
})
})
describe('when handled action', function () {
beforeEach(function () {
this.syntagme.dispatch({
source: 'ACTION',
action: { type: 'TEST_1', count: 1, },
})
this.syntagme.dispatch({
source: 'ACTION',
action: { type: 'TEST_2', count: 1, },
})
})
describe('called', function () {
it('should be true', function () {
assert.ok(subscriber.calledOnce)
})
})
describe('state', function () {
it('should be reduced', function () {
assert.deepEqual({
reducer1: true,
reducer2: true,
count: 2,
}, subscriber.args[0][0])
})
})
})
})
describe('order reduce', function () {
var subscriber
beforeEach(function () {
subscriber = sinon.stub()

0 comments on commit 637355e

Please sign in to comment.
You can’t perform that action at this time.