Skip to content

How to reduce repetition in VSM? (DRY principle) #47

Answered by albertbori
albertbori asked this question in Q&A
Discussion options

You must be logged in to vote

You'll want to avoid using classes for your state models due to the ephemeral nature of states, and the memory storage implications.

There are two common approaches to reducing repetition in VSM state models:

  1. Forward actions to other states.
  2. Use Protocol-Oriented Programming (POP) techniques.

Forwarding Actions

To reuse other models' actions without deferring to Object-Oriented Programming (OOP) techniques, you can forward one model's action to another like so:

struct LoaderModel {
    func load() -> some Publisher<MyViewState, Never> {
        load(page: 1)
    }

    fileprivate func load(page: Int) -> some Publisher<MyViewState, Never> {
        ...
    }
}

struct LoadedModel {
    let

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by albertbori
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant