undying
v1.1.1
Published
Auto-save on local-storage objects
Downloads
1
Readme
undying 🧟🧛🧟👻🧛🧟
Tiny & fast "auto-save" data objects to local storage or session storage
Usage
import { undying } from 'undying';
const defaultValues = {
favouriteColor: 'blue',
};
const undyingObject = undying('storage-key', defaultValues);
// If exists on local storage, ignores default values,
// If does not exists, creates it with default values
undyingObject.favouriteColor = 'red';
// async flushed to local storage.
An undying object is a JSON-format object proxy, flushing changes into localStorage. When an undying object's property is assigned with another object (or array), the actual saved value is an undying object. This means mutations in the object's inner tree and even array mutations are caught and saved into the localStorage.
Every change triggers an async flush - meaning that multiple changes wait until the end of the current execution stack and the store occurs only once in a dedicated microtask.
Session storage
import { undying } from 'undying';
const undyingData = undying.session('storage-key', {});
Watching changes
import { undying } from 'undying';
const undyingObject = undying('user-defaults'); // defaults to an empty object, otherwise last stored data
undying.observe(undyingObject, (value) => {
// value is the whole tree data
});