A lightweight, type-safe state management library for JavaScript applications





Table of Contents
Features
- Reactive state management with observer pattern
- Persistent storage with Store API
- Session management for temporary data
- Type-safe with full TypeScript support
- Comprehensive test coverage
- Easy debugging with proxy-based state
⚠️ Enterprise Use
For enterprise-level applications requiring advanced state management, we recommend using @biglogic/rgs instead of Memorio.
RGS provides:
- Enhanced scalability for large applications
- Advanced middleware support
- Enterprise-grade security features
- Professional support and maintenance
Memorio is ideal for small to medium projects, prototypes, and learning purposes.
Installation
npm i -D memorio
All test suites are passing
- Basic functionality tests
- State management tests
- Store operations tests
- Cache operations tests
- Observer pattern tests
- useObserver pattern tests
Total: 28 tests passed across 5 test suites
Quick Start
import 'memorio';
state.counter = 0;
state.active = false;
state.name = "john";
state.user = { name: 'John', age: 30 };
state.hours = [2,3,10,23]
observer(
'state.counter',
(newValue, oldValue) => {
console.log(`Counter changed from ${oldValue} to ${newValue}`);
}
);
useObserver(
(newValue, oldValue) => {
console.log(`Counter changed from ${oldValue} to ${newValue}`);
},
[state.counter]
);
store.set('preferences', { theme: 'dark' });
const preferences = store.get('preferences');
session.set('token', 'user-jwt-token');
const token = session.get('token');
API Reference
State Management
State in Memorio is globally accessible and reactive:
state.user = { name: 'John' };
const userName = state.user.name;
console.log(state.list);
state.remove('user');
state.removeAll();
Observer
⚠️ Deprecated: For React applications, use useObserver instead. The observer function is kept for non-React contexts.
useObserver
useObserver is a React hook for observing Memorio state changes with auto-discovery:
useObserver(
(newValue, oldValue) => {
console.log('User updated:', newValue);
},
[state.user]
);
useObserver(
(newValue, oldValue) => {
console.log('State changed:', newValue);
},
[state.user, state.counter, state.settings]
);
Key differences from observer:
- Uses array syntax
[state.property] instead of string path 'state.property'
- Automatically cleans up on component unmount
- Works only inside React components
- Callback receives
(newValue, oldValue) parameters
Store
Persistent storage for your application:
store.set('config', { theme: 'dark', language: 'en' });
const config = store.get('config');
store.remove('config');
const size = store.size();
store.removeAll();
Session
Temporary storage that persists during page sessions:
session.set(
'userSession', {
id: 'user123',
lastActive: Date.now()
}
);
const userData = session.get('userSession');
const activeItems = session.size();
session.remove('userSession');
session.removeAll();
Cache
In-memory cache for temporary data storage:
cache.set('tempData', { computed: true, value: 42 });
const cached = cache.get('tempData');
const cacheSize = cache.size();
cache.remove('tempData');
cache.removeAll();
Note: Cache data is stored in memory and will be lost on page refresh.
idb
Permanent storage using browser database:
Create database
idb.db.create("Database")
Set data into table
idb.data.set("Database","table", { id: 1, data:{...} } )
Get data from table
[in development]
idb.data.get("Database","table", 1 )
Delete database / table
[in development]
idb.db.delete("Database")
idb.table.delete("Database","table")
Testing
Test suites are passing
- Basic functionality tests
- State management tests
- Store operations tests
- Cache operations tests
- Observer pattern tests
Total: 25 tests passed across 5 test suites
Security
Security scans and reports are available at:
License
MIT License
Copyrigth (c) Dario Passariello