Mercurial > pub > ImplabJs
comparison src/implab/components/StateMachine.js @ 0:fc2517695ee1
Initial commit, draft import of existing work
| author | cin |
|---|---|
| date | Thu, 01 Jun 2017 13:20:03 +0300 |
| parents | |
| children | 7d7059d2a810 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:fc2517695ee1 |
|---|---|
| 1 define([ "dojo/_base/declare", "./safe", "./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 }); |
