ORS - Observable regenerating store
![Coverage Status](https://coveralls.io/repos/github/arqex/ors/badge.svg?branch=master)
The Observable Regenerating Store is a tree data structure that emits events when it's updated, even if the change happens in one of the leaves, making easier to think in a reactive way.
An ORS can be read or updated like any JS object:
import ors from '@arqex/ors'
let store = ors({
people: {
alice: { age: 22 },
bob: {age: 38}
}
});
store.addChangeListener( () => console.log('Changed') );
store.people.alice.age = 23;
See this example working
ORS is intended to be the single store where centralize all the data of a JavaScript application, it's the simplest way of manage the state: Just use it and listen to changes to update your UI. See how it works.
Installation
npm install --save @arqex/ors
Usage
import ors from '@arqex/ors'
let store = ors({
people: {
alice: { age: 12, children: [] },
bob: { age: 38, children: ['alice'] }
}
});
let alice = store.people.alice;
console.log( alice.age );
store.addChangeListener( () => console.log('Store updated') );
store.people.bob.addChangeListener( () => console.log('Bob updated') );
store.people.bob.children.push('chris');
console.log( store.people.bob.children[1] );
store.people.bob.age = 39;
See this example working
How does ORS work?
ORS has 3 main features. They are probably all the features it has and that makes it simple and powerful.
Feature 1: It looks like a standard JS object
ORS is really simple to use because we don't need to learn any methods or strategy to make it work. It loks like just any other object:
let store = ors({
people: [
{name: 'Jude', age: 20}
];
});
store.people[0].name;
store.people[0].age = 21;
store.people[0].age;
let jude = store.people.pop();
jude;
store.people.length;
The stores look like JS standard objects, but they really aren't. Objects and arrays are used as baseIn the background, it uses JS Proxies to keep track of the changes and spread them up through the store.
but it regenerates when a node gets updated and emit change events.
It has some features that makes it the simplest way of managing the state:
It looks like a standard JS object.
It's completely observable. We can listen for updates at any node, and change events are triggered in the parent nodes when children are updated.
When a child is updated, the whole branch that contains it gets regenerated after the change event, making simpler to use memoization to derive data.
Usage
To be documented...
Compatibility
These are the minimum browser versions to make ORS work:
Chrome | Firefox | Edge | IE | Safari |
49 | 18 | 12 | Won't work | 10 |
Mobile browsers:
Android | Chrome | iOS Safari | Samsung | Opera |
49 | 49 | 10 | 6.2 | 30 |
On Node.js it works since version 6.5.