import { Optional } from 'polar-shared/src/util/ts/Optional' ; import { DurationStr , TimeDurations } from 'polar-shared/src/util/TimeDurations' ; import { DurationMS } from 'polar-shared/src/util/TimeDurations' ; Deprecated export class LocalPrefs { public static mark ( key , value = true ) { if ( value ) { this . set ( key , 'true' ) ; } else { this . set ( key , 'false' ) ; } } public static toggle ( key , value = false ) { this . mark ( key , ! this . isMarked ( key , value ) ) ; } /**
     * The initial value is false, After that the value is true.
     */ public static markOnceRequested ( key ) { const result = this . isMarked ( key ) ; this . mark ( key ) ; return result ; } public static async markOnceExecuted ( key , handler , otherwise ? ) { const marked = this . isMarked ( key ) ; if ( marked ) { if ( otherwise ) { await otherwise ( ) ; } return ; } await handler ( ) ; this . mark ( key ) ; } public static isMarked ( key , defaultValue = false ) { const currentValue = this . get ( key ) . getOrElse ( `${ defaultValue }` ) ; return currentValue === 'true' ; } public static isDelayed ( key , duration ) { const durationMS = TimeDurations . toMillis ( duration ) ; const pref = this . get ( key ) . getOrUndefined ( ) ; if ( pref && pref . match ( /[0-9]+/ ) ) { const until = parseInt ( pref , 10 ) ; const now = Date . now ( ) ; if ( now < until ) { return true ; } else { return false ; } } else { return false ; } } /**
     * Compute how long this item is delayed. Returns a positive number for the
     * delay or a negative number if the item delay has expired.
     */ public static computeDelay ( key ) { const pref = this . get ( key ) . getOrUndefined ( ) ; if ( pref && pref . match ( /[0-9]+/ ) ) { const until = parseInt ( pref , 10 ) ; const now = Date . now ( ) ; return until - now ; } else { return undefined ; } } public static markDelayed ( key , duration ) { const durationMS = TimeDurations . toMillis ( duration ) ; const until = Date . now ( ) + durationMS ; this . set ( key , `${ until }` ) ; } /**
     * Return true if the given pref is defined.
     */ public static defined ( key ) { return this . get ( key ) . isPresent ( ) ; } public static get ( key ) { const storage = StorageBackends . get ( ) ; return storage . get ( key ) ; } public static set ( key , value ) { if ( typeof value === 'number' ) { value = value . toString ( ) ; } const storage = StorageBackends . get ( ) ; storage . set ( key , value ) ; } } export class StorageBackends { public static delegate ? : IStorageBackend ; public static get ( ) { if ( this . delegate ) { return this . delegate ; } return new LocalStorageBackend ( ) ; } } interface IStorageBackend { get ( key ) : Optional < string > ; set ( key , value ) : void ; } class LocalStorageBackend { public get ( key ) { return Optional . of ( window . localStorage . getItem ( key ) ) ; } public set ( key , value ) { window . localStorage . setItem ( key , value ) ; } } export class MockStorageBackend { private backing : { [ key ] : string ; } = { } ; public get ( key ) { return Optional . of ( this . backing [ key ] ) ; } public set ( key , value ) { this . backing [ key ] = value ; } }