Sync variable's value with persistent storage (LocalStorage by default)

const theme = new Persist('theme.current', {default: 'os'});
console.log(theme.value); // Output: os

theme.value = 'light'; // Any changes to `.value` will automatically sync with localStorage

location.reload(); // Simulate refresh
console.log(theme.value); // Output: light

Type Parameters

  • T

Constructors

Properties

Accessors

Methods

Constructors

  • Type Parameters

    • T

    Parameters

    Returns Persist<T>

Properties

key: string
options: PersistOptions<T>

Accessors

  • get value(): T
  • Current value or default if undefined

    Returns T

  • set value(v): void
  • Set value with proxy object wrapper to sync future changes

    Parameters

    • v: undefined | T

    Returns void

Methods

  • Delete value from storage

    Returns void

  • Load value from storage

    Returns void

  • Save current value to storage

    Returns void

  • Return value as JSON string

    Returns string

    Stringified object as JSON

  • Return current value

    Returns T

    Current value

  • Callback function which is run when there are changes

    Parameters

    • fn: ((value: T) => any)

      Callback will run on each change; it's passed the next value & it's return is ignored

        • (value): any
        • Parameters

          • value: T

          Returns any

    Returns (() => void)

    Function which will unsubscribe the watch/callback when called

      • (): void
      • Returns void