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

Use change-emitter module to handle subscriptions #1729

Closed
wants to merge 1 commit into from
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
},
"homepage": "http://redux.js.org",
"dependencies": {
"change-emitter": "^0.1.2",
"lodash": "^4.2.1",
"lodash-es": "^4.2.1",
"loose-envify": "^1.1.0",
Expand Down
36 changes: 4 additions & 32 deletions src/createStore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import isPlainObject from 'lodash/isPlainObject'
import $$observable from 'symbol-observable'
import { createChangeEmitter } from 'change-emitter'

/**
* These are private action types reserved by Redux.
Expand Down Expand Up @@ -56,15 +57,8 @@ export default function createStore(reducer, initialState, enhancer) {

var currentReducer = reducer
var currentState = initialState
var currentListeners = []
var nextListeners = currentListeners
var isDispatching = false

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

/**
* Reads the state tree managed by the store.
Expand Down Expand Up @@ -99,26 +93,7 @@ export default function createStore(reducer, initialState, enhancer) {
* @returns {Function} A function to remove this change listener.
*/
function subscribe(listener) {
if (typeof listener !== 'function') {
throw new Error('Expected listener to be a function.')
}

var isSubscribed = true

ensureCanMutateNextListeners()
nextListeners.push(listener)

return function unsubscribe() {
if (!isSubscribed) {
return
}

isSubscribed = false

ensureCanMutateNextListeners()
var index = nextListeners.indexOf(listener)
nextListeners.splice(index, 1)
}
return emitter.listen(listener)
}

/**
Expand Down Expand Up @@ -172,10 +147,7 @@ export default function createStore(reducer, initialState, enhancer) {
isDispatching = false
}

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

return action
}
Expand Down