LocalStorage.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. /* global localStorage */
  7. var Base = require('./Base');
  8. var LocalStorage = function (_Base) {
  9. _inherits(LocalStorage, _Base);
  10. function LocalStorage() {
  11. _classCallCheck(this, LocalStorage);
  12. return _possibleConstructorReturn(this, (LocalStorage.__proto__ || Object.getPrototypeOf(LocalStorage)).apply(this, arguments));
  13. }
  14. _createClass(LocalStorage, [{
  15. key: 'read',
  16. value: function read() {
  17. var data = localStorage.getItem(this.source);
  18. if (data) {
  19. return this.deserialize(data);
  20. } else {
  21. localStorage.setItem(this.source, this.serialize(this.defaultValue));
  22. return this.defaultValue;
  23. }
  24. }
  25. }, {
  26. key: 'write',
  27. value: function write(data) {
  28. localStorage.setItem(this.source, this.serialize(data));
  29. }
  30. }]);
  31. return LocalStorage;
  32. }(Base);
  33. module.exports = LocalStorage;