Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. function reactRouter(router) {
  2. return (reducer, initialState) => next => {
  3. const baseStore = next(reducer, initialState);
  4.  
  5. function storeIsInSyncWithRouter() {
  6. //... blah blah implementation details
  7. }
  8.  
  9. // Apply router middleware which intercepts TRANSITION_TO actions
  10. const wrappedDispatch = routerMiddleware(router)()(baseStore.dispatch);
  11.  
  12. const store = {
  13. ...baseStore,
  14. dispatch: (action) => {
  15. wrappedDispatch(action);
  16.  
  17. // After dispatching, check if store state is out of sync with router
  18. if (!storeIsInSyncWithRouter()) {
  19. const { pathname, query, state } = baseStore.getState().router;
  20. // Trigger transition in response to external state change
  21. // E.g. Redux Devtools time travel
  22. router.transitionTo(pathname, query, state);
  23. }
  24.  
  25. return action;
  26. }
  27. };
  28.  
  29. // Need some way to subscribe to transitions
  30. // Different from History.addChangeListener because transitions may be
  31. // aborted, redirected, async, etc.
  32. router.subscribe((location, params, components) => {
  33. // Triggers LOCATION_DID_CHANGE actions
  34. // (probably should rename this to DID_TRANSITION_TO for clarity)
  35. if (!storeIsInSyncWithRouter()) {}
  36. baseStore.dispatch(locationDidChange(location, params));
  37. }
  38. });
  39.  
  40. return store;
  41. };
  42. }
  43.  
  44. // Compose with other higher-order stores
  45. const store = compose(
  46. applyMiddleware(m1, m2, m3)
  47. reactRouter(router),
  48. devTools,
  49. createStore
  50. )(reducer, initialState);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement