mutable-store
Advanced tools
+79
| # Mutable Store | ||
| A lightweight, type-safe state management library for JavaScript/TypeScript applications that embraces mutability with a subscription pattern. | ||
| ## Features | ||
| - 📦 Tiny footprint with zero dependencies | ||
| - 🔄 Subscribe to state changes | ||
| - 🧩 Works with plain JavaScript objects or class instances | ||
| - 📐 Fully typed with TypeScript | ||
| - 🛠️ Simple, intuitive API | ||
| ## Installation | ||
| ```bash | ||
| npm install mutable-store | ||
| ``` | ||
| ## Usage | ||
| ```ts | ||
| import { createMutableStore } from "mutable-store"; | ||
| const store = createMutableStore({ | ||
| count: 0, | ||
| increment() { | ||
| this.count++; | ||
| }, | ||
| }); | ||
| store.subscribe((sub) => { | ||
| console.log(sub); | ||
| }); | ||
| store.increment(); | ||
| ``` | ||
| ## API | ||
| ### createMutableStore(state: any) | ||
| Creates a mutable store from a plain JavaScript object or class instance. | ||
| #### Parameters | ||
| - `state`: The state object or class instance to make mutable. | ||
| #### Returns | ||
| The mutable state object with a `subscribe` method for subscribing to changes. | ||
| ### subscribe(fn: (sub: subType) => void) | ||
| Subscribes to state changes. | ||
| // example on how to subscribe and unsubscribe | ||
| ```typescript | ||
| const store = createMutableStore({ | ||
| count: 0, | ||
| increment() { | ||
| this.count++; | ||
| }, | ||
| }); | ||
| const unsubscribe = store.subscribe((sub) => { | ||
| console.log(sub); | ||
| }); | ||
| store.increment(); // { setterName: 'increment', args: [] } | ||
| unsubscribe(); // unsubscribe from the state changes | ||
| ``` | ||
| #### Parameters | ||
| - `fn`: The function to call when the state changes. | ||
| #### Returns | ||
| A function to unsubscribe from the state changes. |
+1
-1
| { | ||
| "name": "mutable-store", | ||
| "version": "1.0.0", | ||
| "version": "1.0.1", | ||
| "description": "a mutable state management library for javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
18193
9.12%6
20%1
-50%80
Infinity%