{"action":"created","issue":{"url":"https://api.github.com/repos/rackt/redux/issues/922","labels_url":"https://api.github.com/repos/rackt/redux/issues/922/labels{/name}","comments_url":"https://api.github.com/repos/rackt/redux/issues/922/comments","events_url":"https://api.github.com/repos/rackt/redux/issues/922/events","html_url":"https://github.com/rackt/redux/issues/922","id":112583272,"number":922,"title":"Proposal: add `subscribe` to middlewareAPI","user":{"login":"jonnyreeves","id":200185,"avatar_url":"https://avatars.githubusercontent.com/u/200185?v=3","gravatar_id":"","url":"https://api.github.com/users/jonnyreeves","html_url":"https://github.com/jonnyreeves","followers_url":"https://api.github.com/users/jonnyreeves/followers","following_url":"https://api.github.com/users/jonnyreeves/following{/other_user}","gists_url":"https://api.github.com/users/jonnyreeves/gists{/gist_id}","starred_url":"https://api.github.com/users/jonnyreeves/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonnyreeves/subscriptions","organizations_url":"https://api.github.com/users/jonnyreeves/orgs","repos_url":"https://api.github.com/users/jonnyreeves/repos","events_url":"https://api.github.com/users/jonnyreeves/events{/privacy}","received_events_url":"https://api.github.com/users/jonnyreeves/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-10-21T13:04:43Z","updated_at":"2015-10-21T21:57:22Z","closed_at":"2015-10-21T20:57:55Z","body":"Exapand the existing `middlewareAPI` to include `store.subscribe`, ie:\r\n\r\n```js\r\nvar middlewareAPI = {\r\n  getState: store.getState,\r\n  dispatch: (action) => dispatch(action),\r\n  subscribe: store.subscribe\r\n};\r\n```\r\n\r\n### Motivation\r\nStateful middleware implementations wish to observe the store to know when the store' state has been modified and trigger a side-effect; for example - OAuth middleware will detain `CALL_API` actions when it detects an expired OAuth Grant until the store has been updated with a new, valid Grant:\r\n\r\n```js\r\nexport function createMiddleware() {\r\n  var detainedActions = [];\r\n\r\n  return store => next => action => {\r\n      if (!action[CALL_API]) {\r\n        return next(action);\r\n      }\r\n      if (store.getState().oauthGrant.expired) {\r\n        detainedActions.push(action);\r\n        await obtainNewOauthGrant(store);\r\n        redispatchDetainedActions(store.dispatch);\r\n      } else {\r\n        // ... sign the request.\r\n    }\r\n  }  \r\n\r\n  async function obtainNewOauthGrant(store) {\r\n    const unsubscribe = store.subscribe(() => {\r\n      if (!store.getState().oauthGrant.expired) {\r\n        unsubscribe();\r\n        resolve();\r\n      }\r\n    });\r\n    store.dispatch(obtainNewGrant());\r\n  }\r\n\r\n  function redispatchDetainedActions(dispatch) {\r\n    // ... redispatch detained actions.\r\n  }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/rackt/redux/issues/comments/150036708","html_url":"https://github.com/rackt/redux/issues/922#issuecomment-150036708","issue_url":"https://api.github.com/repos/rackt/redux/issues/922","id":150036708,"user":{"login":"acdlite","id":3624098,"avatar_url":"https://avatars.githubusercontent.com/u/3624098?v=3","gravatar_id":"","url":"https://api.github.com/users/acdlite","html_url":"https://github.com/acdlite","followers_url":"https://api.github.com/users/acdlite/followers","following_url":"https://api.github.com/users/acdlite/following{/other_user}","gists_url":"https://api.github.com/users/acdlite/gists{/gist_id}","starred_url":"https://api.github.com/users/acdlite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/acdlite/subscriptions","organizations_url":"https://api.github.com/users/acdlite/orgs","repos_url":"https://api.github.com/users/acdlite/repos","events_url":"https://api.github.com/users/acdlite/events{/privacy}","received_events_url":"https://api.github.com/users/acdlite/received_events","type":"User","site_admin":false},"created_at":"2015-10-21T21:57:22Z","updated_at":"2015-10-21T21:57:22Z","body":"@jonnyreeves It may help to know that `applyMiddleware()` itself is a store enhancer :) https://github.com/rackt/redux/blob/master/src/utils/applyMiddleware.js"}}