The easy way to do state
A namespaceable localStorage pub/sub utility for the masses.
Event-driven, persistent state management built on top of localStorage and sessionStorage.
const pointsStorage = new CheapState('points');
pointsStorage.set('paceaux', 10);Optionally, instantiate with sessionStorage:
const pointsStorage = new CheapState('points', 'session');pointsStorage.set('frank', 10);
pointsStorage.get('frank');// 10pointsStorage.setObject({
'frank': 10,
'joe': 20,
'sally': 30
});
pointsStorage.get('frank'); // 10const points = new Map([
['frank', 10],
['joe', 20],
['sally', 30]
]);
pointsStorage.setObject(points);
pointsStorage.get('frank'); // 10pointsStorage.delete('sally');pointsStorage.clear();const badgeEls = document.querySelectorAll('.badge');
badgeEls.forEach((badgeEl) => {
updateBadge(badgeEl)
// add a subscriber
pointsStorage.subscribe((payload) => {
// does the key match this element's key?
if (payload.key === badgeEl.dataset.key) {
// update it!
updateBadge(badgeEl);
}
});
});Global Class
new CheapState(namespace, type)
| name | type | Description |
|---|---|---|
| namespace | string | the namespaces that goes with the CheapState class |
| type | string | local or session |
| name | type | Description |
|---|---|---|
| namespace | string | the namespaces that goes with the CheapState class |
| keyname | string | the keyname to be namespaced |
string with with <namespace>.<keyname>.
Makes a value safe to be stored in localStorage.
| name | type | Description |
|---|---|---|
| value | any | the value to be converted to a string |
string version of the value.
Converts a string into a JavaScript value;
| name | type | Description |
|---|---|---|
| value | any | the value to be converted from a string |
any version of the value.
Adds a namespace to storage
| name | type | Description |
|---|---|---|
| namespace | string | Adds a namespace to storage |
| storage | Storage | either the localStorage or sessionStorage object |
string the namespace for the CheapState instance.
Function[] a list of the observers for the CheapState instance.
string[] a list of the namespaces in storage.
Storage either localStorage or sessionStorage.
Map<'string', any> all of the items in storage for the given namespace.
number the number of items in storage for the given namespace.
number the number of items in storage for the given namespace. (alias for size)
Determines if a namespace already exists
| name | type | Description |
|---|---|---|
| namespace | string | the namespace to check |
boolean if the namespace exists.
Sets an item into storage
| name | type | Description |
|---|---|---|
| key | string | the key to set |
| value | any | the value to set |
Sets an object's keys and values into storage.
| name | type | Description |
|---|---|---|
| dataObject | Object | Map |
Gets an item from storage.
| name | type | Description |
|---|---|---|
| key | string | an unnamespaced key name |
any the value of the key.
Determines if an item exists in storage.
| name | type | Description |
|---|---|---|
| key | string | an unnamespaced key name |
boolean whether the key exists.
Deletes an item from storage.
| name | type | Description |
|---|---|---|
| key | string | an unnamespaced key |
| name |
Deletes all items in the namespaced storage.
| name | type | Description |
|---|---|---|
| key | string | an unnamespaced key name |
Adds a function to observables; allows it to receive a payload when storage changes
| name | type | Description |
|---|---|---|
| observable | Function | a function that should fire when a change happens to storage |
Removes a function from observables
| name | type | Description |
|---|---|---|
| observable | Function | a function to remove |
Sends a payload to the observer
| name | type | Description |
|---|---|---|
| data | any | a message to send when a change happens. It's sent to all observers. |