StateManagerJS 1.0.0

dotnet add package StateManagerJS --version 1.0.0
                    
NuGet\Install-Package StateManagerJS -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StateManagerJS" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StateManagerJS" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="StateManagerJS" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StateManagerJS --version 1.0.0
                    
#r "nuget: StateManagerJS, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StateManagerJS@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StateManagerJS&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=StateManagerJS&version=1.0.0
                    
Install as a Cake Tool

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

Method Description
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.


There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 73 6/7/2025