Sample Designs
Checkboxes
Basic Checkbox
This checkbox starts in unchecked state and can be switched on and off by a single Action
Enabled/Disabled
Since a checkbox can be disabled both with checkmark and without it, it requires two States for disabled condition.
Direct assignment
With two extra Actions it's now possible to set the desired State of a checkbox to a particular condition without prior knowledge of its current State. For instance, CHECK Action is guaranteed to bring the checkbox into ON State, unless it was disabled.
Overwriting disabled states
The same Actions as above can be applied to disabled States, allowing to change the value of checkbox without enabling it. However, not that TOGGLE Action will not work in disabled States.
Radio Group
Here we create an FSM that stores a list of options in its Context and allows to select one of them by dispathing a SELECT Action.
Here we also create a single DISABLED State, because we uphoisted the state of many controls rather than controlling a single one, as in Checkbox example, allowing us to preserve the visible state of every radio option when being DISABLED.
Also, a special UNSET Action, that removes the selection: it works by not passing a Payload, which triggers $index = -1 fallback to default value
Data Loader
The most notable example of a stateful control object in web programming is an asynchronous request, or, in general Javascript, a Promise type. The most basic variant of Mermaid representation of it is already a Yantrix code for an async call:
the more beautiful and best-practice approach to it though would look like this:
Dropdown Control
+Initmarks that INIT is the initialStateof theFSM.+ByPassalso implies that transition through thisStateis synchronous, i.e. theStateis intermediary. That is emphasized by a special descriptor of the outgoingAction: [-]#{items, selectedIndex}describes a shape ofContextfor allStates.itemsis the list of dropdown values, andselectedIndexstores currently selected item. Without extra expressions these values are copied from the precedingContext#{items=[], selectedIndex = 0}sets the initial value for thatContext#{items, selectedIndex} <= sortBy($optionsList, 'id'), 0fills bothContextproperties fromPayload:itemsis a sortedoptionsListproperty, assuming it's a List of Objects that have property ofid.selectedIndexis set to 0, when the list of options is externally updated
Event integration
There's no big reason to create a FSM for merely a dropdown unless you want to connect it to the global Event Stack and build something bigger on Yantrix:
subscribe/click => OPENin CLOSED state produces OPENActionon incomingclickEvent, which transitions theFSMinto OPEN state- likewise,
subscribe/click SELECT (index)produces SELECTActionand passesindexproperty fromEvent Metato itsPayload, which transitions theFSMinto the SELECTEDState subscribe/clickOutside => CLOSEproduces a CLOSEActionto return the dropdown to the originalState(CLOSED)- Both CLOSED and OPEN
Statesemit correspondingEvents, that are pipelined intoEvent Busand connect the component to others. - SELECTED is another sort of intermediary State: while
emit/selected (index) <= #{ selectedIndex }lets theEvent Busknow which item was selected, at the same timesubscribe/selected => CLOSEtransitions theFSMback to CLOSEDStatevia CLOSEAction. It behaves similar to INITState– theFSMgoes through it. However, unlike that case, here it will emitEventand wait for its settlement before it runs further processing.