A video series exploring functional programming and Swift.
#74 • Monday Sep 30, 2019 • Subscriber-only

Modular State Management: View Actions

It’s time to fully modularize our app! Our views can still send any app action, so let’s explore transforming stores to focus in on just the local actions a view cares about.

This episode builds on concepts introduced previously:

#74 • Monday Sep 30, 2019 • Subscriber-only

Modular State Management: View Actions

It’s time to fully modularize our app! Our views can still send any app action, so let’s explore transforming stores to focus in on just the local actions a view cares about.

This episode builds on concepts introduced previously:


Subscribe to Point‑Free

This episode is for subscribers only. To access it, and all past and future episodes, become a subscriber today!

See subscription optionsorLog in

Sign up for our weekly newsletter to be notified of new episodes, and unlock access to any subscriber-only episode of your choosing!

Sign up for free episode

Introduction

And just like that our views now take a smaller subset of state than the full AppState, which means the stores that power the views are starting to look a lot more like the reducers that power them.

It’s also worth noting how simple these changes were, and how quick we made them. We merely changed the state that the views operated on to focus in on just what they needed, and then we made sure that the stores we passed to those views were transformed in order to pluck out these values.

Subscribe to Point-Free

👋 Hey there! Does this episode sound interesting? Well, then you may want to subscribe so that you get access to this episodes and more!


Exercises

  1. It can be useful to produce “read-only” stores that cannot send any actions. Write a view that transforms a store that can perform actions into a store that cannot perform actions. What is the appropriate data type to describe the Action of such a store?

    In our second episode on algebraic data types, we explored such a transformation.

  2. In our first episode on algebraic data types, we introduced the Either type, which is the most generic, non-trivial enum one could make:

    enum Either<A, B> {
      case left(A)
      case right(B)
    }
    

    In this episode we create a wrapper enum called CounterViewAction to limit the counter view’s ability to send any app action. Instead of introducing an ad hoc enum, refactor things to utilize the Either type.

    How does this compare to utilizing structs and tuples for intermediate state?


References

Chapters
Introduction
00:06
Transforming a store’s action
00:40
Combining view functions
06:35
Focusing on favorite primes actions
08:45
Extracting our first modular view
11:27
Focusing on prime modal actions
13:43
Focusing on counter actions
15:24
Next time: what’s the point?
22:11