Skip to content
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

Simplify createStore(), remove nextListeners & ensureCanMutateNextListeners() #2376

Closed
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
18 changes: 5 additions & 13 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,8 @@ export default function createStore(reducer, preloadedState, enhancer) {
let currentReducer = reducer
let currentState = preloadedState
let currentListeners = []
let nextListeners = currentListeners
let isDispatching = false

function ensureCanMutateNextListeners() {
if (nextListeners === currentListeners) {
nextListeners = currentListeners.slice()
}
}

/**
* Reads the state tree managed by the store.
*
Expand Down Expand Up @@ -105,8 +98,7 @@ export default function createStore(reducer, preloadedState, enhancer) {

let isSubscribed = true

ensureCanMutateNextListeners()
nextListeners.push(listener)
currentListeners = currentListeners.concat(listener)

return function unsubscribe() {
if (!isSubscribed) {
Expand All @@ -115,9 +107,9 @@ export default function createStore(reducer, preloadedState, enhancer) {

isSubscribed = false

ensureCanMutateNextListeners()
const index = nextListeners.indexOf(listener)
nextListeners.splice(index, 1)
currentListeners = currentListeners.slice()
const index = currentListeners.indexOf(listener)
currentListeners.splice(index, 1)
}
}

Expand Down Expand Up @@ -172,7 +164,7 @@ export default function createStore(reducer, preloadedState, enhancer) {
isDispatching = false
}

const listeners = currentListeners = nextListeners
const listeners = currentListeners
for (let i = 0; i < listeners.length; i++) {
const listener = listeners[i]
listener()
Expand Down