{"action":"created","comment":{"url":"https://api.github.com/repos/jamiebuilds/unstated/pulls/comments/171713105","pull_request_review_id":100616284,"id":171713105,"diff_hunk":"@@ -13,8 +13,20 @@ export class Container<State: {}> {\n     this._listeners = [];\n   }\n \n-  setState(state: $Shape<State>) {\n-    this.state = Object.assign({}, this.state, state);\n+  setState(state: $Shape<State> | (State => $Shape<State>)): void {\n+    const prevState = this.state;\n+    const newState = typeof state === 'function' ? state(prevState) : state;\n+\n+    /**\n+     * allow either early return or ternary return of previous state to\n+     * propagate correctly through component tree by not calling _listeners\n+     * and leaving old state in place.\n+     */\n+    if (newState == null || newState === prevState) {","path":"src/unstated.js","position":15,"original_position":15,"commit_id":"ff77c96e775d39d7746773c67e37f1d2e97afba6","original_commit_id":"ff77c96e775d39d7746773c67e37f1d2e97afba6","user":{"login":"jamiebuilds","id":952783,"avatar_url":"https://avatars0.githubusercontent.com/u/952783?v=4","gravatar_id":"","url":"https://api.github.com/users/jamiebuilds","html_url":"https://github.com/jamiebuilds","followers_url":"https://api.github.com/users/jamiebuilds/followers","following_url":"https://api.github.com/users/jamiebuilds/following{/other_user}","gists_url":"https://api.github.com/users/jamiebuilds/gists{/gist_id}","starred_url":"https://api.github.com/users/jamiebuilds/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamiebuilds/subscriptions","organizations_url":"https://api.github.com/users/jamiebuilds/orgs","repos_url":"https://api.github.com/users/jamiebuilds/repos","events_url":"https://api.github.com/users/jamiebuilds/events{/privacy}","received_events_url":"https://api.github.com/users/jamiebuilds/received_events","type":"User","site_admin":false},"body":"I want it to match the React behaviour as closely as possible. Refactoring component state into container state should be near seamless","created_at":"2018-03-01T22:22:43Z","updated_at":"2018-03-01T22:22:43Z","html_url":"https://github.com/jamiebuilds/unstated/pull/1#discussion_r171713105","pull_request_url":"https://api.github.com/repos/jamiebuilds/unstated/pulls/1","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/jamiebuilds/unstated/pulls/comments/171713105"},"html":{"href":"https://github.com/jamiebuilds/unstated/pull/1#discussion_r171713105"},"pull_request":{"href":"https://api.github.com/repos/jamiebuilds/unstated/pulls/1"}},"in_reply_to_id":171430237},"pull_request":{"url":"https://api.github.com/repos/jamiebuilds/unstated/pulls/1","id":166552305,"html_url":"https://github.com/jamiebuilds/unstated/pull/1","diff_url":"https://github.com/jamiebuilds/unstated/pull/1.diff","patch_url":"https://github.com/jamiebuilds/unstated/pull/1.patch","issue_url":"https://api.github.com/repos/jamiebuilds/unstated/issues/1","number":1,"state":"open","locked":false,"title":"Allow callback style setState","user":{"login":"StevenLangbroek","id":296796,"avatar_url":"https://avatars0.githubusercontent.com/u/296796?v=4","gravatar_id":"","url":"https://api.github.com/users/StevenLangbroek","html_url":"https://github.com/StevenLangbroek","followers_url":"https://api.github.com/users/StevenLangbroek/followers","following_url":"https://api.github.com/users/StevenLangbroek/following{/other_user}","gists_url":"https://api.github.com/users/StevenLangbroek/gists{/gist_id}","starred_url":"https://api.github.com/users/StevenLangbroek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/StevenLangbroek/subscriptions","organizations_url":"https://api.github.com/users/StevenLangbroek/orgs","repos_url":"https://api.github.com/users/StevenLangbroek/repos","events_url":"https://api.github.com/users/StevenLangbroek/events{/privacy}","received_events_url":"https://api.github.com/users/StevenLangbroek/received_events","type":"User","site_admin":false},"body":"This PR allows the \"callback\" style `setState`, which React also allows. Where React allows it for asynchronicity, the change here would allow advanced users who are aware of (im)mutability concerns (which you take care of with Object.assign, to a certain extent) to make all state changes with a single pass of `lodash.flow` or `R.evolve`:\r\n\r\n```js\r\nimport { flow, update, set, add } from 'lodash/fp';\r\n\r\nclass MyState extends Container {\r\n  increment() {\r\n    this.setState(flow(\r\n      update(['nested', 'counter'], add(1)),\r\n      set(['didIncrement'], true)\r\n    ));\r\n  }\r\n}\r\n```\r\n\r\n\"Advanced\" (imagine airquotes, thanks :) ).\r\n```js\r\nimport { flow, update, set, add, subtract, multiply } from 'lodash/fp';\r\n\r\nclass MyState extends Container {\r\n  constructor() {\r\n    super();\r\n\r\n    this.increment = this.createSetState(add, 1);\r\n    this.decrement = this.createSetState(substract, 1);\r\n    this.double = this.createSetState(multiply, 2);\r\n  }\r\n\r\n  createSetState(method, count) {\r\n    return () => this.setState(flow(\r\n      update(['nested', 'counter'], method(count))\r\n    ));\r\n  }\r\n}\r\n```\r\n\r\n### Todo\r\n\r\n- [x] decide on Object.assign when using callback style\r\n- [ ] add tests\r\n- [ ] write documentation\r\n- [ ] bump versions, publish etc.\r\n","created_at":"2018-02-01T14:29:51Z","updated_at":"2018-03-01T22:22:43Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a224bea6d96b828d21b01b969d193078f1ff4bf5","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/jamiebuilds/unstated/pulls/1/commits","review_comments_url":"https://api.github.com/repos/jamiebuilds/unstated/pulls/1/comments","review_comment_url":"https://api.github.com/repos/jamiebuilds/unstated/pulls/comments{/number}","comments_url":"https://api.github.com/repos/jamiebuilds/unstated/issues/1/comments","statuses_url":"https://api.github.com/repos/jamiebuilds/unstated/statuses/ff77c96e775d39d7746773c67e37f1d2e97afba6","head":{"label":"StevenLangbroek:callback_setstate","ref":"callback_setstate","sha":"ff77c96e775d39d7746773c67e37f1d2e97afba6","user":{"login":"StevenLangbroek","id":296796,"avatar_url":"https://avatars0.githubusercontent.com/u/296796?v=4","gravatar_id":"","url":"https://api.github.com/users/StevenLangbroek","html_url":"https://github.com/StevenLangbroek","followers_url":"https://api.github.com/users/StevenLangbroek/followers","following_url":"https://api.github.com/users/StevenLangbroek/following{/other_user}","gists_url":"https://api.github.com/users/StevenLangbroek/gists{/gist_id}","starred_url":"https://api.github.com/users/StevenLangbroek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/StevenLangbroek/subscriptions","organizations_url":"https://api.github.com/users/StevenLangbroek/orgs","repos_url":"https://api.github.com/users/StevenLangbroek/repos","events_url":"https://api.github.com/users/StevenLangbroek/events{/privacy}","received_events_url":"https://api.github.com/users/StevenLangbroek/received_events","type":"User","site_admin":false},"repo":{"id":119845442,"name":"unstated","full_name":"StevenLangbroek/unstated","owner":{"login":"StevenLangbroek","id":296796,"avatar_url":"https://avatars0.githubusercontent.com/u/296796?v=4","gravatar_id":"","url":"https://api.github.com/users/StevenLangbroek","html_url":"https://github.com/StevenLangbroek","followers_url":"https://api.github.com/users/StevenLangbroek/followers","following_url":"https://api.github.com/users/StevenLangbroek/following{/other_user}","gists_url":"https://api.github.com/users/StevenLangbroek/gists{/gist_id}","starred_url":"https://api.github.com/users/StevenLangbroek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/StevenLangbroek/subscriptions","organizations_url":"https://api.github.com/users/StevenLangbroek/orgs","repos_url":"https://api.github.com/users/StevenLangbroek/repos","events_url":"https://api.github.com/users/StevenLangbroek/events{/privacy}","received_events_url":"https://api.github.com/users/StevenLangbroek/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/StevenLangbroek/unstated","description":"State ain't shit","fork":true,"url":"https://api.github.com/repos/StevenLangbroek/unstated","forks_url":"https://api.github.com/repos/StevenLangbroek/unstated/forks","keys_url":"https://api.github.com/repos/StevenLangbroek/unstated/keys{/key_id}","collaborators_url":"https://api.github.com/repos/StevenLangbroek/unstated/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/StevenLangbroek/unstated/teams","hooks_url":"https://api.github.com/repos/StevenLangbroek/unstated/hooks","issue_events_url":"https://api.github.com/repos/StevenLangbroek/unstated/issues/events{/number}","events_url":"https://api.github.com/repos/StevenLangbroek/unstated/events","assignees_url":"https://api.github.com/repos/StevenLangbroek/unstated/assignees{/user}","branches_url":"https://api.github.com/repos/StevenLangbroek/unstated/branches{/branch}","tags_url":"https://api.github.com/repos/StevenLangbroek/unstated/tags","blobs_url":"https://api.github.com/repos/StevenLangbroek/unstated/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/StevenLangbroek/unstated/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/StevenLangbroek/unstated/git/refs{/sha}","trees_url":"https://api.github.com/repos/StevenLangbroek/unstated/git/trees{/sha}","statuses_url":"https://api.github.com/repos/StevenLangbroek/unstated/statuses/{sha}","languages_url":"https://api.github.com/repos/StevenLangbroek/unstated/languages","stargazers_url":"https://api.github.com/repos/StevenLangbroek/unstated/stargazers","contributors_url":"https://api.github.com/repos/StevenLangbroek/unstated/contributors","subscribers_url":"https://api.github.com/repos/StevenLangbroek/unstated/subscribers","subscription_url":"https://api.github.com/repos/StevenLangbroek/unstated/subscription","commits_url":"https://api.github.com/repos/StevenLangbroek/unstated/commits{/sha}","git_commits_url":"https://api.github.com/repos/StevenLangbroek/unstated/git/commits{/sha}","comments_url":"https://api.github.com/repos/StevenLangbroek/unstated/comments{/number}","issue_comment_url":"https://api.github.com/repos/StevenLangbroek/unstated/issues/comments{/number}","contents_url":"https://api.github.com/repos/StevenLangbroek/unstated/contents/{+path}","compare_url":"https://api.github.com/repos/StevenLangbroek/unstated/compare/{base}...{head}","merges_url":"https://api.github.com/repos/StevenLangbroek/unstated/merges","archive_url":"https://api.github.com/repos/StevenLangbroek/unstated/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/StevenLangbroek/unstated/downloads","issues_url":"https://api.github.com/repos/StevenLangbroek/unstated/issues{/number}","pulls_url":"https://api.github.com/repos/StevenLangbroek/unstated/pulls{/number}","milestones_url":"https://api.github.com/repos/StevenLangbroek/unstated/milestones{/number}","notifications_url":"https://api.github.com/repos/StevenLangbroek/unstated/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/StevenLangbroek/unstated/labels{/name}","releases_url":"https://api.github.com/repos/StevenLangbroek/unstated/releases{/id}","deployments_url":"https://api.github.com/repos/StevenLangbroek/unstated/deployments","created_at":"2018-02-01T14:18:55Z","updated_at":"2018-02-01T14:18:57Z","pushed_at":"2018-02-26T14:52:00Z","git_url":"git://github.com/StevenLangbroek/unstated.git","ssh_url":"git@github.com:StevenLangbroek/unstated.git","clone_url":"https://github.com/StevenLangbroek/unstated.git","svn_url":"https://github.com/StevenLangbroek/unstated","homepage":null,"size":148,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"jamiebuilds:master","ref":"master","sha":"1e77074cbe5418c5dc1882faf4c904129cd369f4","user":{"login":"jamiebuilds","id":952783,"avatar_url":"https://avatars0.githubusercontent.com/u/952783?v=4","gravatar_id":"","url":"https://api.github.com/users/jamiebuilds","html_url":"https://github.com/jamiebuilds","followers_url":"https://api.github.com/users/jamiebuilds/followers","following_url":"https://api.github.com/users/jamiebuilds/following{/other_user}","gists_url":"https://api.github.com/users/jamiebuilds/gists{/gist_id}","starred_url":"https://api.github.com/users/jamiebuilds/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamiebuilds/subscriptions","organizations_url":"https://api.github.com/users/jamiebuilds/orgs","repos_url":"https://api.github.com/users/jamiebuilds/repos","events_url":"https://api.github.com/users/jamiebuilds/events{/privacy}","received_events_url":"https://api.github.com/users/jamiebuilds/received_events","type":"User","site_admin":false},"repo":{"id":119693982,"name":"unstated","full_name":"jamiebuilds/unstated","owner":{"login":"jamiebuilds","id":952783,"avatar_url":"https://avatars0.githubusercontent.com/u/952783?v=4","gravatar_id":"","url":"https://api.github.com/users/jamiebuilds","html_url":"https://github.com/jamiebuilds","followers_url":"https://api.github.com/users/jamiebuilds/followers","following_url":"https://api.github.com/users/jamiebuilds/following{/other_user}","gists_url":"https://api.github.com/users/jamiebuilds/gists{/gist_id}","starred_url":"https://api.github.com/users/jamiebuilds/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamiebuilds/subscriptions","organizations_url":"https://api.github.com/users/jamiebuilds/orgs","repos_url":"https://api.github.com/users/jamiebuilds/repos","events_url":"https://api.github.com/users/jamiebuilds/events{/privacy}","received_events_url":"https://api.github.com/users/jamiebuilds/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jamiebuilds/unstated","description":"State so simple, it goes without saying","fork":false,"url":"https://api.github.com/repos/jamiebuilds/unstated","forks_url":"https://api.github.com/repos/jamiebuilds/unstated/forks","keys_url":"https://api.github.com/repos/jamiebuilds/unstated/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jamiebuilds/unstated/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jamiebuilds/unstated/teams","hooks_url":"https://api.github.com/repos/jamiebuilds/unstated/hooks","issue_events_url":"https://api.github.com/repos/jamiebuilds/unstated/issues/events{/number}","events_url":"https://api.github.com/repos/jamiebuilds/unstated/events","assignees_url":"https://api.github.com/repos/jamiebuilds/unstated/assignees{/user}","branches_url":"https://api.github.com/repos/jamiebuilds/unstated/branches{/branch}","tags_url":"https://api.github.com/repos/jamiebuilds/unstated/tags","blobs_url":"https://api.github.com/repos/jamiebuilds/unstated/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jamiebuilds/unstated/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jamiebuilds/unstated/git/refs{/sha}","trees_url":"https://api.github.com/repos/jamiebuilds/unstated/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jamiebuilds/unstated/statuses/{sha}","languages_url":"https://api.github.com/repos/jamiebuilds/unstated/languages","stargazers_url":"https://api.github.com/repos/jamiebuilds/unstated/stargazers","contributors_url":"https://api.github.com/repos/jamiebuilds/unstated/contributors","subscribers_url":"https://api.github.com/repos/jamiebuilds/unstated/subscribers","subscription_url":"https://api.github.com/repos/jamiebuilds/unstated/subscription","commits_url":"https://api.github.com/repos/jamiebuilds/unstated/commits{/sha}","git_commits_url":"https://api.github.com/repos/jamiebuilds/unstated/git/commits{/sha}","comments_url":"https://api.github.com/repos/jamiebuilds/unstated/comments{/number}","issue_comment_url":"https://api.github.com/repos/jamiebuilds/unstated/issues/comments{/number}","contents_url":"https://api.github.com/repos/jamiebuilds/unstated/contents/{+path}","compare_url":"https://api.github.com/repos/jamiebuilds/unstated/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jamiebuilds/unstated/merges","archive_url":"https://api.github.com/repos/jamiebuilds/unstated/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jamiebuilds/unstated/downloads","issues_url":"https://api.github.com/repos/jamiebuilds/unstated/issues{/number}","pulls_url":"https://api.github.com/repos/jamiebuilds/unstated/pulls{/number}","milestones_url":"https://api.github.com/repos/jamiebuilds/unstated/milestones{/number}","notifications_url":"https://api.github.com/repos/jamiebuilds/unstated/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jamiebuilds/unstated/labels{/name}","releases_url":"https://api.github.com/repos/jamiebuilds/unstated/releases{/id}","deployments_url":"https://api.github.com/repos/jamiebuilds/unstated/deployments","created_at":"2018-01-31T13:56:02Z","updated_at":"2018-03-01T22:22:16Z","pushed_at":"2018-03-01T12:59:59Z","git_url":"git://github.com/jamiebuilds/unstated.git","ssh_url":"git@github.com:jamiebuilds/unstated.git","clone_url":"https://github.com/jamiebuilds/unstated.git","svn_url":"https://github.com/jamiebuilds/unstated","homepage":"http://unstated.io","size":190,"stargazers_count":2142,"watchers_count":2142,"language":"JavaScript","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":54,"mirror_url":null,"archived":false,"open_issues_count":4,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":54,"open_issues":4,"watchers":2142,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/jamiebuilds/unstated/pulls/1"},"html":{"href":"https://github.com/jamiebuilds/unstated/pull/1"},"issue":{"href":"https://api.github.com/repos/jamiebuilds/unstated/issues/1"},"comments":{"href":"https://api.github.com/repos/jamiebuilds/unstated/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/jamiebuilds/unstated/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/jamiebuilds/unstated/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/jamiebuilds/unstated/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/jamiebuilds/unstated/statuses/ff77c96e775d39d7746773c67e37f1d2e97afba6"}},"author_association":"NONE"}}
{"actor":{"display_login":"jamiebuilds"}}