OOSMOS for JavaScript (which is written in TypeScript and transpiled to JavaScript) is an open source,
easy-to-use hierarchical state machine class that
can be used
with an environment that has a module loader, e.g. node.js, or a
browser.
NOTE:OOSMOS stands for Object Oriented State Machine Operating System.
OOSMOS is a small footprint C/C++ state machine based operating system targeting the industrial automation space.
(See oosmos.com.) This JavaScript implementation borrows from the Object-Oriented and
State Machine elements of OOSMOS but the Operating System elements are supported by the JavaScript runtime.
1.1 Features
Very readable encapsulated state machine structure.
Simple -- less than 400 lines of code.
Same code runs in a browser or Node.js.
Supports arbitrarily deep hierarchical state machines.
Supports state local variables. (Ideal for caching jQuery elements.)
Simple API: Only 5 principal APIs.
Can run multiple state machines concurrently.
Event functions can pass arguments to the ENTER
function via the Transition API.
Simple Timeout - An application that toggles
between two states based on timeouts.
Nested Timeout - Is similar to the Simple Timeout
demo, but demonstrates the use of more than one timeout active at a time.
jQuery - Demonstrates the use of OOSMOS to manage
the display of application elements and user interaction.
2. Simple Timeout
This demo serves as a "hello world" of OOSMOS. It merely toggles between
two states based on timeouts. It also shows how to enable state machine
debugging.
2.1 Simple Timeout State Chart
Example 1. Simple Timeout
2.2 Simple Timeout Debug Output
2.3 Simple Timeout State Machine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { StateMachine } from 'OOSMOS';
class SimpleTimeoutTest extends StateMachine {
constructor() {
super({ DEFAULT: 'A',
A: {
ENTER: function() {
this.Print("In state A");
this.SetTimeoutSeconds(4);
},
TIMEOUT: function() {
this.Transition('B');
},
},
B: {
ENTER: function() {
this.Print("In state B");
this.SetTimeoutSeconds(1);
},
TIMEOUT: function() {
this.Transition('A');
},
},
});
}
}
const pSimpleTimeoutTest = new SimpleTimeoutTest();
With the advent of AJAX and Websockets, more and more of the logic of a web application
can run in the browser. In fact, I recently developed a feature-rich
messaging application that loads only one
page of HTML and then, under control of a set of OOSMOS.js state machines,
uses a Websockets connection
to interact with the server exchanging snippets of JSON.
Here is how it works: Most of the HTML that is loaded is hidden from
view using style="display:none"
and then the state machine based JavaScript makes the appropriate elements visible based on
user interaction. What follows is a simple live example. First, take a look
at the state machine. We start out in the Idle state and then
transition to the Active state when you press the
eStart button which sends the eStart event.
Follow the rest
of the logic by inspecting the state chart and the state machine.
As we ENTER states, we show the corresponding <div>
in the HTML. Conversely, we hide them in the EXIT. (See
lines 9 and 17.)
We can set up jQuery event handlers inside the context where they
apply and unbind them when we are done. (See lines
25-26 and 30-31.)
State local variables are created on lines
37-41 and 76-80 by
replacing the object with a function that declares the state-local variables
and then returns the object.