Skip to content

Commit

Permalink
avoid useless calls to listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
splendido committed Jul 26, 2016
1 parent cb5a00b commit eb5a5ea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
19 changes: 15 additions & 4 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export default function createStore(reducer, preloadedState, enhancer) {
var currentState = preloadedState
var currentListeners = []
var nextListeners = currentListeners
var listeners = []
var listenerId = 0
var isDispatching = false

function ensureCanMutateNextListeners() {
Expand Down Expand Up @@ -140,8 +142,14 @@ export default function createStore(reducer, preloadedState, enhancer) {
isSubscribed = false

ensureCanMutateNextListeners()
var index = nextListeners.indexOf(listener)
let index = nextListeners.indexOf(listener)
nextListeners.splice(index, 1)

index = listeners.indexOf(listener)
if (index !== -1 && index > listenerId) {
listener()
listeners.splice(index, 1)
}
}
}

Expand Down Expand Up @@ -196,10 +204,13 @@ export default function createStore(reducer, preloadedState, enhancer) {
isDispatching = false
}

var listeners = currentListeners = nextListeners
for (var i = 0; i < listeners.length; i++) {
listeners[i]()
listeners = currentListeners = nextListeners
for (listenerId = 0; listenerId < listeners.length; listenerId++) {
listeners[listenerId]()
}
currentListeners = nextListeners
listeners = []
listenerId = 0

return action
}
Expand Down
40 changes: 31 additions & 9 deletions test/createStore.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import expect from 'expect'
import { createStore, combineReducers } from '../src/index'
import {
addTodo,
dispatchInMiddle,
import {
addTodo,
dispatchInMiddle,
getStateInMiddle,
subscribeInMiddle,
unsubscribeInMiddle,
throwError,
unknownAction
throwError,
unknownAction
} from './helpers/actionCreators'
import * as reducers from './helpers/reducers'
import * as Rx from 'rxjs'
Expand Down Expand Up @@ -388,18 +388,40 @@ describe('createStore', () => {

store.dispatch(unknownAction())
expect(listener1.calls.length).toBe(1)
expect(listener2.calls.length).toBe(2)
expect(listener3.calls.length).toBe(2)
expect(listener2.calls.length).toBe(1)
expect(listener3.calls.length).toBe(1)
expect(listener4.calls.length).toBe(1)

unsubscribe4()
store.dispatch(unknownAction())
expect(listener1.calls.length).toBe(1)
expect(listener2.calls.length).toBe(3)
expect(listener3.calls.length).toBe(3)
expect(listener2.calls.length).toBe(2)
expect(listener3.calls.length).toBe(2)
expect(listener4.calls.length).toBe(1)
})

it('notifies a listener unsubscribed with nested dispatch with the new state', () => {
const store = createStore(reducers.todos)

var firstCall = true
const listener1 = expect.createSpy().andCall(() => {
unsubscribe2()
if (firstCall) {
firstCall = false
store.dispatch(unknownAction())
}
})
const listener2 = expect.createSpy()

store.subscribe(listener1)
let unsubscribe2 = store.subscribe(listener2)

store.dispatch(unknownAction())
expect(listener1.calls.length).toBe(2)
expect(listener2.calls.length).toBe(1)
})


it('provides an up-to-date state when a subscriber is notified', done => {
const store = createStore(reducers.todos)
store.subscribe(() => {
Expand Down

0 comments on commit eb5a5ea

Please sign in to comment.