@ushiboy/freezable-store
v0.1.0
Published
Freezable Store Library
Downloads
4
Readme
Freezable Store
The freezable-store notify when own state assigned.
Example
Simple Example
import createFreezableStore from '@ushiboy/freezable-store';
const store = createFreezableStore({
count: 0
});
const state = store.state;
const observer1 = () => {
console.log(`change: count:${state.count}`);
};
const observer2 = () => {
console.log(`(observer 2) change: count:${state.count}`);
};
store.observe(observer1);
store.observe(observer2);
console.log(`count: ${state.count}`); // count: 0;
store.assign({
count: state.count + 1
});
console.log(`count: ${state.count}`); // count: 1;
try {
state.count = state.count + 1; // throw Error
} catch (e) {
console.log(`${e}`); // Error: Should use assign
}
console.log(`count: ${state.count}`); // count: 1;
store.unobserve(observer2);
store.assign({
count: state.count + 1
});
console.log(`count: ${state.count}`); // count: 2;
Example Application
- counter: Simple counter application.
- counter-ext: React counter application (with server side rendering).
API
createFreezableStore<T>(initialState: T): Store<T>
Creates an instance of freezable store.
const store = createFreezableStore({ users: [], organization: 'mycompany' });
Instance properties
store.state: T
Provides its own state.
console.log(store.state.organization); // -> 'mycompany'
store.state.organization = 'other'; // -> throw Error
Instance methods
store.assign(...sources: any): void
Updates its own state. Notify.
store.assign({ users: ['user1', 'user2', 'user3'] }, { organization: 'newcompany' });
store.observe(o: (state: T) => void): void
Register an observer.
const observer = state => {
console.log(store.state.users);
};
store.observe(observer);
store.unobserve(o: (state: T) => void): void
Unregister an observer.
const observer = state => {
console.log(store.state.users);
};
store.unobserve(observer);
License
MIT