@s-libs/js-core
    Preparing search index...

    Class Persistence<T>

    Get and put objects from/to local storage. They will be (de)serialized as JSON, so be sure that's OK for your objects.

    If localStorage is not available, all methods essentially act as noops.

     // if 'my key' has never been used before
    const persistence = new Persistence('my key');
    persistence.get(); // returns `undefined`

    // now you set it
    persistence.put({ name: 'Robert' });

    // this will work even after the app reloads (e.g. the next week)
    persistence.get(); // returns { name: 'Robert' }

    Type Parameters

    • T
    Index

    Constructors

    Methods

    Constructors

    • Type Parameters

      • T

      Parameters

      • key: string

        The key in local storage at which to find the existing object (if any), and to save it.

      Returns Persistence<T>

    Methods

    • Deletes the saved item from local storage.

      Returns void

    • Retrieves a deserialized copy of the saved object, or undefined if it has not been set.

      Returns undefined | T

    • Serializes obj and saves it in local storage.

      Parameters

      • obj: T

      Returns void