comparison core/src/js/components/StateMachine.js @ 29:acdcdf1a8d21

repository reorganized
author cin
date Tue, 26 Jun 2018 19:35:44 +0300
parents src/implab/components/StateMachine.js@7d7059d2a810
children
comparison
equal deleted inserted replaced
28:d796bbbe558c 29:acdcdf1a8d21
1 define([ "dojo/_base/declare", "../safe", "../text/format" ], function(declare, safe, format) {
2 return declare(null, {
3 states : null,
4
5 current : null,
6
7 constructor : function(opts) {
8 safe.argumentNotNull(opts, "opts");
9 safe.argumentNotNull(opts.states, "opts.states");
10 safe.argumentNotNull(opts.initial, "opts.initial");
11
12 this.states = opts.states;
13 this.current = opts.initial;
14
15 if (safe.isNull(this.states[this.current]))
16 throw new Error("Invalid initial state " + this.current);
17 },
18
19 move : function(input, noThrow) {
20 safe.argumentNotNull(input, "input");
21
22 var next = this.states[this.current][input];
23 if(safe.isNull(next)) {
24 if (noThrow)
25 return false;
26 else
27 throw new Error(format("Invalid transition {0}-{1}->?", this.current, input));
28 } else {
29 this.current = next;
30 return true;
31 }
32 }
33 });
34 });