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

Synthesize update(state:actions:environment:) for ModelProtocol #16

Merged
merged 1 commit into from Sep 16, 2022

Conversation

gordonbrander
Copy link
Collaborator

@gordonbrander gordonbrander commented Sep 15, 2022

#15 caused fx to be run immediately instead of joined on main. The intent was to allow for composing multiple actions by sending up many Just(.action) fx. However,

  • This is verbose to write, and rather "chatty".
  • It also makes the store implementation less straightforward, since without joining on main, we must check if fx was completed immediately before adding to fx dictionary. Joining on main solves this problem by running the fx on next tick, after the fx has been added to dictionary.
  • Additionally, it means that off-main-thread fx are required to be joined manually on main to prevent SwiftUI from complaining.

This PR introduces an alternative approach to composing multiple update functions. Any type that conforms to ModelProtocol has a update(state:actions:environment) static function synthesized for it. This function can be used to simulate the effect of sending multiple actions in sequence immediately, in effect, composing the actions.

update(
    state: state,
    actions: [
        .setEditor(
            text: detail.entry.body,
            saveState: detail.saveState
        ),
        .presentDetail(true),
        .requestEditorFocus(false)
    ],
    environment: environment
)

State is updated immediately, fx are merged, and last transaction wins.

Now that we have a way to immediately sequence actions in same state update, we no longer need to run fx on same tick. Joining on main is my preference from an API perspective because it has fewer footguns in implementation and use.

Breaking changes

  • Remove Update.pipe. Redundant now. Was never happy with it anyway. It was an inelegant way to accomplish the same thing as update(state:actions:environment:).
  • Revert fx to be joined on main thread. We join on main with a .default QoS, because fx should be async/never block user interaction.

Also:
- Remove Update.pipe. Was never happy with this approach. Ineligant.
- Revert fx to be joined on main thread

Now that we have a way to immediately sequence actions, we no longer
need to run fx on same tick. Joining on main is my preference from an
API perspective because it has fewer footguns in implementation and use.

We join on main with a .default QoS, because fx should be async/never
block user interaction.
@gordonbrander gordonbrander changed the title Synthesize update(state:actions:environment) for ModelProtocol Synthesize update(state:actions:environment:) for ModelProtocol Sep 15, 2022
@gordonbrander gordonbrander merged commit 5ea1c8b into main Sep 16, 2022
@gordonbrander gordonbrander deleted the 2022-09-15-update-actions-multi branch September 16, 2022 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant