Giter Club home page Giter Club logo

state's Introduction

@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.

state's People

Contributors

sovgut avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.