smartobject-watchify
An extension to make a smartobject instance listenable to its resource changes
1. Overview
This module is used make a smartobject instance able to listen to its resource changes, which could be helpful for creating a machine node responsive to any reosurce change within it.
2. Installation
$ npm install smartobject-watchify --save
3. Basic Usage
var SmartObject = soWatchify = ; var so = ;so = ; so; // attach a listener to receive the change from resource 'temperature/0/sensorValue' so; so; // Modify the sensorValue of the temperature sensor and the listener will be triggeredso;
4. APIs
require('smartobject-watchify')(so)
smartobject-watchify
exports a function that receives the smartobject instance as the parameter to be extended and returned.
Arguments:
so
(Object): The instance of SmartObject class.
Returns:
- (Object) Watchified smartobject.
Examples:
var SmartObject = soWatchify = ; var so = ;
so.onChange(path, listener)
Attach a listener to observe a given resource for its change.
Arguments:
path
(String): The path to the resource, e.g.'humidity/6/sensorValue'
.listener
(Function):function(cVal, pVal) {}
, wherecVal
is the current value andpVal
is the previous value before updated.
Returns:
- (none)
Examples:
so;
so.onChangeOnce(path, listener)
Attach an one-time listener to observe a given resource for its change.
Arguments:
path
(String): The path to the resource, e.g.'humidity/6/sensorValue'
.listener
(Function):function(cVal, pVal) {}
, wherecVal
is the current value andpVal
is the previous value before updated.
Returns:
- (Constructor) WatchifiedSmartObject
Examples:
so;
so.removeListener(path, listener)
Remove a specified listener from listening to the given resource path.
Arguments:
path
(String): The path to the resource, e.g.'humidity/6/sensorValue'
.listener
(Function): The listener function.
Returns:
- (none)
Examples:
var { // Listen to 'presence/7/dInState' only once for its change}; // attach listenerso; // remove listenerso;
so.removeAllListeners(path)
Remove all listeners from listening to the given resource path.
Arguments:
path
(String): The path to the resource, e.g.'humidity/6/sensorValue'
.
Returns:
- (none)
Examples:
so;
## License
Licensed under MIT.