{"action":"opened","issue":{"url":"https://api.github.com/repos/mxstbr/react-boilerplate/issues/1183","repository_url":"https://api.github.com/repos/mxstbr/react-boilerplate","labels_url":"https://api.github.com/repos/mxstbr/react-boilerplate/issues/1183/labels{/name}","comments_url":"https://api.github.com/repos/mxstbr/react-boilerplate/issues/1183/comments","events_url":"https://api.github.com/repos/mxstbr/react-boilerplate/issues/1183/events","html_url":"https://github.com/mxstbr/react-boilerplate/issues/1183","id":187028672,"number":1183,"title":"Run saga policy (prevent saga duplication in more flexible way)","user":{"login":"afitiskin","id":204739,"avatar_url":"https://avatars.githubusercontent.com/u/204739?v=3","gravatar_id":"","url":"https://api.github.com/users/afitiskin","html_url":"https://github.com/afitiskin","followers_url":"https://api.github.com/users/afitiskin/followers","following_url":"https://api.github.com/users/afitiskin/following{/other_user}","gists_url":"https://api.github.com/users/afitiskin/gists{/gist_id}","starred_url":"https://api.github.com/users/afitiskin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/afitiskin/subscriptions","organizations_url":"https://api.github.com/users/afitiskin/orgs","repos_url":"https://api.github.com/users/afitiskin/repos","events_url":"https://api.github.com/users/afitiskin/events{/privacy}","received_events_url":"https://api.github.com/users/afitiskin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-11-03T10:58:00Z","updated_at":"2016-11-03T10:58:00Z","closed_at":null,"body":"**Feature request**\r\n\r\nHello guys. This topic is not new. I've checked issues #390, #637 and #888 and have a few ideas how to improve proposed solutions.\r\n\r\nSo, I like the idea and solution of preventing saga duplication proposed by @kraffslol at #390 (by changing saga injector), but this solution was rejected. The point is:\r\n> Sagas themselves should manage their lifecycle \r\n\r\nand proposal solution is to cancel watchers on `LOCATION_CHANGE` action:\r\n```javascript\r\nyield take(LOCATION_CHANGE);\r\nyield cancel(watcher);\r\n```\r\n\r\nBut in some cases this way have problems. #888 is an example when new saga should not be run automatically, because we would like to handle this kind of location change action in previously run saga. To do that we have to tweak async injector (in other case we well have saga duplicates that will ruin our application).\r\n\r\nLong story short, what about to have a combined solution? To do that we can add some kind of saga run policy. Injector should check this policy and run (or not run) each saga. This way has benefits of both solutions and can handle all possible saga-usage cases.\r\n\r\nCode example to illustrate my idea:\r\n```javascript\r\n// some container's sagas.js\r\n\r\nexport function* daemonSaga() {\r\n  // run only once on load\r\n}\r\n\r\nexport function* disposableSaga() {\r\n  // run on each page enter\r\n}\r\ndisposableSaga.runAlways = true;\r\n\r\nexport [\r\n  daemonSaga,\r\n  disposableSaga,\r\n];\r\n\r\n// store.js\r\n// ... much code here\r\nstore.asyncReducers = {};\r\nstore.asyncSagas = {};\r\nreturn store;\r\n\r\n// asyncInjectors.js\r\nexport function injectAsyncSagas(store, isValid) {\r\n  return function injectSagas(sagas) {\r\n    // invariant and warning here...\r\n  \r\n    sagas.forEach((saga) => {\r\n      if (!store.asyncSagas[saga] || saga.runAlways === true) {\r\n        store.runSaga(saga);\r\n        store.asyncSagas[saga] = true;\r\n      }\r\n    });\r\n  }\r\n}\r\n```\r\n\r\nWhat do you think?"}}
{"actor":{"display_login":"afitiskin"}}