git-off

git off handles large files in git repos
git clone https://noulin.net/git/git-off.git
Log | Files | Refs | README

state_machine.js (1381B)


      1 function AcceptorStateMachine(states, state) {
      2   this.currentState = state || null;
      3   this.states = states || {};
      4 }
      5 
      6 AcceptorStateMachine.prototype.runTo = function runTo(finalState, done, bindObject, inputError) {
      7   if (typeof finalState === 'function') {
      8     inputError = bindObject; bindObject = done;
      9     done = finalState; finalState = null;
     10   }
     11 
     12   var self = this;
     13   var state = self.states[self.currentState];
     14   state.fn.call(bindObject || self, inputError, function(err) {
     15     if (err) {
     16       if (state.fail) self.currentState = state.fail;
     17       else return done ? done.call(bindObject, err) : null;
     18     } else {
     19       if (state.accept) self.currentState = state.accept;
     20       else return done ? done.call(bindObject) : null;
     21     }
     22     if (self.currentState === finalState) {
     23       return done ? done.call(bindObject, err) : null;
     24     }
     25 
     26     self.runTo(finalState, done, bindObject, err);
     27   });
     28 };
     29 
     30 AcceptorStateMachine.prototype.addState = function addState(name, acceptState, failState, fn) {
     31   if (typeof acceptState === 'function') {
     32     fn = acceptState; acceptState = null; failState = null;
     33   } else if (typeof failState === 'function') {
     34     fn = failState; failState = null;
     35   }
     36 
     37   if (!this.currentState) this.currentState = name;
     38   this.states[name] = { accept: acceptState, fail: failState, fn: fn };
     39   return this;
     40 };
     41 
     42 module.exports = AcceptorStateMachine;