<commit_before>import StateMachine from 'torii/lib/state-machine';

var transitionTo = StateMachine.transitionTo;
export default function(session){
  var sm = new StateMachine({
    initialState: 'unauthenticated',

    states: {
      unauthenticated: {
        // Actions
        startOpen: transitionTo('opening'),
        startFetch: transitionTo('fetching')
      },
      authenticated: {
        // Properties
        currentUser: null,
        isAuthenticated: true,
        startClose: transitionTo('closing')
      },
      opening: {
        isWorking: true,
        isOpening: true,
        // Actions
        finishOpen: function(data){
          for (var key in data) {
            if (data.hasOwnProperty(key)) {
              this.states['authenticated'][key] = data[key];
            }
          }
          this.transitionTo('authenticated');
        },
        failOpen: function(errorMessage){
          this.states['error.open'].errorMessage = errorMessage;
          this.transitionTo('error.open')
        }
      },
      fetching: {
        isWorking: true,
        isOpening: true,
        // Actions
        finishFetch: function(currentUser){
          this.states['authenticated'].currentUser = currentUser;
          this.transitionTo('authenticated');
        },
        failOpen: function(errorMessage){
          this.states['error.fetch'].errorMessage = errorMessage;
          this.transitionTo('error.fetch')
        }
      },
      closing: {
        isWorking: true,
        isClosing: true,
        isAuthenticated: true,
        // Actions
        finishClose: function(){
          this.transitionTo('unauthenticated');
        },
        failClose: function(errorMessage){
          this.states['error.close'].errorMessage = errorMessage;
          this.transitionTo('error.close')
        }
      },
      'error.open': {
        // Properties
        errorMessage: null
      },
      'error.fetch': {
        // Properties
        errorMessage: null
      },
      'error.close': {
        // Properties
        errorMessage: null
      }
    }
  });
  sm.session = session;
  return sm;
}<commit_msg>Update statemachine to avoid in-escapable error states<commit_after>import StateMachine from 'torii/lib/state-machine';

var transitionTo = StateMachine.transitionTo;

function copyProperties(data, target) {
  for (var key in data) {
    if (data.hasOwnProperty(key)) {
      target[key] = data[key];
    }
  }
}

function transitionToClearing(target, propertiesToClear) {
  return function(){
    for (var i;i<propertiesToClear.length;i++) {
      this[propertiesToClear[i]] = null;
    }
    this.transitionTo(target);
  }
}

export default function(session){
  var sm = new StateMachine({
    initialState: 'unauthenticated',

    states: {
      unauthenticated: {
        errorMessage: null,
        isAuthenticated: false,
        // Actions
        startOpen: transitionToClearing('opening', ['errorMessage']),
        startFetch: transitionToClearing('fetching', ['errorMessage'])
      },
      authenticated: {
        // Properties
        currentUser: null,
        isAuthenticated: true,
        startClose: transitionTo('closing')
      },
      opening: {
        isWorking: true,
        isOpening: true,
        // Actions
        finishOpen: function(data){
          copyProperties(data, this.states['authenticated']);
          this.transitionTo('authenticated');
        },
        failOpen: function(errorMessage){
          this.states['unauthenticated'].errorMessage = errorMessage;
          this.transitionTo('unauthenticated')
        }
      },
      fetching: {
        isWorking: true,
        isFetching: true,
        // Actions
        finishFetch: function(data){
          copyProperties(data, this.states['authenticated']);
          this.transitionTo('authenticated');
        },
        failFetch: function(errorMessage){
          this.states['unauthenticated'].errorMessage = errorMessage;
          this.transitionTo('unauthenticated')
        }
      },
      closing: {
        isWorking: true,
        isClosing: true,
        isAuthenticated: true,
        // Actions
        finishClose: function(){
          this.transitionTo('unauthenticated');
        },
        failClose: function(errorMessage){
          this.states['unauthenticated'].errorMessage = errorMessage;
          this.transitionTo('unauthenticated')
        }
      }
    }
  });
  sm.session = session;
  return sm;
}
{'fraction_non_alphanumeric': 0.11651785714285715, 'fraction_numerical': 0.0, 'mean_word_length': 2.140154169586545, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6520626', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1187, 'n_words': 220}