import { storeKey } from "./constants" ; import { IStore } from "./IStore" ; import { MemoryStore } from "./MemoryStore" ; import { localStorageAvailable } from "./utils" ; export default class OpenLoginStore { private static instance : OpenLoginStore ; public storage : IStore ; private constructor ( storage ) { this . storage = storage ; try { if ( ! storage . getItem ( storeKey ) ) { this . resetStore ( ) ; } } catch ( error ) { } } static getInstance ( ) { if ( ! this . instance ) { this . instance = new this ( localStorageAvailable ? localStorage : new MemoryStore ( ) ) ; } return this . instance ; } toJSON ( ) { return this . storage . getItem ( storeKey ) ; } resetStore ( ) { const currStore = this . getStore ( ) ; this . storage . setItem ( storeKey , JSON . stringify ( { } ) ) ; return currStore ; } getStore ( ) { return JSON . parse ( this . storage . getItem ( storeKey ) ) ; } get < T > ( key ) { const store = JSON . parse ( this . storage . getItem ( storeKey ) ) ; return store [ key ] ; } set < T > ( key , value ) { const store = JSON . parse ( this . storage . getItem ( storeKey ) ) ; store [ key ] = value ; this . storage . setItem ( storeKey , JSON . stringify ( store ) ) ; } }