Bolsa is a lightweight state management library written in TypeScript, offering:
✅ Global State Management ✅ Automatic Persistence (IndexedDB for Web, AsyncStorage for React Native) ✅ Middleware Support for logging, validation, and more ✅ Minimal Dependencies & Blazing Fast Performance
npm install bolsaor
yarn add bolsaimport { createStore } from "bolsa";
const counter = createStore(0, "counter-key");
counter.subscribe(value => console.log("Counter Updated:", value));
counter.set(5); // Updates state & persists it
counter.update(n => n + 1); // Increments stateconst userStore = createStore({ name: "Alice", age: 25 }, "user-store");
userStore.subscribe(user => console.log("User State:", user));
userStore.set({ name: "Bob", age: 30 }); // Stores user dataMiddleware functions allow you to modify, log, or validate state changes before they are applied.
import { createStore, Middleware } from "bolsa";
const logger: Middleware<number> = (state, next) => {
console.log("Previous State:", state);
next(state);
console.log("New State:", state);
};
const preventNegative: Middleware<number> = (state, next) => {
if (state < 0) {
console.warn("Negative values not allowed!");
return;
}
next(state);
};
const counter = createStore(0, "counter-key", [logger, preventNegative]);- Web: Uses IndexedDB for persistence.
- React Native: Uses AsyncStorage.
- State updates save the data
- Data is stored in IndexedDB (Web) or AsyncStorage (React Native).
Creates a new store with optional persistence and middleware support.
initialState: T- The initial state of the store.storageKey?: string- Optional key for persistence.middlewares?: Middleware<T>[]- Optional array of middleware functions.
get(): T- Returns the current state.set(newState: T): Promise<void>- Updates the state.update(updater: (state: T) => T): Promise<void>- Updates the state using a function.subscribe(callback: (state: T) => void): () => void- Subscribes to state changes.use(middleware: Middleware<T>): void- Adds a middleware function.
✅ Fast & Lightweight (Minimal dependencies) ✅ Cross-Platform (Works on Web & React Native) ✅ Automatic Persistence (IndexedDB / AsyncStorage) ✅ Middleware Support for enhanced control ✅ Simple API for ease of use
This project is licensed under the MIT License.
We welcome contributions! Feel free to open an issue or submit a pull request.
If you like this project, give it a ⭐ on GitHub!