Snapshot / Bootstrap with Immutable.js AltStores?
- Dominant language
- JavaScript
- Stars
- 3.4k
- Forks
- 312
- PR merge metrics
- No merged PRs in 30d
Description
First off, thanks for Alt - I'm just starting to integrate it and it's been a breeze.
I'm having some difficulty getting `alt.takeSnapshot()` and `alt.bootstrap()` to work when using `AltStore` instances and the`alt/utils/ImmutableUtil` wrapper function. I _think_ I have followed the directions for its use as per https://github.com/goatslacker/alt/blob/master/docs/utils/immutable.md. When I call `alt.takeSnapshot()`, the returned string looks OK. That is, it contains the data in the format prescribed at http://alt.js.org/docs/bootstrap/. However, when I pass that string back into `alt.bootstrap()`, crazy things happen to the data and my application state goes bye-bye.
I put together a branch of the alt-tutorial repo that illustrates the weirdness that I'm seeing: https://github.com/goatslacker/alt-tutorial/compare/master...mwise:immutable-stores
To see the issue, run `npm start`, then click some favorites, then click 'Take snapshot', then click 'Restore snapshot' and watch all the data go away.
The "crazy things" that happen to my data are a little tough to explain. I did some debugging (in my actual app, not in the above linked repo), and it looked like the call to `Immutable.fromJS()` at https://github.com/goatslacker/alt/blob/master/src/utils/ImmutableUtil.js#L19 was being passed the correct objects. However - and this is kind of the crazy part - the calls to `Immutable.fromJS()` seemed to be returning results as if they were reducing the values being returned. That is, if my snapshot string was:
```
'{"AccessTokenStore":{"accesstokenvalue":"ACCESSTOKEN"},"ConfigStore":{"configvalue":"CONFIG"}}'
```
I would see successive calls to `Immutable.fromJS()` that looked like this:
```
// call 1:
Immutable.fromJS({ accesstokenvalue: "ACCESSTOKEN" })
// returns an Immutable.Map such that:
AccessTokenStore.getState().toJS() === { "accesstokenvalue": "ACCESSTOKEN" }
// call 2:
Immutable.fromJS({ configvalue: "CONFIG" })
// returns an Immutable.Map such that:
ConfigStore.getState().toJS() === {
"accesstokenvalue": "ACCESSTOKEN",
"configvalue": "CONFIG"
}
// WAT?!
```
I sort of couldn't believe my eyes when I saw that second call. So either a) I'm doing something incredibly dumb and somehow clobbering some part of Immutable's internals or b) there's a bug in `Immutable.fromJS()` or c) there's a bug in Alt.js's ImmutableUtils or d) my AltStore is setup incorrectly.
**tl,dr: It seems like snapshot/bootstrap don't "just work" when used with AltStores with Immutable.js state objects. Any insight or suggestions would be greatly appreciated.**
Thanks,
Mark
Contributor guide
Research direction
Start with the immutable-stores branch of the alt-tutorial reproduction and run npm start, then follow the snapshot and restore flow. Inspect src/utils/ImmutableUtil.js around line 19 and the AltStore bootstrap path. Done means snapshot/bootstrap preserves separate Immutable store state instead of combining or clearing it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100