Signal class

Signal is used to hold the data. Signal is immutable, each modification will generate a new signal instance.


Properties

Signal.id

String, the id of the signal, the id represents the origin of the signal. All the variations from this signal will share the same id.

Signal.seq

String, alias of Signal.id

Signal.payload

Object, the payload data

Signal.anonPayload

Primitive type, the anonymous payload. When you pass a primitive type in the signal's constructor, it will be stored in the anonymous payload

Signal.error

Error type, the error represented by the signal, if there is any.

Signal.end

Boolean, if it it and end signal. (For back pressure)


Methods

constructor(payload)

Arguments

Arguement Type Description
payload any the payload object

Note: you don't need to create a signal by your own. Usually, you use node's send or push method directly with the payload itself. These method will construct a signal for you.

create signal with constructor

const s1 = new Signal(1);
console.log(s1.payload); // --> {__anon__: 1}  anonymous payload, with value 1

const s2 = new Signal({v: 10});
console.log(s2.payload); // --> {v: 10}

Signal.new(payload)

create a new signal with new payload, but keep the signal id

Arguments

Arguement Type Description
payload any the payload object

Return

Type Description
Signal the new signal

create new signal and keep the signal id

const s1 = new Signal({v: 1});

const s2 = s1.new({v: 2});

console.log(s1.payload);  // --> {v:1}
console.log(s2.payload);  // --> {v:2}
console.log(s1.id === s2.id);  // true

Signal.get(key)

get value in the payload with a key

Arguments

Arguement Type Description
key String the key of the value in payload

Return

Type Description
any the value or null

get the payload with a key

const s1 = new Signal({v: 1});

console.log(s1.get('v'));  // --> 1

Signal.set(key, value)

set the payload with a key, returns a new signal with the same id

Arguments

Arguement Type Description
key String the key of the value in payload
value any the value

Return

Type Description
Signal the new signal

get the payload with a key

const s1 = new Signal({v: 1});
const s2 = s1.set('k', 'this is a value');
console.log(s2.get('v')); // --> 1, 
console.log(s2.get('k'));  // --> this is a value

Signal.del(key)

del the payload with a key, returns a new signal with the same id

Arguments

Arguement Type Description
key String the key of the value in payload

Return

Type Description
Signal the new signal

get the payload with a key

const s1 = new Signal({v: 1, k: 'string value', j: true});
const s2 = s1.del('k');
console.log(s2.get('v')); // --> 1, 
console.log(s2.get('k'));  // --> null

Signal.getResult()

get the value of special field __result__. This special field in signal is used to store the processing result of an actuator. The result value of actuator will be stored in this field.

Return

Type Description
any the result or null

get the payload with a key

const ns = collar.ns('test');
const input = ns.input('input');

input
  .do(s => {
    return 100;
  })
  .do(s => {
    console.log(s.getResult()); // --> 100
  });

input.push({}); // push any input signal through input node

results matching ""

    No results matching ""