Ru | En
Async functions in Drakon.Tech are state machines. State machines are a powerful tool for modeling computer systems' behavior. Developers can use state machines to define complex reactions to external events.
Modern web applications benefit most from using state machines because web applications have to deal both with user actions and remote servers. User actions are unpredictable, and remote servers reply unreliably and slowly. Various combinations of events can occur. State machines are good at dealing with such combinations.
The problem with state machines is that few developers know what they are. Luckily, one does not need to know about state machines to work with them in Drakon.Tech.
How to create a state machine in Drakon.Tech
To create a state machine, one needs to make an async function. Async functions in DrakonTech are implemented with state machines.
How to create an instance of a state machine:
- Call an async function without the await keyword and append _create to the function's name. For example, if the async function is called Foo, call Foo_create() function. Pass the parameters to this function as usual. The _create function returns a reference to a new state machine.
- Start the machine with the run() method.
machine = Foo_create(); machine.run();
How to stop a state machine
A state machine in Drakon.Tech stops by itself when the control flow reaches the End icon.
And yet, sometimes, it is necessary to interrupt a state machine at an arbitrary moment.
For example, the user got tired of waiting before all the data got downloaded in the current view and went to another page within the same web application. It is not possible to cancel an operation that has already started on the server, but one can ignore the result of that operation. Stopping a state machine does precisely that.
Set the state field to undefined to stop a state machine.
machine.state = undefined;
How to send a message to a state machine
Two icons make a state machine wait for messages, the Simple input icon and the Choice icon with the receive keyword. Based on these icons, the code generator will add methods that one can call on the outside of the state machine. When such a method gets called, the state machine will receive a message and continue its work. It is vital to call such event methods when the state machine expects that. Otherwise, the state machine will ignore the method calls.
The Simple input icon
Specify the name of the event method and its arguments in the Simple input icon. Drakon.Tech will generate a method for the state machine with the given name and set of arguments. When the control flow reaches the Simple input icon, the state machine will enter the state of waiting until the specified method is called. After the call, the state machine will continue its work, and the arguments will be available as local variables.
In this example, the Simple input icon tells the code generator to create a hello(name) method. The state machine will stop in the Simple input icon and wait until the hello(name) method is called.
One async function can have several Simple input icons. Every Simple input icon corresponds to a state of the state machine. If different Simple input icons specify the same method name, the set of arguments must also be the same.
The Choice icon with the receive keyword
The Simple input icon defines waiting for one type of event. If the state machine needs to wait for events of various kinds, use the Choice icon with the receive keyword.
Specify the names of the event methods and their arguments in the Case icons under the Choice-receive icon. The Choice-receive icon is a point where the state machine temporarily stops; it's one of the states of the state machine (just like the Simple input icon). At this point, the state machine accepts the messages specified by the Case icons.
There are two Choice-receive icons, and therefore two states in this example state machine.
In both states, the state machine accepts two types of messages, left() and right(). The text printed when these messages come in depends only on the message itself, left() or right(). And the color of the message depends on the state of the state machine.
Note that async silhouettes do not always need an End icon. In a typical control algorithm, there is no End icon.
Sending messages from an async function using the Simple output icon
When one state machine sends a message to another state machine, there is a risk that the other state machine will call a method on the first one while the first one is not ready. In other words, the second state machine can call a method on the first one before that first state machine enters a state where it will be able to accept and handle the message.
To avoid the premature processing of a message, use the Simple output icon when one state machine sends a message to another.
The Simple output icon postpones the processing of the message until the next iteration in the browser's event loop.
Use the Simple output icon when one state machine calls a method on another state machine.
The Pause icon
The Pause icon stops the async function for a specified period.
Set the delay in milliseconds in the Pause icon.