Filter actions in scoped store

In order for a child store to be able to process a child action, it must be sent through the parent store for it to reach the app-level reducer in the first place, which is inevitably responsible for invoking the child reducer with that action internally.

While you can't prevent a parent from observing that child actions are being sent, you can use access control to "hide" the contents of certain child state and actions:

public enum ChildAction {
  case actionVisibleFromParent
  case anotherActionVisibleFromParent
  case internal(Internal)

  public struct Internal {
    internal var action: Action

    internal enum Action {
      case anInternalAction
      case anotherInternalAction
    }
  }
}

The caveat is if you want to test these actions, they must be internal and not private, and if they are internal you must modularize your feature in order to prevent the parent domain from accessing them. There is also quite a bit of boilerplate, but maybe it could be eliminated with access control for enum cases.

We've generally found that this kind of hiding goes against the grain in TCA, though. Do you have a concrete, motivating use of such a pattern? Why shouldn't a parent know what their child is up to? :wink:

If a child feature truly needs local domain that the parent shouldn't know about or observe at all, that domain needs to live outside of the reducer (e.g. as some local @State and action closures, or in a local TCA store that communicates with the child store).

1 Like