watcher

Watches over changes in objects and executes callbacks when those changes happen.
Source:
Version:
  • 2.0.6
Author:
  • Pedro Miguel Pereira Serrano Martins

Methods

(inner) get(objId) → {Object}

Source:
This function returns a shallow clone of the object with the given object id. This happens so as to minize side effects and prevents direct object manipulation to the object being watched.
Parameters:
Name Type Description
objId module:watcher~Id The unique id of the object we want to get.
Throws:
If the watch list does not contain any object with the given objId.
Type
ObjectNotWatched
Returns:
Type
Object

(inner) onChange(objId, callback)

Source:
Sets the callback for the given object. This callback will be executed everytime the object changes via the set method.
Parameters:
Name Type Description
objId module:watcher~Id The id of the object to which we want to attach a callback.
callback module:watcher~onChangeCallback The callback to be executed when the object changes via the set method.
Throws:

(inner) reset()

Source:
Removes all the watched objects and all their associated callbacks. Empties the watch list.

(inner) set(objId, newObj)

Source:
Replaces the object currently being watched with the given one. If your code has references affecting the old object (which is very unlikely given that you always work with clones) they will be lost. Calls the object's callback passing the new object as an argument.
Parameters:
Name Type Description
objId module:watcher~Id The id of the object we want to replace.
newObj Object The new object that will replace the current object with the id.
Throws:
If the watch list does not contain any object with the given objId.
Type
ObjectNotWatched

(inner) unwatch(objId)

Source:
Removes the object with the given objId from the watch list. It also deletes all callbacks associated with it.
Parameters:
Name Type Description
objId module:watcher~Id The object that will be unwatched.
Throws:
If the watch list does not contain any object with the given objId.
Type
ObjectNotWatched

(inner) watch(objId, obj)

Source:
Adds the following object to the watchlist with the given id.
Parameters:
Name Type Description
objId module:watcher~Id The unique id of the object we want to watch over.
obj Object The object that will be watched.
Throws:
If the watch list is already watching an object with the given objId.
Type
ObjectAlreadyWatched

Type Definitions

Id

Source:
The unique id of the object we want to watch over. Can be anything, althout I strongly recommend it to be a primitive type, such as a string, or a number.
Type:
  • Object

onChangeCallback(oldObj, newObj)

Source:
Parameters:
Name Type Description
oldObj Object The old object being watched.
newObj Object The new object that is now being watched.