Proxserve Class
Constructor
Construct a new proxserve instance.
Parameters:
Name | Type | Default value | Description |
---|---|---|---|
target | Object|Array | The target for proxserve to observe | |
options | Object | {...} | See the constructor's options section |
options.delay | Number | 10 | |
options.strict | Boolean | true | |
options.emitReference | Boolean | false |
Returns:
Example:
Constructor's Options
Proxserve has 3 options. Each is very important and affects the core of the library.
Name | Type | Default value |
---|---|---|
delay | Number | 10 |
The delay option controls the delay of the event cycles. Whenever a change is made to the object it triggers a cycle that accumulates
all changes for several milliseconds and then fires them all together. This behavior has several benefits which you can read about at
the concepts section. The delay makes listening and responding to changes asynchronous
This event cycle can be disabled and turned synchronous by setting the delay to 0 (or lower, which will affect the
automatic destroy for garbage collection) if you need to respond to changes
immediately (at the same event loop cycle if speaking in Nodejs terms).
Example:
Name | Type | Default value |
---|---|---|
strict | Boolean | true |
The strict option determines whether Proxserve will destroy deleted/detached sub-objects automatically.
Whenever a property gets deleted, Proxserve will revoke the proxy-object of this property and will
destroy all internal metadata used by the library. This ensures the garbage collector will collect the
object (assuming there are no other references to this object). Objects will keep on living for
Disabling the strict mode will not revoke the deleted proxy-objects and will not delete its metadata.
Deleting can still lead to zero references to the object and thus still being garbage collected but because of circular references
in the metadata there are chances of objects and child objects not being garbage collected and leading to unexpected behaviour
like memory overflow. In cases like this you can destroy objects manually.
Example:
Name | Type | Default value |
---|---|---|
emitReference | Boolean | false |
This option is a tricky one. The emitReference option controls whether a reference is emitted along with the change-event object.
Emitting a reference is much faster (because it doesn't require a deep copy) but will lead to very confusing
results. For example the
Example:
Caution: when emittin a reference, the
Destroy
Destroys (revokes) a proxserve object or sub-object along with all of its children and internal references.
It does not delete the property off the parent object for you. If you will not delete a destroyed property it will refer to the original
target object.
Parameters:
Name | Type | Description |
---|---|---|
proxy | Proxy-Object | The proxserve's main object or sub-object |
Example:
splitPath
Key component of the whole events system is the path of each change. In order to analyze this string you will want to break it to
an array of object keys and array indexes. The splitPath method does that very efficiently.
Parameters:
Name | Type | Description |
---|---|---|
path | String | The path of the emitted change-event |
Returns:
Example:
evalPath
Evaluates a path according to an object. Returns the evaluated sub-object, property and the actual value.
Parameters:
Name | Type | Description |
---|---|---|
obj | Object | The object to evaluate the path for |
path | String | A path of an emitted change-event |
Returns:
Example:
Object Methods
Every proxserve object and sub-objects have special methods attached to them. You may overwrite the methods property names if needed and use the alias instead.
on (alias: $on)
Attach an event listener to the object, listening to any changes made to it or to its sub-objects.
Accepted values: "create", "update", "delete" and special value "change".
Parameters:
Name | Type | Optional | Description |
---|---|---|---|
events | String|Array | The event or events to listen for | |
path | String | Yes | Path selector. Useful for objects not yet created |
listener | Function | The listener function to invoke | |
id | Number|String | Yes | An identifier for removeListener |
Hint: use the "path" parameter in order to attach listeners
to primitives or to objects not yet created.
Examples:
once (alias: $once)
Attach an event listener that runs only once.
Parameters: same as on
removeListener (alias: $removeListener)
removes a listener from a path by an identifier or by the listener's function.
Parameters:
Name | Type | Optional | Description |
---|---|---|---|
path | String | yes | Path to a sub-property |
id | Number|String|Function | The identifier or function of the listener |
Example:
removeAllListeners (alias: $removeAllListeners)
removes all listener from a path.
Parameters:
Name | Type | Optional | Description |
---|---|---|---|
path | String | yes | Path to a sub-property |
Example:
stop
Stops the object and children from emitting change events.
Note: calling "stop" overrides "block" status
Example:
block
Blocks the object and children from changes. You can't alter or delete any property.
Note: calling "block" overrides "stop" status
Example:
activate
Resume default behavior of mutability and emitting change events, Meaning the state is inherited from parent
(unless "force" parameter is used).
Parameters:
Name | Type | Default value | Description |
---|---|---|---|
force | Boolean | false | Force being active regardless of parent's state |
Example:
getOriginalTarget
Special built-in method to get the target object behind the proxserve object.
Hint: also useful for checking if an object is a proxserve object
Example:
Caution: altering the target object behind the proxserve will not emit events