@pyramine/storage-space
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Storage-Space

This packages provides a way to predefine the use localStorage and sessionStorage keys for your application. It also provides some additional type-safety for serializing values to the storage engine.

Installation

npm install @pyramine/storage-space

Usage

First we will define our spaces:

import {sessionSpace, localSpace} from "@pyramine/storage-space";

export const storage = {
    first: sessionSpace<string>('first', 'inital-value'),
    second: localSpace<{ complex: string }>('second'),
};


storage.first.set('myValue');
storage.second.set({complex: 'myValue'});

storage.first.value; // 'myValue'
storage.second.value; // { complex: 'myValue' }

Serializers

This package provides some default serializers:

  • JsonSerializer (used by default)
  • Base64Serializer

You can switch the serializer at any given time:

import {Base64Serializer} from "@pyramine/storage-space";

storage.first.serializeUsing(new Base64Serializer());

You can even define custom serializers by creating class which implements our StorageSerializer interface.

Events

The space emit events you can listen on:

name description
change Fired when ever the underlying value change. Even when the value got removed.
remove Similar to change but only fires if the value got removed.
storage.first.on('change', (event) => {
    console.log(event);
});
storage.first.once('change', (event) => {
    console.log(event);
});

Readme

Keywords

none

Package Sidebar

Install

npm i @pyramine/storage-space

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

84.8 kB

Total Files

7

Last publish

Collaborators

  • christian98