GithubHelp home page GithubHelp logo

Comments (1)

mbrandonw avatar mbrandonw commented on August 22, 2024

Hi there @Ankoma22.

Actions are nested because it aids in modularity. For example, if you have:

enum AppAction {
 case screen1(Screen1Action)
 case screen2(Screen2Action)
}

Then the domain, reducer and view for screen 1 and 2 can each be broken out into their own modules with no dependencies on each other, and they can be pulled back and combined into a main app reducer that powers the entire application. This makes it possible to run those screens in isolation (without building the full app), and can help with compile times as well as many other benefits.

Redux may use global actions, but that could also be due to the fact that JS does not have proper enums, and so they really have no choice. You certainly can use global actions in the architecture by creating a "marker" protocol that your actions conform to:

protocol Action {}

struct MyState { ... }
struct MyAction: Action { ... }

func reducer(state: inout MyState, action: Action) {
  switch action {
  case _ as MyAction:
    // Do things with the action here
    break

  default:
    // Do nothing here?
    break
  }
}

We do not recommend this approach since it makes composition more difficult (you can't pullback), and you lose exhaustivity of handling actions. Notice that we have a default in the switch, and it's not clear what we should do there. Should it be an error if that ever executes? Exhaustivity means we never have to worry about actions coming in that we don't expect, and means we can refactor actions in far, deep corners of the code base and understand how those changes impact the entire application.

And we don't believe that nested actions goes against the law of Demeter. That law is really only applicable to reference types, where knowledge of far away units can cause you to make incorrect assumptions about how the system behaves. Value types don't have this problem, they are just inert data with no behavior.

Hope that helps clear up some of your questions!

from episode-code-samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.