CrabUI
Loading...
Searching...
No Matches
Commands / Data flow

CUICommand and CUIData is a "record (string Name, object data = null)"

You can create and dispatch them up and down the component three for whoever consumes them

This turns out to be an essential tool, it allows you to not depent on the layout
Now you don't need to pass callbacks and objects, you just know that somewhere there is a child that will consume this type of data and you just dispatch it into the void

You can rearrange the layout as you want and nothing will break

How to use them

Commmands and data are essentially the same, but commands are moving up and data is moving down

To listen for commands :

  • AddCommand(string name, Action<object> action) on a parent
  • DispatchUp(CUICommand command) on a nested child

To listen for data:

  • set "string Consumes" on a child to some value
  • Add callback to "event Action<Object> OnConsume" event on a child
  • DispatchDown(CUIData data) on a parent

Also you can add callbacks to "event Action<CUICommand> OnAnyCommand" "eventAction<CUIData> OnAnyData" events to listen for anything

Commands and data will travel until someone consumes them

As you might noticed there's some asymmetry:
Commands are dispatched through a very small number of parents While data could be potentially broadcasted to a list with 10000 childs

So data broadcast is potentially laggy (although i've tested it on a list with 2000 buttons, it's balls cheap)

To address this you can add names to parent.Emits collection, then parent won't just broadcast data, it'll search for consumers and pass data only to them directly

Also note that some components will already dispatch commands if Command is defined
E.g. CUISlider will send its current value when dragged with provided command name