Comments (3)
Just noting that this and redux-persist
's State Reconcilers are both only for initial state. This should be made very explicit in any docs for this. Merging existing, in-memory data is out-of-scope and should be done in application code / actions, not in mst-persist
as it's in-memory and not related to persisting to disk.
That being said, we could expose some of these functions if users want to use elsewhere in their code I guess, but they should all be pretty simple.
Another note is that this is related to implementing Transforms. We could just not have merge strategies built-in and leave them up to the user to implement as transforms. If we do have merge strategies built-in, then the order between them and transforms needs to be defined (and merge strategies should probably just be implemented as a transform).
I would assume the merge goes first, then future transforms, but I'm actually not sure that's optimal, or that having the merge order be pre-defined is intuitive. It may be more intuitive to have merge strategies be mutually exclusive with transforms, so the user has to explicitly add it to their list of transforms.
The only issue there is that redux-persist
has transforms per key while state reconcilers are for the whole store. It might just make sense to have transforms in mst-persist
be on the whole store as well -- the user chooses what to do with each key on their own, and this allows for transforms involving multiple keys as well. Migrations seem to be done that way as well, so that way a migration would just be a transform as well.
(Getting a bit too into the details of transforms in the wrong issue, but so I don't forget again, transforms should probably receive a dereferenced snapshot instead of an MST object so that mutable changes don't accidentally change the state of the running app. EDIT: and then I forgot again that we're acting on a snapshot by definition: all transforms run inside of onSnapshot
)
from mst-persist.
Since I don't really understand most of what you're talking about, but can mostly assume it's to do with how to persist selected parts of our tree:
Assume you're making a game, where you have something roughly like
Game: <Game>
Player: <Player>
Characters: <Character[]>
- Id: <Guid>
name: <string>
type: <Ref<CharacterClass>>
hp: <number>
entities: <Entity[]>
modifiers: <Modifier[]>
currentCharacter: <Ref<Character>>
Meta: <GameMeta>
CharacterClasses: <CharacterClass[]>
Zones: <Zone[]>
- Id: <Guid>
Entities: <Entity[]>
- Id: <Guid>
name: 'Loot Chest'
entities: <Entity[]>
- Id: <Guid>
name: 'Coins'
qty: 100
Layers: <MapLayer[]>
- Id: <Guid>
depth: <number>
Map: <Map>
tileMap: <String>
tileSet: <String>
currentZone: <Ref<Zone>>
Some of this I want to persist, some of it i want to come from initial data:
Game.Player
> persist
Game.currentZone
> persist
Game.Zones[]
> initial data
Game.Zones[].Entities
> persist
Suggestions on how to do this without:
- merge strategy
- rewriting the Store to delineate peristable vs non persistable items?
from mst-persist.
@airtonix sorry for the delayed response
but can mostly assume it's to do with how to persist selected parts of our tree:
Sorry that's not correct. This issue is about how to merge persisted data with data created during .create
. It currently overwrites data from .create
as that is applySnapshot
's default. It does use the defaults defined inside of your model itself though.
Assume you're making a game, where you have something roughly like
This is a fairly complex model -- minimal examples are much more helpful and significantly easier to respond to. I saw this when you commented, but given the complexity here, I did not really have the time to respond. I also did not see your edits until now
Some of this I want to persist, some of it i want to come from initial data:
You might be looking for #27, but I can see that this would be useful for the initial data portion as well.
Suggestions on how to do this without:
- merge strategy
- rewriting the Store to delineate peristable vs non persistable items? [sic]
- I'd say the main workaround would be to delete persisted data and add the initial data after
persist
runs. - The other would be to add some defaults to the model itself since
applySnapshot
will use those in the case ofundefined
(i.e. if no data is persisted for a part of a model). But due to the nested nature of your data, I don't think this would solve your problem without #27. For simpler models, this does work though and is how I've done defaults myself.
from mst-persist.
Related Issues (20)
- `storage` type should be more specific
- Add CI
- CJS export is missing __esModule - Code transpiled to CJS can't use default export HOT 3
- Docs should mention that mst-persist is compatible with redux-persist's Storage Engines
- options.jsonify is always forced to true HOT 2
- When using AsyncLocalStorage, "Illegal invocation" error occurs HOT 3
- `persist` should return disposer
- [RFC] Transforms & Migrations (& Plugins?) HOT 2
- Initial State isn't merged -- undefined is not assignable to type HOT 3
- Persist changed value from `true` to `false` HOT 3
- Add Deep Whitelists & Blacklists HOT 5
- add expiration time? HOT 2
- Persist Array in React Native, empty string? -- can't repro HOT 3
- jest failing on react-scripts - needs a CJS build HOT 9
- Should you clear storage when changing your schema? HOT 2
- data not saved when using `blacklist` or `whitelist` -- can't repro HOT 1
- mst peerDep is outdated HOT 3
- Add tests HOT 2
- Refactor to TypeScript / Add typings
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mst-persist.