Watchers and computed properties (inspired by Vue.js)
import {observe, set, unset, watch, ignore} from 'reaktiv';
const data = {
person: {
firstName: 'Bar',
lastName: 'Foo',
fullName() {
return `${data.person.firstName} ${data.person.lastName}`;
}
}
};
observe(data);
console.log(data.person.fullName) // 'Bar Foo'
const watcher = watch(data.person, 'fullName', value => {
console.log(value); // 'Baz Foo'
});
data.person.firstName = 'Baz';
set(data.person, 'age', 34); // Add a new property to the object
console.log(data.person.age); // 34
unset(data.person, 'age'); // Delete a property
console.log(data.person.age); // undefined
ignore(watcher); // Unsubscribe the watcher from all its dependencies
Makes reactive all properties of an object.
Adds a new property to an object and notifies watchers if the property doesn't already exist.
Warning: The object cannot be a root object.
Deletes a property from an object and notifies watchers.
Warning: The object cannot be a root object.
Returns a watcher that watches a property of an object.
Type: Object
Object to watch the property.
Type: String
Path of the watched property in the object.
Type: Function
Called when the value of the watched property has changed.
Type: Object
Type: Boolean
Default: false
Deeply watch the property of the object.
Unsubscribe a watcher from all its dependencies.