UNPKG

avo-inspector

Version:

[![npm version](https://badge.fury.io/js/avo-inspector.svg)](https://badge.fury.io/js/avo-inspector)

337 lines (336 loc) 12.6 kB
"use strict"; 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 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 AndroidAvoStorage = /** @class */ (function (_super) { __extends(AndroidAvoStorage, _super); function AndroidAvoStorage() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.androidMemoryDataToAvoidAsyncQueries = {}; _this.storageLib = null; _this.AsyncStorage = null; _this.memoryStorageInitialized = false; _this.onStorageInitFuncs = []; _this.shouldLog = false; return _this; } AndroidAvoStorage.prototype.init = function (shouldLog) { var _this = this; if (!process.env.BROWSER) { this.storageLib = require("@react-native-async-storage/async-storage"); this.shouldLog = shouldLog; this.AsyncStorage = this.storageLib.default; this.loadAndroidDataToMemoryToAvoidAsyncQueries(function () { _this.initializeStorageAndroid(); }); } }; AndroidAvoStorage.prototype.loadAndroidDataToMemoryToAvoidAsyncQueries = function (onLoaded) { var _this = this; this.AsyncStorage.getAllKeys().then(function (keys) { return _this.AsyncStorage.multiGet(keys).then(function (keyVals) { if (_this.shouldLog) { console.log("Avo Inspector: android loaded data from memory"); } keyVals.forEach(function (keyVal) { var key = keyVal[0]; _this.androidMemoryDataToAvoidAsyncQueries[key] = keyVal[1]; if (_this.shouldLog) { console.log(key, keyVal[1]); } }); onLoaded(); }); }); }; AndroidAvoStorage.prototype.initializeStorageAndroid = function () { this.memoryStorageInitialized = true; this.onStorageInitFuncs.forEach(function (func) { func(); }); }; AndroidAvoStorage.prototype.isInitialized = function () { return this.storageLib && this.memoryStorageInitialized; }; AndroidAvoStorage.prototype.getItemAsync = function (key) { var maybeItem = this.AsyncStorage.getItem(key); return maybeItem.then(function (storedItem) { return storedItem != null ? JSON.parse(storedItem) : null; }); }; AndroidAvoStorage.prototype.getItem = function (key) { var maybeItem = this.androidMemoryDataToAvoidAsyncQueries[key]; return this.parseJson(maybeItem); }; AndroidAvoStorage.prototype.setItem = function (key, value) { this.AsyncStorage.setItem(key, JSON.stringify(value)); this.androidMemoryDataToAvoidAsyncQueries[key] = JSON.stringify(value); }; AndroidAvoStorage.prototype.removeItem = function (key) { this.AsyncStorage.removeItem(key); this.androidMemoryDataToAvoidAsyncQueries[key] = null; }; AndroidAvoStorage.prototype.runAfterInit = function (func) { if (this.memoryStorageInitialized === true) { func(); } else { this.onStorageInitFuncs.push(func); } }; return AndroidAvoStorage; }(PlatformAvoStorage)); var IosAvoStorage = /** @class */ (function (_super) { __extends(IosAvoStorage, _super); function IosAvoStorage() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.reactNative = null; return _this; } IosAvoStorage.prototype.init = function (_shouldLog) { if (!process.env.BROWSER) { this.reactNative = require("react-native"); } }; IosAvoStorage.prototype.isInitialized = function () { return this.reactNative != null; }; IosAvoStorage.prototype.getItemAsync = function (key) { var Settings = this.reactNative.Settings; var maybeItem = Settings.get(key); return Promise.resolve(this.parseJson(maybeItem)); }; IosAvoStorage.prototype.getItem = function (key) { var Settings = this.reactNative.Settings; var maybeItem = Settings.get(key); return this.parseJson(maybeItem); }; IosAvoStorage.prototype.setItem = function (key, value) { var _a; var Settings = this.reactNative.Settings; Settings.set((_a = {}, _a[key] = JSON.stringify(value), _a)); }; IosAvoStorage.prototype.removeItem = function (key) { var _a; var Settings = this.reactNative.Settings; Settings.set((_a = {}, _a[key] = null, _a)); }; IosAvoStorage.prototype.runAfterInit = function (func) { func(); }; return IosAvoStorage; }(PlatformAvoStorage)); var BrowserAvoStorage = /** @class */ (function (_super) { __extends(BrowserAvoStorage, _super); function BrowserAvoStorage() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.useFallbackStorage = false; _this.fallbackStorage = {}; _this.storageInitialized = false; _this.onStorageInitFuncs = []; _this.shouldLog = false; return _this; } BrowserAvoStorage.prototype.init = function (shouldLog) { this.shouldLog = shouldLog; this.initializeStorageWeb(this.isLocalStorageAvailable()); }; BrowserAvoStorage.prototype.initializeStorageWeb = function (isLocalStorageAvailable) { this.storageInitialized = true; if (isLocalStorageAvailable === false) { this.useFallbackStorage = true; } this.onStorageInitFuncs.forEach(function (func) { func(); }); }; BrowserAvoStorage.prototype.isLocalStorageAvailable = function () { var uid = new Date().toISOString(); try { window.localStorage.setItem(uid, uid); if (window.localStorage.getItem(uid) === uid) { window.localStorage.removeItem(uid); return true; } else { return false; } } 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 () { if (thisStorage.useFallbackStorage === true) { var maybeItem = thisStorage.fallbackStorage[key]; resolve(thisStorage.parseJson(maybeItem)); } else { if (typeof window !== "undefined") { var maybeItem = void 0; try { maybeItem = window.localStorage.getItem(key); } catch (error) { if (thisStorage.shouldLog) { console.error("Avo Inspector Storage getItemAsync error:", error); } resolve(null); } resolve(thisStorage.parseJson(maybeItem)); } else { resolve(null); } } }); }); }; BrowserAvoStorage.prototype.getItem = function (key) { var maybeItem; if (this.storageInitialized === false) { maybeItem = null; } else if (this.useFallbackStorage === true) { maybeItem = this.fallbackStorage[key]; } else if (process.env.BROWSER) { if (typeof window !== "undefined") { try { maybeItem = window.localStorage.getItem(key); } catch (error) { if (this.shouldLog) { console.error("Avo Inspector Storage getItem error:", error); } } } } return this.parseJson(maybeItem); }; BrowserAvoStorage.prototype.setItem = function (key, value) { var _this = this; this.runAfterInit(function () { if (_this.useFallbackStorage === true) { _this.fallbackStorage[key] = JSON.stringify(value); } else { if (typeof window !== "undefined") { try { window.localStorage.setItem(key, JSON.stringify(value)); } catch (error) { if (_this.shouldLog) { console.error("Avo Inspector Storage setItem error:", error); } } } } }); }; BrowserAvoStorage.prototype.removeItem = function (key) { var _this = this; this.runAfterInit(function () { if (_this.useFallbackStorage === true) { _this.fallbackStorage[key] = null; } else { if (typeof window !== "undefined") { try { window.localStorage.removeItem(key); } catch (error) { if (_this.shouldLog) { console.error("Avo Inspector Storage removeItem error:", error); } } } } }); }; BrowserAvoStorage.prototype.runAfterInit = function (func) { if (this.storageInitialized === true) { func(); } else { this.onStorageInitFuncs.push(func); } }; return BrowserAvoStorage; }(PlatformAvoStorage)); var AvoStorage = /** @class */ (function () { function AvoStorage(shouldLog) { this.Platform = null; if (!process.env.BROWSER) { var reactNative = require("react-native"); this.Platform = reactNative.Platform.OS; if (this.Platform === "android") { this.storageImpl = new AndroidAvoStorage(); } else if (this.Platform === "ios") { this.storageImpl = new IosAvoStorage(); } else { throw new Error("Avo Inpector is not supported on " + this.Platform); } } else { this.Platform = "browser"; this.storageImpl = new BrowserAvoStorage(); } this.storageImpl.init(shouldLog); } 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;