yohash / UnityReact

A react-like backend built in C# for Unity
MIT License
0 stars 0 forks source link

Static `Action` to allow Components to Subscribe to `IAction` dispatches #14

Open yohash opened 2 months ago

yohash commented 2 months ago

Each Action would have a delegate Dispatched(IAction action) that is static to each IAction type. Any Component can attach to it by subscribing to the static delegate, public static Dispatched OnDispatch;.

This will allow a Component to latch on to actions like "events".

Motivation: It is difficult to "trigger" game effects using state. The pattern tends towards:

  1. an IAction mutates some FSM in state to a "has been triggered" type
  2. the listening Component will initiate its triggered effect, and then dispatches a "trigger initiated" type IAction
  3. the same FSM in state now moves to the "trigger complete" type

Alternately, we can of course compare if (oldProps.Value != props.Value), but this still leaves us needing to "reset" the "trigger var".

This sort of "action as event" might be nice to try out.

yohash commented 2 months ago

To consider here are the merits between:

  1. capturing an Action in Middleware vs. Component
    • Since this isnt the Component rendering (updating), we won't be returning elements to mount. Elements are mounted in response to state, so they can persist as extensions of the Component.Props.

Is there truly value to hooking into an Action from a Component? Maybe for one-off events, but there must be true value to triggering these 1-off events in Components instead of Middleware. Without the benefit of mounting an Element, is there any value add?