Dissecting the applyMiddleware Function<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Dissecting the applyMiddleware Function

Posted on: January 16, 2018

applyMiddleware is probably the most popular store enhancer[1]. Most people don't realize that a middleware is a concept built as a store enhancer and in this article, we will see how the concept of middleware leverages Redux's store enhancer hook on the store to allows creating custom code to be executed between the dispatch of an action and the time the action reaches the reducers.

The applyMiddleware is called when the store is created by the createStore function[2]. The first parameter are the reducers, and the second parameter is the composition of store enhancer. applyMiddleware is most of the time called with the compose function and allows to setup many middlewares. Here is a basic setup with createStore where we can see how the applyMiddleware is invoked.

1const store = createStore( reducers, compose( applyMiddleware(middleware1, middleware2, middleware2), aSecondStoreEnhancerHere ) )

If we recall from the article entitled "dissecting the createstore"[2], the createStore function call at some point the composition of enhancers like the code below this paragraph. For the sake of simplicity, we will assume that we are not composing store enhancer but only have one which will be the applyMiddleware. The only difference with the composition is that the composition would be called one after the other one in sequence. The main idea to grasp at the moment is not the possibility of calling several enhancers one after the other but that the enhancer calls the store and that the return function of the enhancer is called with the reducers and the preloaded state.

1return enhancer(createStore)(reducer, preloadedState)

This is crucial to visualize. The enhancer is a function that takes as a parameter the store and outputs a function that will be executed by the createStore with two parameters which is the reducer and preloaded state. This function will return at its turn the store and how to dispatch action to this one.

This is why the applyMiddleware has the following shapes:

1export default function applyMiddleware(...middlewares) {
2 return (createStore) => (...args) => {
3 // Code removed for brevity
4 return { ...store, dispatch };
5 }

If we deconstruct or de-generalize the code of the createstore to be using directly the applyMiddleware store enhancer we would end up with the following code.

1return applyMiddleware(createStore)(reducer, preloadedState)
5const createStoreFromApplyMiddleware = applyMiddleware(createStore);
6return createStoreFromApplyMiddleware(reducer, preloadedState);

The applyMiddleware code first task is to call the actual createStore function which returns the store. The second task is to get a reference to the dispatch function. This will be used in the third task which is to create the middlewareAPI object who is sent to every middleware.

1const store = createStore(...args);
2let dispatch = store.dispatch;
3let chain = [];
5const middlewareAPI = {
6 getState: store.getState,
7 dispatch: (...args) => dispatch(...args)
9// To be continued, removed for brivety

Since applyMiddleware is having a collection of middleware, the store applyMiddleware store enhancer will call each of them with the middlewareAPI and return all the middleware return values into a chain of result. The chain of results is then composed and invoked with the store's dispatch which allows every middleware to be executed with a next argument that will move the next composed middleware and still have them access the middlewareAPI which contains the dispatch of the store and the current state of the reducer.

1chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch)

At the end, the whole store enhancer is about 20 lines of code.

1export default function applyMiddleware(...middlewares) {
2 return (createStore) =>
3 (...args) => {
4 const store = createStore(...args);
5 let dispatch = store.dispatch;
6 let chain = [];
8 const middlewareAPI = {
9 getState: store.getState,
10 dispatch: (...args) => dispatch(...args)
11 };
12 chain = middlewares.map(middleware => middleware(middlewareAPI));
13 dispatch = compose(...chain)(store.dispatch);
15 return { ...store, dispatch }
16 }

As we can see, it's possible to have different behavior like altering the dispatch function in the actual case with the middleware. The function is actually called once at the creation of the store which set up the chain to be called once a dispatch is executed. Since the code is overriding the dispatch and returning a wrapper of composed functions (middleware), every time something is dispatched it will be pass-through all the middleware. The capability to alter dispatch in a store enhancer allows injecting code to be executed after a store[3] is being modified. The code below is an excerpt that illustrates how the compose function could be replaced and used to be returned allowing a hook function.

1function dispatch(...args) {
2 const dispatchResult = store.dispatch(dispatchArgs);
3 hook(notifyListeners, store);
4 return dispatchResult;

In this article, we have brushed the store enhancer of middleware. In the next article, we will see how middlewares actual work and see in detail how the popular Thunk middleware brings asynchronous capability.