question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why are listeners notified before middleware is finished?

See original GitHub issue

I have a bug in my application that I am trying to work out how to fix and the issue goes as follows:

  • An action is triggered
  • I have middleware which records state before and after the action is fired (either side of next(action))
  • I have some React components with a useLayoutEffect() hook that sometimes dispatch actions
  • Some actions seem to trigger immediate renders when calling next() in middleware, which triggers an immediate dispatch and the middleware is not getting the state after the result of the action but its getting the state after the result of the 2nd action, that is the dispatch from useLayoutEffect is occurring in the middle of next(action) and not after the middleware has run.

I am trying to reproduce this in a simplified test case and having a hard time in doing so, what I have determined is that for some actions my application renders immediately when listener() is invoked inside dispatch and for other actions the re-render occurs after the listener and I haven’t yet worked out how to reproduce the first case where the re-render is immediate despite trying here: https://codesandbox.io/s/immutable-architecture-2535n?file=/src/App.tsx

I can’t seem to see any difference in the next(action) call of either action:

Artboard

Regardless my question is why are listeners notified at all until after middleware is finished? Otherwise isn’t there always the risk that a listener dispatches another action and the middleware is unable to act on the finished state of the first action?

https://github.com/reduxjs/redux/blob/b5d07e04364da74cc348eb8398185b304dc3ceb9/src/createStore.ts#L251-L255

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Mar 26, 2021

Sure. If you can manage to come up with any additional unit tests that capture the exact flow of the current behavior, we’d be interested in those even if we don’t end up accepting any changes to the core logic, so we at least have something that would visibly indicate that hypothetical future logic changes are actually changing that flow.

Also note that the current master branch is an as-yet-unreleased TS conversion of the library. You may want to pick up with the 4.x branch, which is still plain JS. Code can get ported either way, of course.

0reactions
georeithcommented, Mar 26, 2021

@markerikson thanks as long as its open to discussion that works for me. I appreciate the fine line you have to tread to keep everyone happy and was why I made this issue to determine whether it was intentional or not and can go from there.

Also appreciate regardless of the original intent it could be seen as a breaking change because someone may be depending on the inverse (I can’t think of the use case but I’m sure they wouldn’t think of mine off the cuff either).

I will take a stab at both approaches and report back.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I capture a "response end" event in node.js+express?
I'm writing a route middleware function. The end handler defines the return code. I want to do things in response to what they...
Read more >
jshttp/on-finished: Execute a callback when a request ... - GitHub
When this module is used on a HTTP CONNECT request, the request is considered "finished" immediately, due to limitations in the Node.js interface....
Read more >
Middleware - Redux
* Sends crash reports as state is updated and listeners are notified. ... * Schedules actions with { meta: { delay: N }...
Read more >
11 Application Events and Event Listener Classes
For HTTP session events, the event listener classes can receive notification when an HTTP session is activated or is about to be passivated,...
Read more >
Redux Toolkit's new listener middleware vs. Redux-Saga
The reason for creating the new listener middleware is to fill that void. In this article, I will compare the new listener middleware...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found