leodutra
11/9/2015 - 6:32 PM

User storage class for simple store2.js user data management (using namespacing). Requires store2.js v2.x (https://github.com/nbubna/store)

User storage class for simple store2.js user data management (using namespacing). Requires store2.js v2.x (https://github.com/nbubna/store)

if (typeof store !== 'function') {
  throw 'UserStorage requires store2.js v2.x (https://github.com/nbubna/store)';
}

function UserStorage(appName, uid) {
  if (this instanceof UserStorage) {
    if (typeof appName !== 'string' || appName.length === 0) {
      throw 'UserStorage::constructor: invalid appName. Must be a string (not blank).';
    }
    if (typeof uid !== 'string' || uid.length === 0) {
      throw 'UserStorage::constructor: invalid uid. Must be a string (not blank).';
    }
    this._store = store.namespace(appName + '.' + uid);
  }
}


UserStorage.prototype = {

  store: function(key, value) {
    this._store.set(key, value);
  },

  get: function(key) {
    return this._store.get(key);
  },

  list: function() {
    return this._store.getAll();
  },

  clear: function(key) {
    this._store.clearAll();
  }
};