# misc/persistedstore (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/persistedstore/

Persistent store.

## PersistedStore extends Object

Persistent store.
- Stores a state object as JSON under one key, and notifies subscribers when it changes.
- Falls back to the default value if the version number (version) differs or parsing fails.
- Standardizes the repeated "localStorage + JSON + listener" skeleton for things like leaderboards / user settings.

Example: Save settings and get notified on change

```js
import { PersistedStore } from "./build/vanilla.js";

const store = new PersistedStore("example.settings", { volume: 1, language: "en" }, 1);
const subscription = store.subscribe((state) => {
	console.log("changed", state.volume);
});
store.set({ volume: 0.5 });
const state = store.get();
console.log(state.volume, state.language);
store.unsubscribe(subscription);
store.reset();
```

- `constructor(storageKey, defaultState = {}, version = 1)` (constructor, since 0.3.0): Constructor.
  - `storageKey` string: Storage key that holds the state.
  - `defaultState` object: Default state. (a copy is used when nothing is stored or the version differs)
  - `version` number: Version number of the saved format. (falls back to the default state if it differs from the stored version)
- `get()` (method, since 0.3.0): Returns the state. (not a copy, so do not modify it directly, use set())
  - returns object: The current state.
- `set(partialState)` (method, since 0.3.0): Updates part of the state. (saves after a shallow merge and notifies subscribers)
  - `partialState` object: Object with only the keys and values to change.
- `replace(nextState)` (method, since 0.3.0): Replaces the whole state.
  - `nextState` object: The new state. (shallow copied)
- `reset()` (method, since 0.3.0): Resets to the default value.
- `subscribe(subscriber)` (method, since 0.3.0): Subscribes. (returns an unsubscribe function)
  - `subscriber` Function: (state) => void
  - returns Function: Unsubscribe function. (calling it ends the subscription)
- `unsubscribe(subscription)` (method, since 0.4.0): Unsubscribes. (pass what subscribe returned, same as calling the returned unsubscribe function)
  - `subscription` Function: Unsubscribe function returned by subscribe. (does nothing if unknown)
- `notify()` (method, since 0.3.0): Notifies subscribers.
- `readFromStorage()` (method, since 0.3.0): Reads from the storage.
  - returns object: A new object with the stored state laid over the default state. (a copy of the default state if missing, a different version or unparsable)
- `writeToStorage()` (method, since 0.3.0): Writes to the storage.
- `readStoredText()` (method, since 0.4.0): Reads the stored text. (empty text if missing or unreadable)
  - returns string: The stored JSON text. An empty string if missing or unreadable.
- `setStorageBackend(storageBackend)` (method, since 0.4.0): Swaps the storage used only by this store. (null means LocalStorage, an object with getItem, setItem, removeItem like a platform storage)
  - Re-reads the state from the swapped-in storage and notifies subscribers. If nothing is stored, it is the default value.
  - `storageBackend` { getItem: Function, setItem: Function, removeItem: Function } | null: Storage used only by this store. (LocalStorage if null or undefined)

  Example: Write to a separate storage such as a platform storage

  ```js
  import { PersistedStore } from "./build/vanilla.js";
  
  const itemMap = new Map();
  const store = new PersistedStore("example.progress", { stage: 1 }, 1);
  store.setStorageBackend({
  	getItem: (key) => {
  		return itemMap.has(key) ? itemMap.get(key) : null;
  	},
  	setItem: (key, value) => {
  		itemMap.set(key, value);
  	},
  	removeItem: (key) => {
  		itemMap.delete(key);
  	}
  });
  store.set({ stage: 3 });
  console.log(itemMap.get("example.progress"));
  ```

- `getStorageBackend()` (method, since 0.4.0): Returns the storage used only by this store. (null if none)
  - returns object | null: Storage used only by this store. null if none.
- `getStorageKey()` (method, since 0.3.0): Returns the storage key.
  - returns string: Storage key.
