wayfair / vsm-ios

An iOS framework for the VSM Architecture
MIT License
9 stars 2 forks source link

Added willSet-didSet to RenderedViewState #32

Closed albertbori closed 1 year ago

albertbori commented 1 year ago

Description

As a followup to #31 , this PR restores some functionality that was lost with the introduction of the @RenderedViewState property wrapper: The ability for a UIKit view or view controller to render / subscribe to the state changes in the willSet event.

Restoring this capability through the @RenderedViewState property wrapper allows engineers to compare the current and future state. This can be helpful in preventing duplicate updates, unnecessary operations, or for fine-tuning animations or navigation.

Example Usage

class MyViewController: UIViewController {
    @RenderedViewState var state: MyViewState
    init(state: MyViewState) {
        _state = .init(wrappedValue: state, render: Self.render)
        super.init(bundle: nil, nib: nil)
    }
    func render(newState: MyViewState) {
        // Compare state (old value) against newState (new value) to determine appropriate actions
        if state != newState {
            ...
        }
        if newState.someValue {
            ...
            $state.observe(newState.someAction())
        }
    }
}

This is an additive change that does not affect current implementations. To opt into the functionality, as in the example above, the engineer only needs add a state parameter to the render function in question. This will automatically switch the state change event that causes the render function to be called from didSet to willSet. These details are called out in the functions DocC documentation.

Type of Change

Checklist