@pyramine/storage-space
v1.0.3
Published
Make accessing the Web-Storage-API more type-safe.
Downloads
5
Readme
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);
});