Skip to content

Avoid unnecessary calls to listeners #1867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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