
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
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).
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.
localStorage
with encoding/decoding.Add
, Remove
, Clear
methods.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.
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();
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 رمزگذاری میشوند.
localStorage
.Add
، Remove
، Clear
.میتوانید فایل 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();
متد | توضیح |
---|---|
Add(item) | اضافه کردن آیتم یا افزایش تعداد در صورت وجود آیتم با شناسه مشابه. |
Remove(id) | حذف آیتم بر اساس شناسه. |
Clear() | پاک کردن همه آیتمها. |
subscribe(cb) | اضافه کردن callback برای اطلاع از تغییرات وضعیت. |
unsubscribe(cb) | حذف callback ثبت شده. |
getState() | گرفتن کپی از وضعیت فعلی. |
setState(newState) | جایگزینی وضعیت و اعلام به شنوندهها. |
MIT License
If you have questions or want to contribute, please open an issue or pull request.
FAQs
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).
We found that statemanagerjs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.