0.8.0 • Published 7 months ago
@profullstack/state-manager v0.8.0
@profullstack/state-manager
Enhanced state manager with web component integration, persistence, and subscription management.
Browser-Only Version
This module has been refactored to be browser-only, removing any Node.js-specific dependencies. It can be used directly in browser environments without any additional polyfills or bundling.
No External Dependencies
The module now uses the browser's native EventTarget API instead of the eventemitter3 library, eliminating all external dependencies. This makes the module lighter and more efficient for browser usage.
Features
- Simple and intuitive API for state management
- Immutable state updates
- Subscription system for state changes
- Path-based state access and updates
- Persistence with various storage adapters (localStorage, sessionStorage, IndexedDB, memory)
- Web component integration
- Middleware support for intercepting and modifying state updates
- Selectors for derived state
Installation
npm install @profullstack/state-managerUsage
Basic Usage
import { createStateManager } from '@profullstack/state-manager';
// Create a state manager with initial state
const stateManager = createStateManager({
user: {
name: 'John Doe',
preferences: {
theme: 'light'
}
},
todos: [
{ id: 1, text: 'Learn state management', completed: false }
]
});
// Subscribe to state changes
const unsubscribe = stateManager.subscribe((state, changedPaths) => {
console.log('State changed:', state);
console.log('Changed paths:', changedPaths);
});
// Update state
stateManager.setState({
user: {
preferences: {
theme: 'dark'
}
}
});
// Get state
const theme = stateManager.getState('user.preferences.theme');
console.log('Theme:', theme); // 'dark'
// Unsubscribe when done
unsubscribe();Persistence
import { createStateManager } from '@profullstack/state-manager';
// Create a state manager with persistence enabled
const stateManager = createStateManager({
user: {
name: 'John Doe'
}
}, {
enablePersistence: true,
persistenceKey: 'my_app_state'
});
// State will be automatically saved to localStorage
stateManager.setState({
user: {
name: 'Jane Doe'
}
});
// When the app is reloaded, the state will be restored from localStorageWeb Component Integration
import { createStateManager } from '@profullstack/state-manager';
// Create a state manager
const stateManager = createStateManager({
todos: [
{ id: 1, text: 'Learn state management', completed: false }
]
});
// Create a connected web component
const { createConnectedComponent } = stateManager.webComponents;
class TodoListElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
connectedCallback() {
this.render();
}
stateChanged(state, path, fullState) {
this.render();
}
render() {
const todos = this.getState('todos');
this.shadowRoot.innerHTML = `
<ul>
${todos.map(todo => `
<li>${todo.text}</li>
`).join('')}
</ul>
`;
}
}
// Register the component
createConnectedComponent('todo-list', TodoListElement, {
statePaths: ['todos']
});
// Use the component in HTML
// <todo-list></todo-list>API Reference
createStateManager(initialState, options)
Creates a new state manager instance.
initialState: Initial state objectoptions: Configuration optionsenablePersistence: Whether to enable persistence (default: false)persistenceKey: Key for persistence storage (default: 'app_state')persistenceAdapter: Persistence adapter (default: localStorage)persistentKeys: Keys to persist (default: all)immutable: Whether to use immutable state (default: true)debug: Whether to enable debug logging (default: false)
StateManager Methods
getState(path): Get the current state or a specific part of the statesetState(update, options): Update the stateresetState(initialState, options): Reset the state to initial valuessubscribe(callback, paths): Subscribe to state changesunsubscribe(callback): Unsubscribe a callback from all subscriptionsuse(type, middleware): Add middleware to the state managercreateSelector(selectorFn, equalityFn): Create a selector function that memoizes the result
License
MIT
0.8.0
7 months ago
0.7.1
7 months ago
0.7.0
7 months ago
0.6.1
7 months ago
0.6.0
7 months ago
0.5.19
7 months ago
0.5.18
7 months ago
0.5.17
7 months ago
0.5.16
7 months ago
0.5.15
7 months ago
0.5.9
7 months ago
0.5.8
7 months ago
0.5.7
7 months ago
0.5.6
7 months ago
0.5.5
7 months ago
0.4.4
7 months ago
1.0.1
8 months ago
1.0.0
8 months ago