sigvalue

2019-07-14 admin

sigvalue是什么

什么是sigvalue,SigValue make value signalable when changed.

sigvalue使用教程帮助文档

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
}

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-69715.html

文章标题:sigvalue

回到顶部