parent
5a96217188
commit
ea192a515b
@ -0,0 +1,56 @@ |
||||
import FSM from "./fsm.mjs"; |
||||
import assert from "assert"; |
||||
|
||||
const add_test = () => { |
||||
console.log("Additional function call on state transition passed."); |
||||
} |
||||
|
||||
class TestMachine { |
||||
constructor(data) { |
||||
console.log("DATA IS", data); |
||||
this.data = data; |
||||
} |
||||
|
||||
async open(state, inc) { |
||||
switch(state) { |
||||
case "START": |
||||
console.log("THIS", this); |
||||
this.data.count = this.data.count + inc; |
||||
return "OPEN"; |
||||
default: |
||||
return "ERROR"; |
||||
} |
||||
} |
||||
|
||||
close(state, inc) { |
||||
switch(state) { |
||||
case "OPEN": |
||||
this.data.count = this.data.count + inc; |
||||
return ["CLOSED", add_test]; |
||||
default: |
||||
return "ERROR"; |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
let data = {count: 0}; |
||||
let events = new TestMachine(data); |
||||
let fsm1 = new FSM(data, events); |
||||
|
||||
fsm1.onTransition((fsm) => { |
||||
console.log("On transition: ", fsm.state); |
||||
}); |
||||
|
||||
await fsm1.do('open', 3); |
||||
assert(fsm1.state === "OPEN"); |
||||
assert(data.count === 3); |
||||
|
||||
await fsm1.do('close', 4); |
||||
assert(fsm1.state === "CLOSED"); |
||||
assert(data.count === 7); |
||||
|
||||
// test the errors
|
||||
await fsm1.do('open'); |
||||
assert(fsm1.state === "ERROR"); |
||||
assert(data.count === 7); // still 2 after error
|
Loading…
Reference in new issue