1.2.4 • Published 8 years ago
@maexsoftware/datastore v1.2.4
Datastore
Installing
$ npm install --save @maexsoftware/datastore
Store
Usage
// Import
const { Store } = require('@maexsoftware/datastore');
// Initialize store
const store = new Store('testStore', 0);
// Add modifier functions to store
store.modifier('increment', (state, value) => state + value);
store.modifier('decrement', (state, value) => state - value);
// Call modifier functions to create modifications to the store state
store.modify.increment(10);
store.modify.decrement(5);
// Calculate new state value
store.state(); // => 5
// The base state does not change
store.baseState; // => 0
// Unless a commit has been made
store.commit();
store.baseState; // => 5
// Stores also have events
store.subscribe('update', function updateListener (modification) {
console.log('update ran with:', modification);
});
// This will publish an 'update' event
// console will log:
// 'update ran with: { type: increment, value: 100 }'
store.modify.increment(100);
// Events also publish 'updated' events when commits are performed
// console will log:
// 'updated with new state: 105'
const subscription = store.subscribe('updated', function updatedListener (state) {
console.log('updated with new state:', state);
});
store.commit();
// And of course you can unsubscribe the listener at any point
subscription.unsubscribe();