Proxy Observe
Proxserve is a lightweight and very fast javascript library for observing any changes made to an object.
It was initially created for the OH! library but has grown so much that it is now a full-blown library by itself.
With Proxserve you are using plain old javascript objects with the addition of event listeners directly on properties, thus monitoring
any changes made to these properties and sub-objects.
Installation
Proxserve runs on both client side and server side (Node.js)
Server side
Run the following command in the terminal
Then require it:
Client side
Simplest way is to use a CDN
Don't forget to replace "1.x.x" with the desired version
(recent version is 1.x.x)
If installed on your server, you may serve Proxserve on your own.
The files for distribution are located at "node_modules/proxserve/dist/".
You may choose between an unminified (proxserve.js) and minified (proxserve.min.js) versions
Main Concepts
Events
The heart of Proxserve is emitting events for every change made. Proxserve treats the object as a tree and emits the event up the tree.
Each event is an object emitted to whoever listens to it, holding a property of a respective path.
Let's say you have an object like this:
Altering any of the three cells of the array will emit an "update" event to "arr", to "subObj" and to "main".
Now let's say you have attached a listener to all objects and then changed the value of "main.subObj.arr[1]" to 99.
"arr" listener will be called with a "change object" that looks like this:
"subObj" listener will be called with a similar change-object but with a path like this: ".arr[1]"
and "main" listener will be called with a change-object with a path like this: ".subObj.arr[1]"
Event Cycle
Proxserve by default has its own "event loop".
It waits and accumulates changes before it fires the events in order to let you decide what data change do you want to handle and what not.
This makes both, Proxserve and your own code, very efficient and fast.
The time of a cycle is controlled via the "delay" option. This cycle can be disabled.
An example for a case like this is Array.splice(). This method causes javascript to generate many many changes
(javascript moves cells in the array one by one).
Instead of having your listener invoked many times, it will be invoked once with an array of all changes.
You will probably want to ignore all of the changes and refer just to the current and final state of the array.
Garbage Collection
Proxserve handles memory very efficiently but also very aggressively.
By default any sub-object that has been detached from its parent object
(by delete/splice of it or its parents) will enter the "destroy" cycle.
This cycle runs once every "delay + 1000" milliseconds. This lets all scripts who might still point to the object finish their business.
At the end of the cycle's time all internal references of the sub-object are deleted and then the sub-object
(which is a proxy-object) gets revoked - javascript deletes the reference of the proxy-object to the original target object,
allowing the garbage collector to collect it.
Caveat: handling objects outside of the proxerve object
might lead to pointing to revoked proxies (destroyed objects).
This can throw unexpected errors. Consider this example:
Solutions for this situation are at the API page
Basic usage
Note: for those who haven't worked with proxy objects before - Proxserve object actually masks a target object. Altering the proxy object allows it to trap your handlers (set, get, etc.) and do custom operations on the target object that is behind it
Initialization
Caution: after initiating a Proxserve do not refer to the original object. This will bypass Proxserve and will not emit events
Basic Event Listeners
There are 3 events - "create", "update" & "delete". You may listen to one of them or to all of them with the special "change" event.
Whenever you instantiate a proxserve it has "hidden" methods attached to the object and sub-objects.
one of them is "on()" or the alias "$on()"
Advanced usage
Advanced Event Listeners
Adding event listeners to properties not yet created, Listeners adding more listeners and more.
Watch out for race conditions