npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easystatemanager

v1.0.14

Published

a lightweight JavaScript library that provides a simple and efficient way to manage state

Downloads

19

Readme

statefulVariable

description

A simple and efficient way to manage state

The statefulVariable Library is lightweight and provides a simple and efficient way to create and persist data across page refreshes

it's reactive in svelte when using the $ symbol and currently working on making it reactive to show updates in real time in plain javascript as well

this can be used for use cases such as a shopping cart and user states similar to what state managers do in other libraries and frameworks.

Installation

You can install the statefulVariable library using npm:

npm install easystatemanager

Usage

Basic Usage

example 1

// Import the statefulVariable library
import statefulVariable from "easystatemanager";

// store initial value in a variable
const myStatefulVariable = statefulVariable("initial value");

// get the current value
console.log(myStatefulVariable.value); // "initial value"

// change value
myStatefulVariable.set("new value");

// get the new value
console.log(myStatefulVariable.value); // "new value"

// if using svelte, this will log all changes in real time without writing multiple console.log statements
$: console.log($myStatefulVariable);

Example 2

// Import the statefulVariable library
import statefulVariable from "easystatemanager";

// create the stateful variable with an initial value of an empty array
const myarray = statefulVariable("myarray", []);

// subscribe to the variable and log the value whenever it changes
myarray.subscribe((value) => {
  console.log(value);
});

// add an item to the cart
myarray.update((value) => [...value, "item 1"]);

// add another item to the myarray
myarray.update((value) => [...value, "item 2"]);

example 3

// Import the statefulVariable library
import statefulVariable from "easystatemanager";

// Create a stateful variable with an initial value
const cart = statefulVariable('cart', []);

// Subscribe to changes in the cart value
const unsubscribe = cart.subscribe((value) => {
  console.log('Cart updated:', value);
});

// Update the cart value
cart.set(['item1', 'item2', 'item3']);

// Access the current value of the cart
console.log('Current cart value:', cart.value);

// if using svelte, this will log whenever the cart updates
$: console.log($cart);

// Unsubscribe from further changes
unsubscribe();

Basic Shopping Cart Example

// Import the statefulVariable library
import statefulVariable from "easystatemanager";

// Create a stateful variable for the shopping cart
const cart = statefulVariable('cart', []);

// Function to add an item to the cart
function addItem(item) {
  cart.update((currentCart) => {
    // Create a new cart array with the added item
    return [...currentCart, item];
  });
}

// Function to remove an item from the cart
function removeItem(item) {
  cart.update((currentCart) => {
    // Filter out the item from the current cart
    return currentCart.filter((cartItem) => cartItem !== item);
  });
}

// Subscribe to changes in the cart value
const unsubscribe = cart.subscribe((value) => {
  console.log('Cart updated:', value);
});

// Add items to the cart
addItem('item1');
addItem('item2');
addItem('item3');

// Remove an item from the cart
removeItem('item2');

// Access the current value of the cart
console.log('Current cart value:', cart.value);

// if using svelte, this will log whenever the cart updates
$: console.log($cart);

// Unsubscribe from further changes
unsubscribe();

inspiration

This library was inspired by svelte but with persistence added and freedom to use across other different frameworks as well as when not using any javascript framework or library

license

This project is licensed under the MIT License. See the LICENSE file for details.