wayfair / vsm-ios

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

willSet and didSet State Publishers #31

Closed albertbori closed 1 year ago

albertbori commented 1 year ago

Description

This PR deprecates the publisher property for the StateContainer class and the @ViewState and @RenderedViewState property wrappers. It replaces the publisher property with two new properties called willSetPublisher and didSetPublisher that more accurately and explicitly indicate the publisher's timing.

Reasoning

During early use of the new @ViewState and @RenderedViewState property wrappers, it was discovered that when observing the state changes in a SwiftUI view via onReceive, the body property had already been evaluated for this state change because it was happening on the underlying StateContainer's willSet event. This means that if an engineer then modifies any SwiftUI view property (i.e., @State, @ObservedObject, etc.) it will cause the body property to be reevaluated, wasting resources and potentially causing timing bugs. In cases where the view state is progressed within the onReceive property, is is possible to cause infinite recursion.

Example Problem Scenario (Credit: @mlarandeau)

As a result, the willSet and didSet event publishers were created so that engineers could have more fine-grained control over the timing of the state observation code.

Type of Change

Checklist