Returns a Readable
(Server) / Writable
(Browser) Svelte Store with a reactive binding to a given Storage
adapter
NOTE: Only JSON-compatible values are supported
As a semi-complete example:
import {get} from "svelte/store";
import {storage} from "svelte-commons/lib/stores/browser";
// Below, creating a factory function wrapper around the `localStorage` Web Storage API
const local_storage = storage(window.localStorage, {
// Both `event` and `event_source` tells the Store what event string to
// listen to and what `EventTarget` to listen from for tab-sync
event: "storage",
event_source: window,
// `.prefix` tells the Store to prefix all storage keys with a specific string (defaults to `svelte-commons.`)
prefix: "my_key_prefix."
});
// Now we can use our new `local_storage` factory to make a reactive Store binding
// to a specific `localStorage` key.
const store = local_storage("my_string_key", "I am default");
// Using `get`, we can see the Store is already at its default
console.log(get(store)); // logs: `I am default`
// Since there is nothing set yet, the actual localStorage key is empty
console.log(window.localStorage.getItem("my_key_prefix.my_string_key")) // logs: `null`
// After setting a value, both the Store and `localStorage` have the same value
store.set("But, this is not default");
console.log(
get(store),
window.localStorage.getItem("my_key_prefix.my_string_key")
); // logs: `But, this is not default`, `"But, this is not default"`
// By setting the Store to the default value OR `undefined`, the
// `localStorage` item is removed
store.set("I am default");
console.log(
get(store),
window.localStorage.getItem("my_key_prefix.my_string_key")
); // logs: `I am default`, `null`
Generated using TypeDoc
Represents the Svelte Store factory returned by
storage