UNPKG

react-native-avo-inspector

Version:

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

177 lines (176 loc) 5.53 kB
class PlatformAvoStorage { parseJson(maybeItem) { if (maybeItem !== null && maybeItem !== undefined) { return JSON.parse(maybeItem); } else { return null; } } } class BrowserAvoStorage extends PlatformAvoStorage { constructor() { super(...arguments); this.useFallbackStorage = false; this.fallbackStorage = {}; this.storageInitialized = false; this.onStorageInitFuncs = []; this.shouldLog = false; this.suffix = ""; } init(shouldLog, suffix) { this.shouldLog = shouldLog; this.suffix = suffix; this.initializeStorageWeb(this.isLocalStorageAvailable()); } initializeStorageWeb(isLocalStorageAvailable) { this.storageInitialized = true; if (!isLocalStorageAvailable) { this.useFallbackStorage = true; } this.onStorageInitFuncs.forEach((func) => { func(); }); } isLocalStorageAvailable() { const 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; } } isInitialized() { return this.storageInitialized; } async getItemAsync(key) { return await new Promise((resolve, _reject) => { this.runAfterInit(() => { if (this.useFallbackStorage) { const maybeItem = this.fallbackStorage[key + this.suffix]; resolve(this.parseJson(maybeItem)); } else { if (typeof window !== "undefined") { let maybeItem; try { maybeItem = window.localStorage.getItem(key + this.suffix); } catch (error) { if (this.shouldLog) { console.error("Avo Inspector Storage getItemAsync error:", error); } resolve(null); } resolve(this.parseJson(maybeItem)); } else { resolve(null); } } }); }); } getItem(key) { let maybeItem; if (!this.storageInitialized) { maybeItem = null; } else if (this.useFallbackStorage) { maybeItem = this.fallbackStorage[key + this.suffix]; } else if (process.env.BROWSER) { if (typeof window !== "undefined") { try { maybeItem = window.localStorage.getItem(key + this.suffix); } catch (error) { if (this.shouldLog) { console.error("Avo Inspector Storage getItem error:", error); } } } } return this.parseJson(maybeItem); } setItem(key, value) { this.runAfterInit(() => { if (this.useFallbackStorage) { this.fallbackStorage[key + this.suffix] = JSON.stringify(value); } else { if (typeof window !== "undefined") { try { window.localStorage.setItem(key + this.suffix, JSON.stringify(value)); } catch (error) { if (this.shouldLog) { console.error("Avo Inspector Storage setItem error:", error); } } } } }); } removeItem(key) { this.runAfterInit(() => { if (this.useFallbackStorage) { this.fallbackStorage[key + this.suffix] = null; } else { if (typeof window !== "undefined") { try { window.localStorage.removeItem(key + this.suffix); } catch (error) { if (this.shouldLog) { console.error("Avo Inspector Storage removeItem error:", error); } } } } }); } runAfterInit(func) { if (this.storageInitialized) { func(); } else { this.onStorageInitFuncs.push(func); } } } export class AvoStorage { constructor(shouldLog, suffix = "") { this.Platform = null; this.Platform = "browser"; this.storageImpl = new BrowserAvoStorage(); this.storageImpl.init(shouldLog, suffix); } isInitialized() { return this.storageImpl.isInitialized(); } async getItemAsync(key) { return await this.storageImpl.getItemAsync(key); } getItem(key) { return this.storageImpl.getItem(key); } setItem(key, value) { this.storageImpl.setItem(key, value); } removeItem(key) { this.storageImpl.removeItem(key); } runAfterInit(func) { this.storageImpl.runAfterInit(func); } }