Options
All
  • Public
  • Public/Protected
  • All
Menu
description

This class handles persistence using in a simple form using either of window.localStorage or a Storage extending object, or a forward predicate that returns values for specific keys.

The persistence uses a simple key-value format where scalar value types are kept in their original type whereas objects are flattened to their corresponding JSON representation.

Note that if you are using an arrow function to read the key-value store, persistence will not be possible.

example

Using the StorageDriver class

  const driver = new StorageDriver();
console.log(driver.read('storage_key')); // returns scalar
console.log(driver.read('storage_key', 'fallback')); // returns scalar
console.log(driver.write('storage_key', value)); // no return



Properties

param storageProvider

The storage provider used for persistence (defaults to window.localStorage).

since

v0.1.0

Hierarchy

  • StorageDriver

Index

Constructors

Properties

Methods

Constructors

  • new StorageDriver(provider?: WindowLocalStorage | Storage | ((key: string) => string)): StorageDriver
  • Constructs a storage driver instance setting the actual storage provider as storageProvider property. By default, this class uses window.localStorage.

    access

    public

    Parameters

    • provider: WindowLocalStorage | Storage | ((key: string) => string) = localStorage

      The storage provider or arrow function, i.e. the "location".

    Returns StorageDriver

Properties

storageProvider: WindowLocalStorage | Storage | ((key: string) => string)

The storage provider used for persistence using this storage driver. This should generally use window.localStorage as a default.

You can also provide an arrow function which consists in reading one value of said persistence layer. Note that the arrow function must return a string formatted value.

var

{WindowLocalStorage | Storage | ((key: string) => string)}

Methods

  • Helper method to read one value from the linked storageProvider using either the getItem method or calling the forward predicate if an arrow function is passed to overwrite the persistence for the active language definition.

    access

    public

    Parameters

    • storageKey: string

      The storage key that must be read.

    • Optional fallbackValue: ScalarValueType

      The fallback value in case the value does not exist.

    Returns ScalarValueType

  • Helper method to write one value using the linked storageProvider using the setItem method.

    Note that persistence is not available if using arrow functions as the storage provider. Currently persistence is only available in browser runtime with window.localStorage.

    access

    public

    Parameters

    Returns void

Generated using TypeDoc