0.8.2 • Published 5 years ago

smartobject-watchify v0.8.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

smartobject-watchify

An extension to make a smartobject instance listenable to its resource changes

NPM

Travis branch npm PyPI npm Greenkeeper badge Coverage Status

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 = require('smartobject'),
    soWatchify = require('smartobject-watchify');

var so = new SmartObject();
so = soWatchify(so);

so.init('temperature', 0, { sensorValue: 31, units : 'C' });

// attach a listener to receive the change from resource 'temperature/0/sensorValue' 
so.onChange('temperature/0/sensorValue', function (cVal, pVal) {
    console.log('A listener to this resource');
    console.log(cVal);  // current value
    console.log(pVal);  // previous value
});

so.onChange('temperature/0/sensorValue', function (cVal, pVal) {
    console.log('Another listener to this resource');
});

// Modify the sensorValue of the temperature sensor and the listener will be triggered
so.write('temperature', 0, 'sensorValue', 80, function (err, data) {
    if (err)
        console.log(err);
});

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:

  1. so (Object): The instance of SmartObject class.

Returns:

  • (Object) Watchified smartobject.

Examples:

var SmartObject = require('smartobject'),
    soWatchify = require('smartobject-watchify');

var so = soWatchify(new SmartObject());


so.onChange(path, listener)

Attach a listener to observe a given resource for its change.

Arguments:

  1. path (String): The path to the resource, e.g. 'humidity/6/sensorValue'.
  2. listener (Function): function(cVal, pVal) {}, where cVal is the current value and pVal is the previous value before updated.

Returns:

  • (none)

Examples:

so.onChange('temperature/3/sensorValue', function (cVal, pVal) {
    // Listen to 'temperature/3/sensorValue' for its change
});


so.onChangeOnce(path, listener)

Attach an one-time listener to observe a given resource for its change.

Arguments:

  1. path (String): The path to the resource, e.g. 'humidity/6/sensorValue'.
  2. listener (Function): function(cVal, pVal) {}, where cVal is the current value and pVal is the previous value before updated.

Returns:

  • (Constructor) WatchifiedSmartObject

Examples:

so.onChangeOnce('presence/7/dInState', function (cVal, pVal) {
    // Listen to 'presence/7/dInState' only once for its change
});


so.removeListener(path, listener)

Remove a specified listener from listening to the given resource path.

Arguments:

  1. path (String): The path to the resource, e.g. 'humidity/6/sensorValue'.
  2. listener (Function): The listener function.

Returns:

  • (none)

Examples:

var presenceListener = function (cVal, pVal) {
    // Listen to 'presence/7/dInState' only once for its change
};

// attach listener
so.onChange('presence/7/dInState', presenceListener);

// remove listener
so.removeListener('presence/7/dInState', presenceListener);


so.removeAllListeners(path)

Remove all listeners from listening to the given resource path.

Arguments:

  1. path (String): The path to the resource, e.g. 'humidity/6/sensorValue'.

Returns:

  • (none)

Examples:

so.removeAllListeners('temperature/0/sensorValue');

Licensed under MIT.

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago