This is an external open-source GitHub repository imported into the WOCSOL Marketplace for discovery. The original repository owner is the primary creator.
Lightweight central store of state with the ability to watch for and react to specific property changes
Lightweight central store of state with the ability to watch for and react to specific property changes
# Toystore.js Lightweight central store of state with the ability to watch for and react to specific property changes **Think "toy" as in small.** This thing is ready to rock at just a hair over 2kb minified and gzipped. ## Installation ``` npm install toystore --save ``` ## Usage Create a new store instance with the initial store values: ```javascript const toystore = require('toystore'); let store = toystore.create({ foo: 'bar', user: { email: 'user@example.com', id: 1, } }); module.exports = store; ``` Get store values anywhere in your app, even nested keys: ```javascript const store = require('./mystore'); function renderUserEmail() { return store.get('user.email'); } ``` Watch for changes on specific keys and react to them: ```javascript const store = require('./mystore'); store.watch(['user.email'], updateUserEmail); ``` Control the order watchers fire using priority weightings. The default priority is 0. Negative numbers can be used to push watchers to the end. ```javascript const store = require('./mystore'); store.watch(['user'], secondTask); store.watch(['user'], firstTask, { priority: 10 }); store.watch(['user'], thirdTask, { priority: -1 }); ``` Watchers can also be executed asynchronously after a short timeout instead of invoked immediately: ```javascript const store = require('./mystore'); // Executed async with setTimeout instead of immediately called store.watch(['user'], updateUserInfo, { async: true }); // Executed async after provided milliseconds store.watch(['user'], updateUserInfoInOtherPlace, { async: 500 }); ``` Update store values from anywhere in your app: ```javascript const store = require('./mystore'); function fetchUser() { return fetch('/myapi/users/1') .then(json => { store.set('user', { email: json.data.emai
Ask questions or discuss this product. New comments are reviewed before publishing.
Loading comments...