@t8/persistent-store
v0.1.0
Published
A container for data persistent across page reloads
Downloads
42
Maintainers
Readme
T8 Persistent Store
A container for data persistent across page reloads
An instance of PersistentStore acts like a regular store from T8 Store (that it extends) with its initial value being restored from the browser storage.
Usage
import { PersistentStore } from "@t8/persistent-store";
let counterStore = new PersistentStore(0, { key: "counter" });Whenever updated, counterStore above will save its value to the "counter" key of localStorage. (Add session: true to the second parameter of the constructor to use sessionStorage instead of localStorage.)
The following call signals the store to read the value from the browser storage, which can be used once or multiple times after creating the store:
counterStore.emit("sync");The following call can be used instead, if it's desirable to sync a store with the browser storage just once regardless of the number of sync calls (coming from multiple independent parts of the code, for example):
counterStore.emit("synconce"); // Syncs once disregarding subsequent sync callsThe way the store value gets saved to and restored from a browser storage entry (including filtering out certain data or otherwise rearranging the saved data) can be redefined by setting options.serialize and options.deserialize in new PersistentStore(value, options). By default, these options act like JSON.stringify() and JSON.parse() respectively.
