Bug 1929079 Comment 3 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

(In reply to Petru-Mugurel Lingurar [:petru] from comment #1)
> I think the middleware needs it's own `dispatch` because inside of middleware instances we don't have access to the store do call `Store::dispatch`.

I was wrong with the above.
The middleware https://searchfox.org/mozilla-central/rev/aecb006bbb135d707ca4b8cea7572dd8abab6817/mobile/android/android-components/components/lib/state/src/main/java/mozilla/components/lib/state/Middleware.kt#52, it's just that if we call Store::dispatch on this the new action will be added to the queue
While if we call `MiddlewareContext::dispatch` the new action will be put at the front of a new middlewares/reducer chain - effectively "interrupting" the evaluation of the initial action.

I feel though that this is an important functionality, [in line with Redux](https://redux.js.org/tutorials/fundamentals/part-4-store#middleware) if we look at the "Redux middleware are written as a series of three nested functions" section where we see
```
return function handleAction(action) {
      // Do anything here: pass the action onwards with next(action),
      // or restart the pipeline with storeAPI.dispatch(action)
```
and
> If you call this dispatch function, it will send the action to the start of the middleware pipeline.

Which seem to describe exactly what our `MiddlewareContext::dispatch` does
(In reply to Petru-Mugurel Lingurar [:petru] from comment #1)
> I think the middleware needs it's own `dispatch` because inside of middleware instances we don't have access to the store do call `Store::dispatch`.

I was wrong with the above.
The middleware [exposes the store to which it is applied](https://searchfox.org/mozilla-central/rev/aecb006bbb135d707ca4b8cea7572dd8abab6817/mobile/android/android-components/components/lib/state/src/main/java/mozilla/components/lib/state/Middleware.kt#52), it's just that if we call `Store::dispatch` the new action will be added to the queue
While if we call `MiddlewareContext::dispatch` the new action will be put at the front of a new middlewares/reducer chain - effectively "interrupting" the evaluation of the initial action.

I feel though that this is an important functionality, [in line with Redux](https://redux.js.org/tutorials/fundamentals/part-4-store#middleware) if we look at the "Redux middleware are written as a series of three nested functions" section where we see
```
return function handleAction(action) {
      // Do anything here: pass the action onwards with next(action),
      // or restart the pipeline with storeAPI.dispatch(action)
```
and
> If you call this dispatch function, it will send the action to the start of the middleware pipeline.

Which seem to describe exactly what our `MiddlewareContext::dispatch` does

Back to Bug 1929079 Comment 3