Introduce key path to shared state extension #11
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Current situation
Shared state extension supports a simple dictionary driven store to save every single state and allows scenes to manipulate the state through the capabilities provided by shared state extension.
Developer needs to define a set of keys in any type which conform to
Hashable
protocol, then use traditional key-value way to get or put state onto the store, but literal keys might cause bugs due to typos, based on that, we decided to introduce key path into shared state extension.How we work on shared state extension with key path
Key path is a feature provided by Swift, which give us a safer way to work on variables because of static type.
To improve the experience about using shared state extension, this time we introduced key path to this extension to free developers from typo caused bugs.
There are two types to make it runs, one is
SharedStateKey
, another one isSharedStateValues
.SharedStateKey
represents a type for you to define your custom keys, here is a case:SharedStateValues
is a struct acts as entry point for you to extend variables as many as you like, which will also generate key path for your custom values, below is a case:Above are all you need to do before making your variables can be shared between different scenes.
After that, you can get or put your variables by as same as before, the only one difference is you pass key path to method as arguments.
We also know the power of property wrapper, especially we have seen tons of usage of them in SwiftUI, so here we bring a new property wrapper for key path driven shared state extension, just like how
@Environment
works in SwiftUI.Hope this newer, safer way to share state between scenes can reduce risks and improve experience to a higher level.