Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Pass custom store enhancer and middleware to redux store #93

Closed
thangngoc89 opened this issue Jan 24, 2016 · 4 comments
Closed

Pass custom store enhancer and middleware to redux store #93

thangngoc89 opened this issue Jan 24, 2016 · 4 comments

Comments

@thangngoc89
Copy link
Contributor

Some basic function that I could build when statinamic have this feature:

I could send a PR for this. But I'm waiting for #91

@MoOx
Copy link
Owner

MoOx commented Jan 24, 2016

Two things:

@thangngoc89
Copy link
Contributor Author

have you any idea about a nice flexible way to allow that

Allow 2 extra args in createStore function:

A proof of concept

// https://github.com/MoOx/statinamic/blob/master/src/redux/createStore/index.js#L4
import { createStore, applyMiddleware, compose } from "redux"
import thunk from "redux-thunk"

export default function(reducer = {}, initialState = {}, extraMiddlewares = {}, storeEnhancers = {}) {
  function promiseMiddleware() {
    return (next) => (action) => {
      const { promise, types, ...rest } = action
      if (!promise) {
        return next(action)
      }
      else if (!promise.then) {
        throw new Error(
          "promiseMiddleware expects a promise object that implements then()"
        )
      }

      const [ REQUEST, SUCCESS, FAILURE ] = types
      next({ ...rest, type: REQUEST })
      return promise.then(
        (response) => next({ ...rest, response, type: SUCCESS }),
        (error) => next({ ...rest, error, type: FAILURE })
      )
    }
  }

  let finalCreateStore

  if (__DEVTOOLS__) {
    const { devTools, persistState } = require("redux-devtools")

    finalCreateStore = compose(
      applyMiddleware(promiseMiddleware, thunk, ...extraMiddlewares),
      devTools(),
      persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/)),
      ...storeEnhancers
    )(createStore)
  }
  else {
    finalCreateStore = applyMiddleware(promiseMiddleware, thunk)(createStore)
  }

  return finalCreateStore(reducer, initialState)
}

```js

@MoOx
Copy link
Owner

MoOx commented Jan 24, 2016

Well, why not.

@thangngoc89
Copy link
Contributor Author

Close ba16912

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

No branches or pull requests

2 participants