LocalForage Test

HTML

<script src="http://peterbenoit.com/dev/AppInReact/js/localforage.min.js"></script>
<h3>Open console log to see results</h3>

JavaScript

var storageKey = 'my key';

var testStates = {
  configure: function(ssm) {
    localforage.config({
      driver: [localforage.WEBSQL,
        localforage.INDEXEDDB,
        localforage.LOCALSTORAGE
      ],
      name: 'WebSQL-Rox'
    });
    console.log('localforage configured with default driver:', localforage.driver());
    ssm.changeState('defaultSet');
  },

  defaultSet: function(ssm) {
    localforage.setItem(storageKey, 'A string!', function(value) {
      console.log('successfully saved', value, 'to key', storageKey)
      ssm.changeState('defaultGet');
    });
  },

  defaultGet: function(ssm) {
    localforage.getItem(storageKey, function(value) {
      console.log('successfully retrieved', value, 'from key', storageKey)
      ssm.changeState('end');
    });
  },

  end: function(ssm) {
    console.log("Test successfully completed!");
  }
};

function SimpleStateMachine(states) {
  this.states = states;
  this.state = null;

  this.changeState = function(newState) {
    var oldState = this.state;
    this.state = newState;
    console.debug('Changing states:', oldState, 'to', newState);
    this.states[newState](this);
    return oldState;
  };
}

ssm = new SimpleStateMachine(testStates);
ssm.changeState('configure');