You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

StateManagerJS

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

StateManagerJS

A simple, lightweight JavaScript State Manager library for managing application state in web projects. It supports: - Adding, removing, and clearing items from state. - Persistent storage using localStorage with data encoding. - Automatic notification to subscribed listeners on state changes. Usage: 1. Create an instance: const cart = new StateManager("cart"); 2. Add items using cart.Add({id, name, price, quantity}); 3. Remove items by id using cart.Remove(id); 4. Subscribe to state changes using cart.subscribe(callback); Designed for use in both classic ASP.NET MVC (place files under Scripts\StateManagement) and ASP.NET Core projects (you may copy files to wwwroot\StateManagement).

1.0.0
Source
nugetNuGet
Version published
Maintainers
1
Created
Source

StateManager.js - Simple State Management with LocalStorage

English

Overview

StateManager.js is a lightweight JavaScript class designed to help you manage application state persistently using localStorage. It supports singleton instances by key, provides a simple API to add, remove, clear items, and subscribe to state changes. It uses base64 encoding to store the state securely.

Features

  • Singleton pattern per key to avoid multiple instances with same key.
  • State persistence in localStorage with encoding/decoding.
  • Manage lists with Add, Remove, Clear methods.
  • Subscribe to state changes with callbacks.
  • Supports quantity increment if item with same ID is added.

Installation

You can include StateManager.js directly in your project or distribute it via NuGet as a public package.

To use via NuGet, after publishing the package, add it to your project as usual.

Usage Example

Include the script:

<script src="stateManager.js"></script>

Create a new state manager instance by a unique key:

const cart = new StateManager("cart");

Add items:

cart.Add({ id: 1, name: "Product One", quantity: 1 });

Subscribe to changes:

cart.subscribe(state => {
  console.log("Current state:", state);
});

Remove items:

cart.Remove(1);

Clear all:

cart.Clear();

API Summary

MethodDescription
Add(item)Adds an item or increases quantity if ID exists.
Remove(id)Removes an item by ID.
Clear()Clears all items.
subscribe(cb)Adds a callback for state changes.
unsubscribe(cb)Removes a previously added callback.
getState()Returns current state copy.
setState(newState)Replaces state and triggers updates.

فارسی

معرفی

StateManager.js یک کلاس جاوااسکریپت سبک و ساده است که به شما کمک می‌کند تا وضعیت (State) برنامه‌تان را به صورت پایدار در localStorage ذخیره و مدیریت کنید. این کلاس از الگوی Singleton برای کلیدهای یکتا استفاده می‌کند و API ساده‌ای برای افزودن، حذف، پاک کردن آیتم‌ها و دریافت اطلاعیه تغییرات وضعیت دارد. داده‌ها به صورت base64 رمزگذاری می‌شوند.

امکانات

  • استفاده از الگوی Singleton بر اساس کلید.
  • ذخیره و بازیابی وضعیت با رمزگذاری در localStorage.
  • مدیریت لیست با متدهای Add، Remove، Clear.
  • امکان ثبت callback برای اطلاع از تغییرات وضعیت.
  • افزایش مقدار (quantity) در صورت وجود آیتم با همان شناسه.

نصب

می‌توانید فایل StateManager.js را مستقیماً در پروژه خود وارد کنید یا آن را به صورت یک پکیج NuGet عمومی منتشر کنید.

بعد از انتشار پکیج در NuGet، آن را به پروژه خود اضافه کنید.

مثال استفاده

اسکریپت را وارد کنید:

<script src="stateManager.js"></script>

یک نمونه جدید با کلید منحصر به فرد بسازید:

const cart = new StateManager("cart");

آیتم اضافه کنید:

cart.Add({ id: 1, name: "محصول یک", quantity: 1 });

ثبت شنونده تغییرات:

cart.subscribe(state => {
  console.log("وضعیت فعلی:", state);
});

حذف آیتم:

cart.Remove(1);

پاک کردن همه:

cart.Clear();

خلاصه API

متدتوضیح
Add(item)اضافه کردن آیتم یا افزایش تعداد در صورت وجود آیتم با شناسه مشابه.
Remove(id)حذف آیتم بر اساس شناسه.
Clear()پاک کردن همه آیتم‌ها.
subscribe(cb)اضافه کردن callback برای اطلاع از تغییرات وضعیت.
unsubscribe(cb)حذف callback ثبت شده.
getState()گرفتن کپی از وضعیت فعلی.
setState(newState)جایگزینی وضعیت و اعلام به شنونده‌ها.

License

MIT License

Contact

If you have questions or want to contribute, please open an issue or pull request.

Keywords

javascript

FAQs

Package last updated on 07 Jun 2025

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.