Javascript API ​
When generating a JS/TS class, it has @yantrix/core as dependency and implements IAutomata interface. The exported class also has static properties to facilitate operating the FSM and its constants.
The snippets below use the Quick Start slider diagram.
Identifier ​
The className option names the generated class. Its static id combines that name with a generation timestamp.
shell
$ yantrix codegen ./slider.mermaid --outfile slider_controller.js --language JavaScript --className Sliderjavascript
import { Slider } from './slider_controller.js';or
javascript
import Slider from './slider_controller.js';javascript
Slider.id.startsWith('Slider_'); // trueState Helpers ​
Internally, IAutomata operates on numeric states. To address States from the diagram, they must be wrapped in a helper:
javascript
import Slider from './slider_controller.js';
Slider.getState('ENABLED'); // returns the numeric ID of this stateThere's also a helper to check if the FSM is in a particular State right now:
javascript
const slider = new Slider();
Slider.hasState(slider, 'ENABLED'); // true
// this is the same as
Slider.getState('ENABLED') === slider.getContext().state;Action Helpers ​
To invoke an Action, a dispatch method should be used. Since it accepts numeric ids, they also have to be mapped.
javascript
Slider.getAction('DISABLE');
Slider.getAction('ENABLE');Since most of the time you need to pass a Payload object into dispatch, there's a helper to create a Payload:
javascript
const payload = Slider.createAction('RESET', {
min: 0,
max: 50,
});
slider.dispatch(payload);Dictionaries ​
Generated modules export two kinds of dictionaries both for Actions and States, that are suggested to be used as reference constants when addressing their identifiers:
javascript
import { actionsDictionary, actionsMap } from './slider_controller.js';
actionsMap.DISABLE === 'DISABLE'; // true
actionsDictionary.DISABLE === Slider.getAction('DISABLE'); // true
slider.dispatch(Slider.createAction(actionsMap.DISABLE, {}));Same dictionaries are provided for States:
javascript
import { statesDictionary, statesMap } from './slider_controller.js';
statesDictionary.ENABLED === Slider.getState(statesMap.ENABLED); // trueTypescript ​
In typescript versions Dictionaries are available as types too:
typescript
import type { statesMap, TActionsSlider } from './slider_controller.js';
type TActionLiteral = TActionsSlider;
type TStateLiteral = keyof typeof statesMap;