It's a(nother) state manager! But let's not get overwhelmed. Treeful simply provides the following to your app.
- Single global object contains all states.
- Subset of the states can be subscribed with callback functions.
- States can be updated.
That's it. No steep learning curve, no configuration. And regardless of its simplicity, Treeful has distinct characteristics that makes it powerful.
- Less code - Minimal lines of code are sufficient. No extra files needed.
- Tree structure - Your state can be nested, and subscribing to parent will automatically subscribe to its children.
- Efficient data transfer - You won't pass around whole tree. Only the subscribed set will be passed for efficiency.
- Framework independent - No wrappers needed. Keep your code as is.
npm install treeful
First, import the package and create your tree (you don't need to instantiate)
import Treeful from 'treeful';
Treeful.add('count', 0) // Add node 'count' with value 0 (to 'root')
.add('todos', [], 'root') // Add node 'todos' to 'root'
.add('filter', 'all', 'todos'); // Add node 'filter' to 'todos' with value of 'all'
Our tree now looks like this:
Subscribe to node 'todos'
by calling:
Treeful.subscribe('todos', callbackTodos);
// callbackTodos will get called when the data in 'todos' or 'filter' gets updated
Get and set data by calling:
let oldData = Treeful.get('filter'); // oldData = 'all'
Treeful.set('filter', 'completed');
// Node 'filter' is updated, and it is a child of 'todos' that is subscribed to callbackTodos
callbackTodos is now called, and passed the new data
function callbackTodos(data, node) {
// data = 'completed' (updated data)
// node = 'filter' (node that changed)
// do some stuff
}
APIs and examples - please refer to documentation.
Join the party - please refer to contributing.
MIT - please refer to license.