@difizen/mana-core
Version:
97 lines (95 loc) • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.localStorageService = exports.LocalStorageService = void 0;
var _manaCommon = require("@difizen/mana-common");
var _debug = require("./debug");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var LocalStorageService = exports.LocalStorageService = /*#__PURE__*/function () {
function LocalStorageService() {
_classCallCheck(this, LocalStorageService);
this.usePathInPrefix = false;
this.storage = {};
this.onDiskQuotaExceededEmitter = new _manaCommon.Emitter();
this.logger = _debug.debug;
if (typeof window !== 'undefined' && window.localStorage) {
this.storage = window.localStorage;
this.testLocalStorage();
} else {
this.logger.log("The browser doesn't support localStorage.");
this.storage = {};
}
}
_createClass(LocalStorageService, [{
key: "onDiskQuotaExceeded",
get: function get() {
return this.onDiskQuotaExceededEmitter.event;
}
}, {
key: "setData",
value: function setData(key, data) {
if (data !== undefined) {
try {
var value = JSON.stringify(data);
this.storage[this.prefix(key)] = value;
} catch (e) {
this.onDiskQuotaExceededEmitter.fire();
}
} else {
delete this.storage[this.prefix(key)];
}
return;
}
}, {
key: "getData",
value: function getData(key, defaultValue) {
var result = this.storage[this.prefix(key)];
if (result === undefined) {
return defaultValue;
}
try {
return JSON.parse(result);
} catch (e) {
return result;
}
}
}, {
key: "prefix",
value: function prefix(key) {
var pathname = typeof window === 'undefined' ? '' : window.location.pathname;
var prefix = this.usePathInPrefix ? "mana:".concat(pathname) : 'mana';
return "".concat(prefix, ":").concat(key);
}
/**
* Verify if there is still some spaces left to save another workspace configuration into the local storage of your browser.
* If we are close to the limit, use a dialog to notify the user.
*/
}, {
key: "testLocalStorage",
value: function testLocalStorage() {
var keyTest = this.prefix('Test');
try {
this.storage[keyTest] = JSON.stringify(new Array(60000));
} catch (error) {
this.onDiskQuotaExceededEmitter.fire();
} finally {
var _this$storage$removeI, _this$storage;
(_this$storage$removeI = (_this$storage = this.storage).removeItem) === null || _this$storage$removeI === void 0 || _this$storage$removeI.call(_this$storage, keyTest);
}
}
}, {
key: "clearStorage",
value: function clearStorage() {
var _this$storage$clear, _this$storage2;
(_this$storage$clear = (_this$storage2 = this.storage).clear) === null || _this$storage$clear === void 0 || _this$storage$clear.call(_this$storage2);
}
}]);
return LocalStorageService;
}();
var localStorageService = exports.localStorageService = new LocalStorageService();