ssr-web-avo-inspector
Version:
Avo Inspector for web with SSR and web workers support
219 lines (218 loc) • 8.38 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AvoStorage = void 0;
var localforage = require("localforage");
var memoryDriver = require("localforage-driver-memory");
var PlatformAvoStorage = /** @class */ (function () {
function PlatformAvoStorage() {
}
PlatformAvoStorage.prototype.parseJson = function (maybeItem) {
if (maybeItem !== null && maybeItem !== undefined) {
return JSON.parse(maybeItem);
}
else {
return null;
}
};
return PlatformAvoStorage;
}());
var memoryStorage = {};
var BrowserAvoStorage = /** @class */ (function (_super) {
__extends(BrowserAvoStorage, _super);
function BrowserAvoStorage() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.localForage = null;
_this.onStorageInitFuncs = [];
_this.shouldLog = false;
_this.storageInitialized = false;
return _this;
}
BrowserAvoStorage.prototype.init = function (shouldLog, suffix) {
var _this = this;
var _a;
this.shouldLog = shouldLog;
if (this.createLocalForage(suffix)) {
(_a = this.localForage) === null || _a === void 0 ? void 0 : _a.ready().then(function () {
_this.loadDataToMemoryToAvoidAsyncQueries(function () {
_this.onInitializeStorageWeb();
});
}).catch(function (error) {
if (_this.shouldLog && typeof window !== "undefined") {
if (typeof window !== "undefined") {
console.log("Avo Inspector: Using in-memory storage because:", error);
}
else {
console.log("Avo Inspector: Using in-memory storage");
}
}
_this.onInitializeStorageWeb();
});
}
else {
this.onInitializeStorageWeb();
}
};
BrowserAvoStorage.prototype.loadDataToMemoryToAvoidAsyncQueries = function (onLoaded) {
var _this = this;
var _a;
if (this.localForage == null) {
onLoaded();
}
else {
var thisStorage_1 = this;
(_a = this.localForage) === null || _a === void 0 ? void 0 : _a.iterate(function (value, key, _iterationNumber) {
if (typeof value === 'string') {
memoryStorage[key] = value;
}
if (thisStorage_1.shouldLog) {
console.log("Avo Inspector: loaded data from memory", key, value);
}
}).then(function (_) {
onLoaded();
}).catch(function (error) {
if (_this.shouldLog) {
if (typeof window !== "undefined") {
console.log("Avo Inspector load data: Using in-memory storage because:", error);
}
else {
console.log("Avo Inspector load data: Using in-memory storage");
}
}
onLoaded();
});
}
};
BrowserAvoStorage.prototype.onInitializeStorageWeb = function () {
this.storageInitialized = true;
this.onStorageInitFuncs.forEach(function (func) {
func();
});
};
BrowserAvoStorage.prototype.createLocalForage = function (suffix) {
try {
this.localForage = localforage.createInstance({
name: "avoinspector" + suffix
});
this.localForage.defineDriver(memoryDriver);
this.localForage.setDriver([this.localForage.LOCALSTORAGE,
this.localForage.INDEXEDDB, this.localForage.WEBSQL, memoryDriver._driver]);
return true;
}
catch (error) {
return false;
}
};
BrowserAvoStorage.prototype.isInitialized = function () {
return this.storageInitialized;
};
BrowserAvoStorage.prototype.getItemAsync = function (key) {
var thisStorage = this;
return new Promise(function (resolve, _reject) {
thisStorage.runAfterInit(function () {
var maybeItem = /* thisStorage. */ memoryStorage[key];
resolve(thisStorage.parseJson(maybeItem));
});
});
};
BrowserAvoStorage.prototype.getItem = function (key) {
var maybeItem = memoryStorage[key];
return this.parseJson(maybeItem);
};
BrowserAvoStorage.prototype.setItem = function (key, value) {
var _this = this;
var _a;
memoryStorage[key] = JSON.stringify(value);
try {
(_a = this.localForage) === null || _a === void 0 ? void 0 : _a.setItem(key, JSON.stringify(value)).then(function (_) { }).catch(function (error) {
if (_this.shouldLog) {
if (typeof window !== "undefined") {
console.log("Avo Inspector: Using in-memory storage because:", error);
}
else {
console.log("Avo Inspector: Using in-memory storage");
}
}
});
}
catch (error) {
if (this.shouldLog) {
console.log("Avo Inspector: Using in-memory storage because:", error);
}
}
};
BrowserAvoStorage.prototype.removeItem = function (key) {
var _this = this;
var _a;
memoryStorage[key] = null;
try {
(_a = this.localForage) === null || _a === void 0 ? void 0 : _a.removeItem(key).then(function () { }).catch(function (err) {
if (_this.shouldLog) {
if (typeof window !== "undefined") {
console.log('Avo Inspector: Using in-memory storage because:', err);
}
else {
console.log('Avo Inspector: Using in-memory storage');
}
}
});
}
catch (error) {
if (this.shouldLog) {
console.error("Avo Inspector: Using in-memory storage because:", error);
}
}
};
BrowserAvoStorage.prototype.runAfterInit = function (func) {
if (this.isInitialized()) {
func();
}
else {
this.onStorageInitFuncs.push(func);
}
};
return BrowserAvoStorage;
}(PlatformAvoStorage));
var AvoStorage = /** @class */ (function () {
function AvoStorage(shouldLog, suffix) {
if (suffix === void 0) { suffix = ""; }
this.Platform = null;
this.Platform = "browser";
this.storageImpl = new BrowserAvoStorage();
this.storageImpl.init(shouldLog, suffix);
}
AvoStorage.prototype.isInitialized = function () {
return this.storageImpl.isInitialized();
};
AvoStorage.prototype.getItemAsync = function (key) {
return this.storageImpl.getItemAsync(key);
};
AvoStorage.prototype.getItem = function (key) {
return this.storageImpl.getItem(key);
};
AvoStorage.prototype.setItem = function (key, value) {
this.storageImpl.setItem(key, value);
};
AvoStorage.prototype.removeItem = function (key) {
this.storageImpl.removeItem(key);
};
AvoStorage.prototype.runAfterInit = function (func) {
this.storageImpl.runAfterInit(func);
};
return AvoStorage;
}());
exports.AvoStorage = AvoStorage;