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
- Cross-platform: Works in Browser, Node.js, Deno, and Edge Workers
- Session Isolation: Each session/request gets isolated state namespace
- Context System: Multi-tenant server-side isolation with
memorio.createContext()
⚠️ 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
Cross-Platform Support
Memorio is designed to work across multiple JavaScript environments:
Platform Compatibility
state | ✅ | ✅ | ✅ | ✅ |
observer | ✅ | ✅ | ✅ | ✅ |
useObserver | ✅ | ⚠️ | ⚠️ | ✅ |
cache | ✅ | ✅ | ✅ | ✅ |
store | ✅ (localStorage) | ⚠️ (memory) | ⚠️ (memory) | ✅ (localStorage) |
session | ✅ (sessionStorage) | ⚠️ (memory) | ⚠️ (memory) | ✅ (sessionStorage) |
idb | ✅ | ❌ | ❌ | ⚠️ |
- ✅ Full support
- ⚠️ Partial support (fallback to in-memory)
- ❌ Not available
Session Isolation
Memorio provides automatic session isolation to prevent state leakage between different requests or contexts:
- Unique Session IDs: Each import gets a unique session identifier
- Namespaced Storage:
store and session keys are prefixed with session ID
- State Isolation:
state is isolated per-instance
This ensures that in server-side environments (Node.js/Deno), different requests don't share state data.
Best Practices
- For Client-Side: Use all features freely - store, session, idb work with real browser storage
- For Server-Side: Use
state and cache for in-memory data; store/session fall back to memory
- For Edge Workers: Same as browser; localStorage/sessionStorage available
Checking Platform
import { isBrowser, isNode, isDeno, getCapabilities } from 'memorio';
console.log('Browser:', isBrowser());
console.log('Node.js:', isNode());
console.log('Deno:', isDeno());
const caps = getCapabilities();
console.log('Platform:', caps.platform);
console.log('Persistent:', store.isPersistent);