Nonstop Perpetual Motion

npm

Need private packages and team management tools?Check out npm Orgs. »

sigvalue

1.0.0 • Public • Published

sigvalue

The Signaled Value library for javascript.

Usage

After installation the only thing you need to do is require the module:

var sigvalue = require('sigvalue');

Class Signal

var s = new sigvalue.Signal()
  , context = { foo: 'bar' };
 
function emitted() {
     console.log(this === context); // true
}
 
s.once(emitted, context);
s.on(emitted, context);
s.removeListener(emitted, context);

Signal accept an extra argument which is the context or this value that should be set for the emitted signal. This means you no longer have the overhead of an event that required fn.bind in order to get a custom this value.

Class Value

Class Value has the following signals:

this.changed = new Signal();
this.itemSpliced = new Signal();
this.propUpdated = new Signal();

When Value presents a single string, number or object, call Value.set method will emit the changed signal.

var s = new sigvalue.Value(1);
s.changed.on(function (newValue, oldValue){
    //newValue == 2, oldValue == 1
})
 
s.set(2);

When Value used as Array, the itemSpliced signals will emit when call push pop shift unshift splice update method.

var s = new sigvalue.Value(new Array(1, 2));
 
s.itemSpliced.on(function (index, removed, added){
    // index === 0 index of array
    // removed === [1] removed elements start from the index of array
    // added === [] added elements start from the index of array
}, s);
 
s.shift();

When Value used as Object, the propUpdated signals will emit when call setProp or deleteProp method.

var s = new sigvalue.Value(new Object());
 
s.propUpdated.on(function (name, value, oldValue){
    // name === 'foo' the name of property
    // value === '1234' the new value of property
    // oldValue === undefined if object don't have the property else old value of property
}, s);
 
s.setProp('foo', '1234');

To get the raw object of Value, use get() method

var o = new Object()
var s = new sigvalue.Value(o);
if(s.get() === o){
    // get the raw object
}

install

npm i sigvalue

Downloadsweekly downloads

1

version

1.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability