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

isDispatching really needed? #1668

Closed
anler opened this issue Apr 26, 2016 · 3 comments
Closed

isDispatching really needed? #1668

anler opened this issue Apr 26, 2016 · 3 comments

Comments

@anler
Copy link

anler commented Apr 26, 2016

Hi! Just a comment/question about something I've seen in the dispatch's code in createStore.js (see the code below). I was wondering why is variable isDispatching needed, if there are two concurrent calls to dispatch the isDispatching check is pointless because is not inside a mutex:

image

But as far as I know, JS' runtime can only execute one thing/function at a time so two concurrent calls to dispatch will never happen and if that's the case I don't see the need for isDispatching right?

Can you tell me if I'm missing something? Thanks!

https://github.com/reactjs/redux/blob/master/src/createStore.js#L149

function dispatch(action) {
  // ...
  if (isDispatching) {
    throw new Error('Reducers may not dispatch actions.')
  }

  try {
    isDispatching = true
    currentState = currentReducer(currentState, action)
  } finally {
    isDispatching = false
  }
  // ...
}
@brigand
Copy link
Contributor

brigand commented Apr 26, 2016

It catches this case, dispatching from within a reducer.

var store = createStore((state={}, action) => {
  if (something) {
    store.dispatch({type: 'ANOTHER_ACTION'})
  }
  return state
})

@anler
Copy link
Author

anler commented Apr 26, 2016

Oh I see, now is clear, is dispatching from within a reducer forbidden to avoid things such as loops (reducer -> dispatch -> reducer ->...) am I right?

Thanks!

@anler anler closed this as completed Apr 26, 2016
@gaearon
Copy link
Contributor

gaearon commented Apr 26, 2016

Yep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants