data-store-manager
v1.0.0
Published
A reactive data store manager with subscription support
Downloads
0
Maintainers
Readme
Data Store Manager
A lightweight reactive data store manager with subscription support, perfect for state management in JavaScript/TypeScript applications.
Features
- 🚀 Reactive data updates
- 🔍 Type-safe API
- 🔔 Field-specific subscriptions
- 🧹 Automatic listener cleanup
- 📦 Zero dependencies
Installation
npm install data-store-manager
# or
yarn add data-store-managerUsage
Basic Example
import { createDataStore } from "data-store-manager";
type User = {
name: string;
age: number;
email: string;
};
const userStore = createDataStore<User>({
name: "Alice",
age: 30,
email: "[email protected]",
});
// Subscribe to all changes
const unsubscribe = userStore.subscribe((changedFields) => {
console.log("Changed fields:", changedFields);
});
// Update single field
userStore.set("name", "Alice Smith");
// Batch update
userStore.update({
age: 31,
email: "[email protected]",
});
// Get current value
console.log(userStore.get("name")); // "Alice Smith"
// Cleanup
unsubscribe();Advanced Subscription
// Subscribe to specific fields
const unsubscribe = userStore.subscribe(
(changedFields) => {
console.log("Name or age changed:", changedFields);
},
["name", "age"]
);API Documentation
createDataStore<T>(initialData: T)
Creates a new data store instance. Returns an object with these methods:
get<F extends keyof T>(field: F): T[F]
Retrieves current value of a field
set<F extends keyof T>(field: F, value: T[F]): void
Updates a single field
update(values: Partial<T>): void
Batch updates multiple fields
subscribe(handler: Listener<T>, fields?: Array<keyof T>): () => void
Subscribe to changes:
- Without fields: Listen to all changes
- With fields: Only listen to specified fields
removeListener(handler: Listener<T>): void
Remove specific listener
clearListeners(): void
Remove all listeners
License
MIT © 周煜城
