{"action":"opened","number":1091,"pull_request":{"url":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls/1091","id":233316749,"node_id":"MDExOlB1bGxSZXF1ZXN0MjMzMzE2NzQ5","html_url":"https://github.com/mobxjs/mobx-state-tree/pull/1091","diff_url":"https://github.com/mobxjs/mobx-state-tree/pull/1091.diff","patch_url":"https://github.com/mobxjs/mobx-state-tree/pull/1091.patch","issue_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues/1091","number":1091,"state":"open","locked":false,"title":"reference, onInvalidation hook, types.safeReference and more","user":{"login":"xaviergonz","id":6306796,"node_id":"MDQ6VXNlcjYzMDY3OTY=","avatar_url":"https://avatars2.githubusercontent.com/u/6306796?v=4","gravatar_id":"","url":"https://api.github.com/users/xaviergonz","html_url":"https://github.com/xaviergonz","followers_url":"https://api.github.com/users/xaviergonz/followers","following_url":"https://api.github.com/users/xaviergonz/following{/other_user}","gists_url":"https://api.github.com/users/xaviergonz/gists{/gist_id}","starred_url":"https://api.github.com/users/xaviergonz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xaviergonz/subscriptions","organizations_url":"https://api.github.com/users/xaviergonz/orgs","repos_url":"https://api.github.com/users/xaviergonz/repos","events_url":"https://api.github.com/users/xaviergonz/events{/privacy}","received_events_url":"https://api.github.com/users/xaviergonz/received_events","type":"User","site_admin":false},"body":"- small refactor over references\r\n- small refactor over scalar and object nodes, by extracting common code to a BaseNode class\r\n- added an internal mechanism to subscribe to hooks (all of them for object nodes, afterAttach/beforeDestroy for scalar nodes)\r\n  - this should make it kind of trivial to make a `addHook(model/array/map/scalar/whateverType, 'afterCreation'..., (self) => {...})` in the future if desired\r\n- added an onInvalidated option to the reference options (explained below)\r\n- added a types.safeReference type (explained below)\r\n- includes PR #1087 \r\n\r\n#### Reference validation: `isValidReference`, `tryReference`, `onInvalidate` hook and `types.safeReference`\r\n\r\nAcessing an invalid reference (a reference to a dead/detached node) triggers an exception.\r\n\r\nIn order to check if a reference is valid, MST offers the `isValidReference(() => ref): boolean` function:\r\n```ts\r\nconst isValid = isValidReference(() => store.myRef)\r\n```\r\n\r\nAlso, if you are unsure if a reference is valid or not you can use the `tryReference(() => ref): ref | undefined` function:\r\n```ts\r\n// the result will be the passed ref if ok, or undefined if invalid\r\nconst maybeValidRef = tryReference(() => store.myRef)\r\n```\r\n\r\nThe options parameter for references also accepts an optional `onInvalidated` hook, which will be called when the reference target node that the reference is pointing to is about to be detached/destroyed. It has the following signature:\r\n```ts\r\nconst refWithOnInvalidated = types.reference(Todo, {\r\n    onInvalidated(event: {\r\n        // what is causing the target to become invalidated\r\n        cause: \"detach\" | \"destroy\"\r\n        // the target that is about to become invalidated\r\n        oldRef: STN \r\n        // parent node of the reference (not the reference target)\r\n        parent: IAnyStateTreeNode \r\n        // a function to set our reference to a new target\r\n        setNewRef: (newRef: STN | undefined | null) => void \r\n    }) {\r\n        // do something\r\n    }\r\n})\r\n```\r\nNote that invalidation will only trigger while the reference is attached to a parent (be it a model, an array, a map, etc.).\r\n\r\nA default implementation of such `onInvalidated` hook is provided by the `types.safeReference` type. It is like a standard reference, except that once the target node becomes invalidated it will:\r\n- If its parent is a model: Set its own property to `undefined`\r\n- If its parent is an array: Remove itself from the array\r\n- If its parent is a map: Remove itself from the map\r\n\r\nStrictly speaking it is a `types.maybe(types.reference(Type))` with a custom `onInvalidate` option.\r\n\r\n```js\r\nconst Todo = types.model({ id: types.identifier })\r\nconst Store = types.model({\r\n    todos: types.array(Todo),\r\n    selectedTodo: types.safeReference(Todo)\r\n})\r\n\r\n// given selectedTodo points to a valid Todo and that Todo is later removed from the todos\r\n// array, then selectedTodo will automatically become undefined\r\n```\r\n","created_at":"2018-11-24T12:10:53Z","updated_at":"2018-11-24T12:10:53Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls/1091/commits","review_comments_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls/1091/comments","review_comment_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues/1091/comments","statuses_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/statuses/971d819644e494468496ee26582db89a5837901c","head":{"label":"mobxjs:safeReference","ref":"safeReference","sha":"971d819644e494468496ee26582db89a5837901c","user":{"login":"mobxjs","id":17475736,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE3NDc1NzM2","avatar_url":"https://avatars3.githubusercontent.com/u/17475736?v=4","gravatar_id":"","url":"https://api.github.com/users/mobxjs","html_url":"https://github.com/mobxjs","followers_url":"https://api.github.com/users/mobxjs/followers","following_url":"https://api.github.com/users/mobxjs/following{/other_user}","gists_url":"https://api.github.com/users/mobxjs/gists{/gist_id}","starred_url":"https://api.github.com/users/mobxjs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mobxjs/subscriptions","organizations_url":"https://api.github.com/users/mobxjs/orgs","repos_url":"https://api.github.com/users/mobxjs/repos","events_url":"https://api.github.com/users/mobxjs/events{/privacy}","received_events_url":"https://api.github.com/users/mobxjs/received_events","type":"Organization","site_admin":false},"repo":{"id":67361566,"node_id":"MDEwOlJlcG9zaXRvcnk2NzM2MTU2Ng==","name":"mobx-state-tree","full_name":"mobxjs/mobx-state-tree","private":false,"owner":{"login":"mobxjs","id":17475736,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE3NDc1NzM2","avatar_url":"https://avatars3.githubusercontent.com/u/17475736?v=4","gravatar_id":"","url":"https://api.github.com/users/mobxjs","html_url":"https://github.com/mobxjs","followers_url":"https://api.github.com/users/mobxjs/followers","following_url":"https://api.github.com/users/mobxjs/following{/other_user}","gists_url":"https://api.github.com/users/mobxjs/gists{/gist_id}","starred_url":"https://api.github.com/users/mobxjs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mobxjs/subscriptions","organizations_url":"https://api.github.com/users/mobxjs/orgs","repos_url":"https://api.github.com/users/mobxjs/repos","events_url":"https://api.github.com/users/mobxjs/events{/privacy}","received_events_url":"https://api.github.com/users/mobxjs/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/mobxjs/mobx-state-tree","description":"Model Driven State Management","fork":false,"url":"https://api.github.com/repos/mobxjs/mobx-state-tree","forks_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/forks","keys_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/teams","hooks_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/hooks","issue_events_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues/events{/number}","events_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/events","assignees_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/assignees{/user}","branches_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/branches{/branch}","tags_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/tags","blobs_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/refs{/sha}","trees_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/statuses/{sha}","languages_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/languages","stargazers_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/stargazers","contributors_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/contributors","subscribers_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/subscribers","subscription_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/subscription","commits_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/commits{/sha}","git_commits_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/commits{/sha}","comments_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/comments{/number}","issue_comment_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues/comments{/number}","contents_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/contents/{+path}","compare_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/merges","archive_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/downloads","issues_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues{/number}","pulls_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls{/number}","milestones_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/milestones{/number}","notifications_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/labels{/name}","releases_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/releases{/id}","deployments_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/deployments","created_at":"2016-09-04T18:28:25Z","updated_at":"2018-11-23T11:06:50Z","pushed_at":"2018-11-24T12:05:48Z","git_url":"git://github.com/mobxjs/mobx-state-tree.git","ssh_url":"git@github.com:mobxjs/mobx-state-tree.git","clone_url":"https://github.com/mobxjs/mobx-state-tree.git","svn_url":"https://github.com/mobxjs/mobx-state-tree","homepage":"","size":5063,"stargazers_count":3614,"watchers_count":3614,"language":"TypeScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":346,"mirror_url":null,"archived":false,"open_issues_count":49,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":346,"open_issues":49,"watchers":3614,"default_branch":"master"}},"base":{"label":"mobxjs:master","ref":"master","sha":"c2a409b164c2243411e42662a1a91cfbcf2fb5c1","user":{"login":"mobxjs","id":17475736,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE3NDc1NzM2","avatar_url":"https://avatars3.githubusercontent.com/u/17475736?v=4","gravatar_id":"","url":"https://api.github.com/users/mobxjs","html_url":"https://github.com/mobxjs","followers_url":"https://api.github.com/users/mobxjs/followers","following_url":"https://api.github.com/users/mobxjs/following{/other_user}","gists_url":"https://api.github.com/users/mobxjs/gists{/gist_id}","starred_url":"https://api.github.com/users/mobxjs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mobxjs/subscriptions","organizations_url":"https://api.github.com/users/mobxjs/orgs","repos_url":"https://api.github.com/users/mobxjs/repos","events_url":"https://api.github.com/users/mobxjs/events{/privacy}","received_events_url":"https://api.github.com/users/mobxjs/received_events","type":"Organization","site_admin":false},"repo":{"id":67361566,"node_id":"MDEwOlJlcG9zaXRvcnk2NzM2MTU2Ng==","name":"mobx-state-tree","full_name":"mobxjs/mobx-state-tree","private":false,"owner":{"login":"mobxjs","id":17475736,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE3NDc1NzM2","avatar_url":"https://avatars3.githubusercontent.com/u/17475736?v=4","gravatar_id":"","url":"https://api.github.com/users/mobxjs","html_url":"https://github.com/mobxjs","followers_url":"https://api.github.com/users/mobxjs/followers","following_url":"https://api.github.com/users/mobxjs/following{/other_user}","gists_url":"https://api.github.com/users/mobxjs/gists{/gist_id}","starred_url":"https://api.github.com/users/mobxjs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mobxjs/subscriptions","organizations_url":"https://api.github.com/users/mobxjs/orgs","repos_url":"https://api.github.com/users/mobxjs/repos","events_url":"https://api.github.com/users/mobxjs/events{/privacy}","received_events_url":"https://api.github.com/users/mobxjs/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/mobxjs/mobx-state-tree","description":"Model Driven State Management","fork":false,"url":"https://api.github.com/repos/mobxjs/mobx-state-tree","forks_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/forks","keys_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/teams","hooks_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/hooks","issue_events_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues/events{/number}","events_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/events","assignees_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/assignees{/user}","branches_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/branches{/branch}","tags_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/tags","blobs_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/refs{/sha}","trees_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/statuses/{sha}","languages_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/languages","stargazers_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/stargazers","contributors_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/contributors","subscribers_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/subscribers","subscription_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/subscription","commits_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/commits{/sha}","git_commits_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/git/commits{/sha}","comments_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/comments{/number}","issue_comment_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues/comments{/number}","contents_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/contents/{+path}","compare_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/merges","archive_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/downloads","issues_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues{/number}","pulls_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls{/number}","milestones_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/milestones{/number}","notifications_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/labels{/name}","releases_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/releases{/id}","deployments_url":"https://api.github.com/repos/mobxjs/mobx-state-tree/deployments","created_at":"2016-09-04T18:28:25Z","updated_at":"2018-11-23T11:06:50Z","pushed_at":"2018-11-24T12:05:48Z","git_url":"git://github.com/mobxjs/mobx-state-tree.git","ssh_url":"git@github.com:mobxjs/mobx-state-tree.git","clone_url":"https://github.com/mobxjs/mobx-state-tree.git","svn_url":"https://github.com/mobxjs/mobx-state-tree","homepage":"","size":5063,"stargazers_count":3614,"watchers_count":3614,"language":"TypeScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":346,"mirror_url":null,"archived":false,"open_issues_count":49,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":346,"open_issues":49,"watchers":3614,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls/1091"},"html":{"href":"https://github.com/mobxjs/mobx-state-tree/pull/1091"},"issue":{"href":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues/1091"},"comments":{"href":"https://api.github.com/repos/mobxjs/mobx-state-tree/issues/1091/comments"},"review_comments":{"href":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls/1091/comments"},"review_comment":{"href":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mobxjs/mobx-state-tree/pulls/1091/commits"},"statuses":{"href":"https://api.github.com/repos/mobxjs/mobx-state-tree/statuses/971d819644e494468496ee26582db89a5837901c"}},"author_association":"CONTRIBUTOR","merged":false,"mergeable":null,"rebaseable":null,"mergeable_state":"unknown","merged_by":null,"comments":0,"review_comments":0,"maintainer_can_modify":false,"commits":6,"additions":1656,"deletions":775,"changed_files":22}}
{"actor":{"display_login":"xaviergonz"}}