{"action":"created","issue":{"url":"https://api.github.com/repos/markerikson/redux-starter-kit/issues/14","repository_url":"https://api.github.com/repos/markerikson/redux-starter-kit","labels_url":"https://api.github.com/repos/markerikson/redux-starter-kit/issues/14/labels{/name}","comments_url":"https://api.github.com/repos/markerikson/redux-starter-kit/issues/14/comments","events_url":"https://api.github.com/repos/markerikson/redux-starter-kit/issues/14/events","html_url":"https://github.com/markerikson/redux-starter-kit/issues/14","id":303163194,"number":14,"title":"createDefaultMiddleware Semantics","user":{"login":"hnordt","id":1625399,"avatar_url":"https://avatars2.githubusercontent.com/u/1625399?v=4","gravatar_id":"","url":"https://api.github.com/users/hnordt","html_url":"https://github.com/hnordt","followers_url":"https://api.github.com/users/hnordt/followers","following_url":"https://api.github.com/users/hnordt/following{/other_user}","gists_url":"https://api.github.com/users/hnordt/gists{/gist_id}","starred_url":"https://api.github.com/users/hnordt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hnordt/subscriptions","organizations_url":"https://api.github.com/users/hnordt/orgs","repos_url":"https://api.github.com/users/hnordt/repos","events_url":"https://api.github.com/users/hnordt/events{/privacy}","received_events_url":"https://api.github.com/users/hnordt/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2018-03-07T16:17:58Z","updated_at":"2018-03-07T19:44:35Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"I would like to suggest a change in the way we support custom middleware because `createDefaultMiddleware`, at least for me, sounds confusing. You are not actually creating a default middleware, you are applying your own middleware while keeping the library defaults.\r\n\r\nInstead of:\r\n\r\n```js\r\nconst middleware = createDefaultMiddleware(logger);\r\nconst store = configureStore({ middleware });\r\n```\r\n\r\nWe could support passing a function to middleware and receiving the default middleware as a param:\r\n\r\n```js\r\nconst middleware = defaultMiddleware => [defaultMiddleware, logger];\r\nconst store = configureStore({ middleware });\r\n```\r\n\r\nThis also allows the consumer to control the order of defaultMiddleware in the chain, which might be useful for some people.\r\n\r\n`defaultMiddleware` would be an object, so in the future we if add more middlewares we could let the user choose the ones he wants:\r\n\r\n```js\r\nconst middleware = ({ thunk, saga }) => [thunk, saga, logger];\r\nconst store = configureStore({ middleware });\r\n```\r\n\r\nThe implementation for `middleware` argument would be something like:\r\n\r\n```js\r\nexport function configureStore(options = {}) {\r\n    const {\r\n        reducer,\r\n        devTools = true,\r\n        preloadedState,\r\n        enhancers = [],\r\n    } = options;\r\n\r\n    let { middleware } = options;\r\n    middleware = parseMiddleware(middleware);\r\n\r\n    // ..\r\n}\r\n\r\nconst defaultMiddleware = { thunk, saga }\r\n\r\nfunction parseMiddleware(middleware) {\r\n    if (middleware === undefined) {\r\n      // The order for our own defaultMiddleware doesn't matter, so that's not an issue.\r\n      return Object.values(defaultMiddleware)\r\n    }\r\n\r\n    if (Array.isArray(middleware)) {\r\n        return middleware\r\n    }\r\n\r\n    if (typeof middleware === \"function\") {\r\n      // Here we need to check for plain objects. The reason is that defaultMiddleware is\r\n      // an object to support destructuring when the consumer wants to select specify\r\n      // middlewares from the defaultMiddleware.\r\n      // But if the consumer uses defaultMiddleware as is, we need to convert it\r\n      // to an array.\r\n      // The order for our own defaultMiddleware doesn't matter, so that's not an issue.\r\n      return middleware(defaultMiddleware)\r\n        .reduce(\r\n          (result, middleware) =>\r\n             isPlainObject(middleware)\r\n               ? [result, ...Object.values(middleware)]\r\n               : [result, middleware],\r\n          []\r\n        )\r\n    }\r\n\r\n    throw new Error(\"`middleware` should be an array or function\")\r\n}\r\n```\r\n\r\nThere is a potential issue where users could think that they would be allowed to pass middleware as objects:\r\n\r\n```js\r\nconst apiMiddleware = { todoMiddleware, userMiddleware, ... }\r\n\r\nconst store = configureStore({\r\n  middleware: defaultMiddleware => [defaultMiddleware, apiMiddleware]\r\n})\r\n```\r\n\r\nIt's not really an issue as long as you don't expect the order of `apiMiddleware` to be maintained. But that's something I didn't like, I don't know how to improve this. Help is welcome."},"comment":{"url":"https://api.github.com/repos/markerikson/redux-starter-kit/issues/comments/371260597","html_url":"https://github.com/markerikson/redux-starter-kit/issues/14#issuecomment-371260597","issue_url":"https://api.github.com/repos/markerikson/redux-starter-kit/issues/14","id":371260597,"user":{"login":"hnordt","id":1625399,"avatar_url":"https://avatars2.githubusercontent.com/u/1625399?v=4","gravatar_id":"","url":"https://api.github.com/users/hnordt","html_url":"https://github.com/hnordt","followers_url":"https://api.github.com/users/hnordt/followers","following_url":"https://api.github.com/users/hnordt/following{/other_user}","gists_url":"https://api.github.com/users/hnordt/gists{/gist_id}","starred_url":"https://api.github.com/users/hnordt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hnordt/subscriptions","organizations_url":"https://api.github.com/users/hnordt/orgs","repos_url":"https://api.github.com/users/hnordt/repos","events_url":"https://api.github.com/users/hnordt/events{/privacy}","received_events_url":"https://api.github.com/users/hnordt/received_events","type":"User","site_admin":false},"created_at":"2018-03-07T19:44:35Z","updated_at":"2018-03-07T19:44:35Z","author_association":"CONTRIBUTOR","body":"@hswolff I think he wanted to say freeze. Object.freeze()"}}
{"actor":{"display_login":"hnordt"}}