Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce key path to shared state extension #11

Merged
merged 1 commit into from
Jul 27, 2021

Conversation

lumiasaki
Copy link
Owner

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 is SharedStateValues.

SharedStateKey represents a type for you to define your custom keys, here is a case:

struct TimestampKey: SharedStateKey {

    // this will give compiler a hint about concrete type.
    static var currentValue: TimeInterval?
}

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:

extension SharedStateValues {

    // this will generate a key path for `timestamp` variable.
    var timestamp: TimeInterval? {
        get { Self[TimestampKey.self] }
        set { Self[TimestampKey.self] = newValue }
    }
}

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.

scene.sbx.getSharedState(by: \.timestamp)
// or
scene.sbx.putSharedState(by: \.timestamp, sharedState: value)

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.

class ViewController: UIViewController, Scene {

    // ...
    @SharedStateInjected(\.timestamp)
    var timestamp: TimeInterval?
}

Hope this newer, safer way to share state between scenes can reduce risks and improve experience to a higher level.

@lumiasaki lumiasaki added the enhancement New feature or request label Jul 27, 2021
@lumiasaki lumiasaki merged commit f30662f into main Jul 27, 2021
@lumiasaki lumiasaki deleted the feature/keypath_shared_state_extension branch July 27, 2021 11:30
@lumiasaki
Copy link
Owner Author

lumiasaki commented Jul 28, 2021

Update:

Delete SharedStateKey and SharedStateValues due to isolation issue. The previous implementation was based on single source of truth, but every variable was setting on or getting from SharedStateKey.currentValue, this might cause problems when you have more than one box and meanwhile you reuse the keys.

Modification is delete SharedStateKey and SharedStateValues completely, every shared state extension owns a separated stateValue, during its initialization phase developer needs to assign your instance of state to shared state extension. Developers do not need to extend SharedStateValues or define your SharedStateKey anymore, just provide an instance of your custom state value.

The rest part is still follow this PR, with same APIs, only one difference is you need to point out type for your key paths as below:

class ViewController: UIViewController, Scene {

    // ...
    @SharedStateInjected(\MyState.timestamp)
    var timestamp: TimeInterval?
}

Code changes: #12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant