Operator: actuator
Make side effects. An actuator does not change the incoming signal (the result of the actuator will be added to a special field in signal, result`, see Signal class for more details), it interacts with external worlds.
Asynchronous Operator (act function accept a callback)
Signature
NS.actuator(comment : string, inputs : map, outputs : map, act : function) : Node
Arguments
Arguement | Type | Description |
---|---|---|
comment | string | optional, the comment of the actuator |
inputs | map | optional, the input description |
outputs | map | optional, the output description |
act | function | optional, the act function, it takes a callback function as the second argument, which accepts an error and result. If the error argument is not empty, an error signal will be sent, otherwise, the result will be injected to the signal as __result__ field |
act function signature:
function act(signal : Signal, done : function) : void
done
argument is a callback function with signature:
function done(error : Error, result : any) : void
If error argument is not null, an ERROR signal is sent to stream.
If result argument is not null, the result is injected to the signal with a special name : __result__
. You can access it with signal.getResult()
Return
Type | Description |
---|---|
Node | the actuator node |
Create an async actuator
const ns = collar.ns('collarjs.demo.actuator');
// an http request actuator
const request = require('request');
const httpRequestActuator = ns.actuator("make http request",
(signal, done) => {
let url = signal.get("url");
request(url, function (error, response, body) {
done(error, body); // put the page body to the outgoing signal's result
})
});
// print collarjs.com home page
ns.just({url:"http://collarjs.com"}) // create a single signal source
.to("make http request", httpRequestActuator) // to http request actuator
.do("print response body", signal => { // print result
console.log(signal.getResult());
})
.sink(); // drive the passive source