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

@sovgut/state

v2.0.37

Published

A lightweight and flexible state management library for any frontend application. This package provides an easy way to manage state using different storage mechanisms such as `localStorage`, `sessionStorage`, cookies, and an in-memory storage solution. Ad

Downloads

154

Readme

@sovgut/state

A lightweight and flexible state management library for any frontend application. This package provides an easy way to manage state using different storage mechanisms such as localStorage, sessionStorage, cookies, and an in-memory storage solution. Additionally, it supports an observer mode to listen for changes in the state.

Installation

You can install the package using npm:

npm install @sovgut/state

Or using yarn:

yarn add @sovgut/state

Features

  • Simple and intuitive API
  • Supports multiple storage mechanisms:
    • localStorage
    • sessionStorage
    • Cookies
    • In-memory storage
  • TypeScript support
  • Custom fallback values
  • Type casting support for stored values
  • Observer mode to listen for changes

Usage

Importing the State Classes

import { LocalState, SessionState, MemoryState, CookieState } from "@sovgut/state";

Using in React Component

Create a React component that uses the state and listens for changes using the observer mode.

import { type IStrategyEvent, LocalState } from "@sovgut/state";
import { memo, useCallback, useEffect, useState } from "react";

export const App: React.FC = memo(() => {
  const [value, setValue] = useState<number>(
    LocalState.getItem("random-number-key", { fallback: Math.random() })
  );

  const handleUpdateEvent = useCallback((event: IStrategyEvent<number>) => {
    if (event.value) {
        setValue(event.value)
    }
  }, []);

  const handleOnClick = useCallback(() => {
    LocalState.setItem("random-number-key", Math.random())
  }, []);

  useEffect(() => {
    LocalState.on("random-number-key", handleUpdateEvent)

    return function cleanup() {
        LocalState.off("random-number-key", handleUpdateEvent)
    }
  }, [handleUpdateEvent]);

  return <button onClick={handleOnClick}>Current Value: {value}</button>
});

Setting Values

You can store different types of values in the state:

LocalState.setItem("key-1", 1n); // BigInt
LocalState.setItem("key-2", 1); // Number
LocalState.setItem("key-3", "foo"); // String
LocalState.setItem("key-4", true); // Boolean
LocalState.setItem("key-5", {}); // Object
LocalState.setItem("key-6", []); // Array

Getting Values

You can retrieve values from the state with optional type casting:

LocalState.getItem("key-1", { cast: "bigint" }); // 1n
LocalState.getItem("key-2", { cast: "number" }); // 1
LocalState.getItem("key-3", { cast: "string" }); // "foo"
LocalState.getItem("key-4", { cast: "boolean" }); // true
LocalState.getItem("key-5", { cast: "object" }); // {}
LocalState.getItem("key-6", { cast: "object" }); // []

You can also provide fallback values, which not only supply a default value if the key does not exist, but also define the type to cast the retrieved value:

LocalState.getItem("nonexistent-key", { fallback: 1n }); // 1n
LocalState.getItem("nonexistent-key", { fallback: 1 }); // 1
LocalState.getItem("nonexistent-key", { fallback: "foo" }); // "foo"
LocalState.getItem("nonexistent-key", { fallback: true }); // true
LocalState.getItem("nonexistent-key", { fallback: {} }); // {}
LocalState.getItem("nonexistent-key", { fallback: [] }); // []

LocalState.setItem("key-1", 1n);
LocalState.setItem("key-2", 1);
LocalState.setItem("key-3", "foo");
LocalState.setItem("key-4", true);
LocalState.setItem("key-5", {});
LocalState.setItem("key-6", []);

LocalState.getItem("key-1", { fallback: 2n }); // 1n
LocalState.getItem("key-2", { fallback: 2 }); // 1
LocalState.getItem("key-3", { fallback: "bar" }); // "foo"
LocalState.getItem("key-4", { fallback: false }); // true
LocalState.getItem("key-5", { fallback: { foo: "bar" } }); // {}
LocalState.getItem("key-6", { fallback: [{ foo: "bar" }, { foo: "bar" }] }); // []

Removing Values

You can remove a specific key from the state:

LocalState.removeItem("key-1");

Clearing All Values

You can clear all values from the state:

LocalState.clear();

Checking for Existence

You can check if a key exists in the state:

LocalState.has("key-1"); // true or false

Using Other Storage Mechanisms

The same API applies to SessionState, MemoryState, and CookieState:

SessionState.setItem("key", "value");
const sessionValue = SessionState.get("key");

MemoryState.setItem("key", "value");
const memoryValue = MemoryState.get("key");

CookieState.setItem("key", "value");
const cookieValue = CookieState.get("key");

Observer Mode

You can listen for changes to the state using the observer mode:

Adding Event Listeners

function onLocalStateChange(event: IStrategyEvent) {
  console.log(`${event.key} changed in ${event.strategy} strategy`, event.value);
}

LocalState.on("key-1", onLocalStateChange);
LocalState.once("key-2", onLocalStateChange);

Removing Event Listeners

LocalState.off("key-1", onLocalStateChange);

Removing All Listeners

You can remove all event listeners:

LocalState.removeAllListeners();

Event Data

The event object contains the key, value, and strategy of change:

export type IStrategy =
  | "local"
  | "session"
  | "cookie"
  | "memory";

export interface IStrategyEvent<Value = unknown> {
  /**
   * The key of the item in the state that triggered the event.
   */
  key: string;

  /**
   * The value associated with the key in the state.
   * This is optional and can be of any type.
   */
  value?: Value;

  /**
   * The strategy type indicating the source of the state change.
   * This will typically be one of the predefined types (local, session, cookie, memory),
   * or a custom strategy type as a string.
   */
  strategy: IStrategy;
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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