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

easystatemanagersvelte

v1.0.10

Published

a lightweight svelte library that provides a simple and efficient way to manage and persist state to local storage

Downloads

31

Readme

persistentVariable

description

A simple and efficient way to manage state

The persistentVariable Library is lightweight and provides a simple and efficient way to create and persist data across page refreshes as well as being reactive for use cases such as a shopping cart and user states in svelte

Installation

You can install the persistentVariable library using npm:

npm install easystatemanagersvelte

Usage

Basic Usage

example 1

// Import the persistentVariable library
import persistentVariable from "easystatemanagersvelte";

// store initial value in a variable
const mypersistentVariable = persistentVariable("initial value");

// get the current value
$: console.log($mypersistentVariable); // log whenever value changes

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

Example 2

// Import the persistentVariable library
import persistentVariable from "easystatemanagersvelte";

// create the stateful variable with an initial value of an empty array
const myarray = persistentVariable("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 persistentVariable library
import persistentVariable from "easystatemanagersvelte";

// Create a stateful variable with an initial value
const cart = persistentVariable('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);

// Unsubscribe from further changes
unsubscribe();

accessing value in a plain javascript file i.e a .js file such as index.js

// Import the persistentVariable library
import persistentVariable from "easystatemanagersvelte";

// store initial value in a variable
const mypersistentVariable = persistentVariable("initial value");

// access a property called value on your variable
console.log(mypersistentVariable.value);

// this should log "initial value" in the console

Basic Shopping Cart Example

// Import the persistentVariable library
import persistentVariable from "easystatemanagersvelte";

// Create a stateful variable for the shopping cart
const cart = persistentVariable('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);

// Unsubscribe from further changes
unsubscribe();

inspiration

This library was inspired by svelte but with persistence added

license

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