Skip to content

Instantly share code, notes, and snippets.

@egonelbre
Created August 23, 2012 15:27
Show Gist options
  • Save egonelbre/3437746 to your computer and use it in GitHub Desktop.
Save egonelbre/3437746 to your computer and use it in GitHub Desktop.
State Machine
function Machine(first, $){
var cur = {}, next = $[first];
var self = {
go : function(to){ next = next ? next : ($[to] || $.undefined); },
trigger : function(event){ var t = cur.tx && cur.tx[event]; t && self.go(t); }
};
return function(){
if(next){
cur.exit && cur.exit.call(self);
cur = next; next = undefined;
self.__proto__ = cur.data;
cur.enter && cur.enter.call(self);
}
cur.fn && cur.fn.call(self);
};
};
var m = Machine("tick", {
undefined : {
fn : function(){
console.log("ERROR: NO STATE!");
this.trigger("recover");
},
tx : {
recover : "tick"
}
},
tick : {
enter : function(){
console.log("entered tick");
},
fn : function(){
console.log("tick");
this.go("tock");
}
},
tock : {
data : {
counter : 0
},
exit : function(){
this.counter += 1;
},
fn : function(){
this.counter += 1;
console.log("tock : " + this.counter);
if(this.counter > 5)
this.go("invalid");
if(this.counter > 3)
this.trigger("forward");
},
tx : {
forward : "tick"
}
}
});
console.log("=========================");
for(var i = 0; i < 15; i += 1)
m();
@zzzgit
Copy link

zzzgit commented Apr 26, 2022

Thanks, bro.

I tried to write a typesafe version which is written in Typescript.

This is the transpiled result:

var tsCustomError = require('ts-custom-error');

class StateMachine {
    _currentState;
    _states;
    _logic;
    constructor(states, initialState, logic) {
        this._states = states;
        this._currentState = initialState;
        this._logic = logic;
    }
    getAllStates() {
        return [...this._states];
    }
    getCurrentState() {
        return this._currentState;
    }
    _verifyState(value) {
        if (!this._states.includes(value)) {
            throw new tsCustomError.CustomError(`[StateMachine][_verifyState]: value "${value}" is not defined in initial states!`);
        }
    }
    switchBy(...factors) {
        const newState = this._logic(this._currentState, ...factors);
        this._verifyState(newState);
        this._currentState = newState;
        return this._currentState;
    }
}

exports.StateMachine = StateMachine;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment