Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cheapstate

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cheapstate

A localStorage pubSub pattern for the masses

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

CheapState

A namespaceable localStorage pub/sub utility for the masses.

Event-driven, persistent state management built on top of localStorage and sessionStorage.

Examples

Instantiantiating:

const pointsStorage = new CheapState('points');
pointsStorage.set('paceaux', 10);

Optionally, instantiate with sessionStorage:

const pointsStorage = new CheapState('points', 'session');

Saving & Getting items


pointsStorage.set('frank', 10);
pointsStorage.get('frank');// 10

Saving an object

pointsStorage.setObject({
    'frank': 10,
    'joe': 20,
    'sally': 30
});
pointsStorage.get('frank'); // 10
Saving a map
const points = new Map([
    ['frank', 10],
    ['joe', 20],
    ['sally', 30]
]);

pointsStorage.setObject(points);
pointsStorage.get('frank'); // 10

Deleting and Clearing

Delete an item
pointsStorage.delete('sally');
Clearing the storage
pointsStorage.clear();

Adding & subscribing

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);
        }
    });
});

API

CheapState

Global Class

new CheapState(namespace, type)

Parameters
nametypeDescription
namespacestringthe namespaces that goes with the CheapState class
typestringlocal or session

Static Methods

getNameSpacedKeyName(namespace, keyname)
Parameters
nametypeDescription
namespacestringthe namespaces that goes with the CheapState class
keynamestringthe keyname to be namespaced
Returns

string with with <namespace>.<keyname>.

convertValue(value)

Makes a value safe to be stored in localStorage.

Parameters
nametypeDescription
valueanythe value to be converted to a string
Returns

string version of the value.

unconvertValue(value)

Converts a string into a JavaScript value;

Parameters
nametypeDescription
valueanythe value to be converted from a string
Returns

any version of the value.

registerNamespace(namespace, storage)

Adds a namespace to storage

nametypeDescription
namespacestringAdds a namespace to storage
storageStorageeither the localStorage or sessionStorage object

Instance Members

namespace

string the namespace for the CheapState instance.

observers

Function[] a list of the observers for the CheapState instance.

namespaces

string[] a list of the namespaces in storage.

storage

Storage either localStorage or sessionStorage.

items

Map<'string', any> all of the items in storage for the given namespace.

size

number the number of items in storage for the given namespace.

length

number the number of items in storage for the given namespace. (alias for size)

Instance Methods

hasNamespace(namespace)

Determines if a namespace already exists

Parameters
nametypeDescription
namespacestringthe namespace to check
Returns

boolean if the namespace exists.

set(key, value)

Sets an item into storage

Parameters
nametypeDescription
keystringthe key to set
valueanythe value to set
setObject(dataObject)

Sets an object's keys and values into storage.

Parameters
nametypeDescription
dataObjectObjectMap
get(key)

Gets an item from storage.

Parameters
nametypeDescription
keystringan unnamespaced key name
Returns

any the value of the key.

has(key)

Determines if an item exists in storage.

Parameters
nametypeDescription
keystringan unnamespaced key name
Returns

boolean whether the key exists.

delete(key)

Deletes an item from storage.

Parameters
nametypeDescription
keystringan unnamespaced key
name
clear()

Deletes all items in the namespaced storage.

Parameters
nametypeDescription
keystringan unnamespaced key name
subscribe(observable)

Adds a function to observables; allows it to receive a payload when storage changes

Parameters
nametypeDescription
observableFunctiona function that should fire when a change happens to storage
unsubscribe(observable)

Removes a function from observables

nametypeDescription
observableFunctiona function to remove
notify(data)

Sends a payload to the observer

nametypeDescription
dataanya message to send when a change happens. It's sent to all observers.

Keywords

FAQs

Package last updated on 22 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc