Summary
object-observer
provides a deep observation of a changes performed on an object/array graph.
Main aspects and features:
-
implemented via native Proxy (revokable)
-
observation is 'deep', yielding changes from a sub-graphs too
-
nested objects of the observable graph are observables too
-
changes delivered in a synchronous way by default, asynchronous delivery (experimental) is optionally available as per Observable
configuration; more details here
-
observed path may optionally be filtered as per observer
configuration; more details here
-
original objects are cloned while turned into Observable
s
-
array specifics:
- generic object-like mutations supported
- intrinsic
Array
mutation methods supported: pop
, push
, shift
, unshift
, reverse
, sort
, fill
, splice
, copyWithin
- massive mutations delivered in a single callback, usually having an array of an atomic changes
-
typed array specifics:
- generic object-like mutations supported
- intrinsic
TypedArray
mutation methods supported: reverse
, sort
, fill
, set
, copyWithin
- massive mutations delivered in a single callback, usually having an array of an atomic changes
-
intrinsic mutation methods of Map
, WeakMap
, Set
, WeakSet
(set
, delete
) etc are not observed (see this issue for more details)
-
following host objects (and their extensions) are skipped from cloning / turning into observables: Date
, Blob
, Error
Support matrix: 61+ | 60+ | 16+ | 8.10.0+
Performance report can be found here
Last versions (full changelog is here)
-
4.2.1
- documentation improved and updated
- dependencies updated
- Issue no. 73 - added DOM-like API of
ObjectObserver
-
4.1.3
-
4.1.1
- Issue no. 70 - automated version bump
- unified
Change
object structure (so that it is always the same shape) - improved perf tests
For a preview/playground you are welcome to:
Install
Use regular npm install object-observer --save-prod
to use the library from your local environment.
Additionally, a CDN deployment available (AWS driven), so one can import it directly:
import { Observable } from 'https://libs.gullerya.com/object-observer/x.y.z/object-observer.min.js';
Note: replace the x.y.z
by the desired version, one of the listed in the changelog.
CDN features:
- HTTPS only, no untrusted man-in-the-middle
- highly available (with many geo spread edges)
- agressive caching setup
Import object-observer
as ES6 module:
import { Observable } from 'object-observer.min.js';
API
Library implements Observable
API as it is defined here.
Additionally, from version 4.2.0 there is also 'DOM-like' API flavor, a one that resonating with DOM's MutationObserver
, ResizeObserver
etc from the syntax perspective.
Under the hood it uses the same Observable
mechanics.
Read docs about this API flavor here.
This is experimental API until specified here otherwise.
Security
Security policy is described here. If/when any concern raised, please follow the process.
Examples
Objects
const
order = { type: 'book', pid: 102, ammount: 5, remark: 'remove me' },
observableOrder = Observable.from(order);
observableOrder.observe(changes => {
changes.forEach(change => {
console.log(change);
});
});
observableOrder.ammount = 7;
observableOrder.address = {
street: 'Str 75',
apt: 29
};
observableOrder.address.apt = 30;
delete observableOrder.remark;
Object.assign(observableOrder, { amount: 1, remark: 'less is more' }, { async: true });
Arrays
let a = [ 1, 2, 3, 4, 5 ],
observableA = Observable.from(a);
observableA.observe(changes => {
changes.forEach(change => {
console.log(change);
});
});
observableA.pop();
observableA.push('a', 'b');
observableA.shift();
observableA.unshift('x', 'y');
observableA.reverse();
observableA.sort();
observableA.fill(0, 0, 1);
observableA.splice(0, 1, 'x', 'y');
let customer = { orders: [ ... ] },
oCustomer = Observable.from(customer);
oCustomer.orders.sort();
oCustomer.orders.reverse();
Arrays notes: Some of array operations are effectively moving/reindexing the whole array (shift, unshift, splice, reverse, sort).
In cases of massive changes touching presumably the whole array I took a pessimistic approach with a special non-detailed events: 'reverse' for reverse
, 'shuffle' for sort
. The rest of these methods I'm handling in an optimistic way delivering the changes that are directly related to the method invocation, while leaving out the implicit outcomes like reindexing of the rest of the Array.
Observation options
object-observer
allows to filter the events delivered to each callback/listener by an optional configuration object passed to the observe
API.
In the examples below assume that callback = changes => {...}
.
let user = {
firstName: 'Aya',
lastName: 'Guller',
address: {
city: 'of mountaineers',
street: 'of the top ridges',
block: 123,
extra: {
data: {}
}
}
},
oUser = Observable.from(user);
oUser.observe(callback, {path: 'firstName'});
oUser.observe(callback, {path: 'address.city'});
oUser.observe(callback, {pathsOf: 'address'});
oUser.observe(callback, {pathsFrom: 'address'});