{"action":"created","issue":{"url":"https://api.github.com/repos/btroncone/ngrx-store-localstorage/issues/65","repository_url":"https://api.github.com/repos/btroncone/ngrx-store-localstorage","labels_url":"https://api.github.com/repos/btroncone/ngrx-store-localstorage/issues/65/labels{/name}","comments_url":"https://api.github.com/repos/btroncone/ngrx-store-localstorage/issues/65/comments","events_url":"https://api.github.com/repos/btroncone/ngrx-store-localstorage/issues/65/events","html_url":"https://github.com/btroncone/ngrx-store-localstorage/issues/65","id":267033793,"number":65,"title":"Feature proposal - custom function for merging initialState and rehydratedState","user":{"login":"Matmo10","id":2548215,"avatar_url":"https://avatars3.githubusercontent.com/u/2548215?v=4","gravatar_id":"","url":"https://api.github.com/users/Matmo10","html_url":"https://github.com/Matmo10","followers_url":"https://api.github.com/users/Matmo10/followers","following_url":"https://api.github.com/users/Matmo10/following{/other_user}","gists_url":"https://api.github.com/users/Matmo10/gists{/gist_id}","starred_url":"https://api.github.com/users/Matmo10/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Matmo10/subscriptions","organizations_url":"https://api.github.com/users/Matmo10/orgs","repos_url":"https://api.github.com/users/Matmo10/repos","events_url":"https://api.github.com/users/Matmo10/events{/privacy}","received_events_url":"https://api.github.com/users/Matmo10/received_events","type":"User","site_admin":false},"labels":[{"id":334724105,"url":"https://api.github.com/repos/btroncone/ngrx-store-localstorage/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2017-10-20T01:02:58Z","updated_at":"2018-02-22T22:39:27Z","closed_at":null,"author_association":"NONE","body":"Suppose I have this initial state:\r\n```ts\r\n{ \r\n   someKey: {}\r\n   itemResults: {\r\n          items: [], \r\n          totalItems: 0,\r\n          isListView: true\r\n    },\r\n   someOtherKey: {}\r\n}\r\n```\r\n\r\nand I have this config:\r\n```ts\r\nlocalStorageSync({\r\n    keys: [{itemResults: ['isListView']}],\r\n    rehydrate: true\r\n})\r\n```\r\n\r\nand I have this state being revived from localStorage:\r\n```ts\r\n{ \r\n   itemResults: {\r\n       isListView: false\r\n   }\r\n}\r\n```\r\n\r\n\r\nWhich means during `@ngrx/store/init`, since [we only do a shallow merge of keys using Object.assign](https://github.com/btroncone/ngrx-store-localstorage/blob/master/src/index.ts#L180), I end up with the following state:\r\n```ts\r\n{ \r\n   itemResults: {\r\n       isListView: false\r\n   }\r\n}\r\n```\r\nSo basically if you use a nested key or keys, then that slice of state overrides the whole slice of initialState for the top level key. I was thinking it'd be nice if I could just use [lodash's merge](https://lodash.com/docs/4.17.4#merge) to merge (or basically Object.assign nested keys also), but I don't want to bloat this library either. It would be nice if I could supply a function to the library to use to do the merge, then I could just use lodash.merge there instead of bundling it with the library. \r\n\r\nCould we possibly add a config option to allow the user to provide a custom merge function? Basically something like this?\r\n\r\n```ts\r\nexport const localStorageSync = (config: LocalStorageConfig) => (reducer: any) => {\r\n    // ... code omitted\r\n\r\n    const mergeFunction = config.customMergeFunction || ((state, rehydratedState) =>  Object.assign({}, state, rehydratedState));\r\n\r\n    return function (state = rehydratedState, action: any) {\r\n        \r\n        if (action.type === INIT_ACTION && rehydratedState) {\r\n            state = mergeFunction(state, rehydratedState);\r\n        }\r\n     \r\n        // ... code omitted\r\n    };\r\n};\r\n```\r\n\r\nAny thoughts/objections, anyone?"},"comment":{"url":"https://api.github.com/repos/btroncone/ngrx-store-localstorage/issues/comments/367847336","html_url":"https://github.com/btroncone/ngrx-store-localstorage/issues/65#issuecomment-367847336","issue_url":"https://api.github.com/repos/btroncone/ngrx-store-localstorage/issues/65","id":367847336,"user":{"login":"tanyagray","id":153077,"avatar_url":"https://avatars1.githubusercontent.com/u/153077?v=4","gravatar_id":"","url":"https://api.github.com/users/tanyagray","html_url":"https://github.com/tanyagray","followers_url":"https://api.github.com/users/tanyagray/followers","following_url":"https://api.github.com/users/tanyagray/following{/other_user}","gists_url":"https://api.github.com/users/tanyagray/gists{/gist_id}","starred_url":"https://api.github.com/users/tanyagray/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tanyagray/subscriptions","organizations_url":"https://api.github.com/users/tanyagray/orgs","repos_url":"https://api.github.com/users/tanyagray/repos","events_url":"https://api.github.com/users/tanyagray/events{/privacy}","received_events_url":"https://api.github.com/users/tanyagray/received_events","type":"User","site_admin":false},"created_at":"2018-02-22T22:39:27Z","updated_at":"2018-02-22T22:39:27Z","author_association":"COLLABORATOR","body":"Thanks everyone for your input, I'm reviewing existing issues and PRs in my available time and see this as a high priority. Have tagged the issue for attention.\r\n\r\nAt this point I understand that the PR #15 is not specific enough, and the ideal solution would be an option to provide custom `serialize` and `deserialize` functions?\r\n\r\nWould appreciate people's (more recent) thoughts on this as the issue has been quiet for a while."}}
{"actor":{"display_login":"tanyagray"}}