Blank JavaScript
The JavaScript operator allows you to execute JavaScript snippets within a graph. This is a blank operator, the input and output ports can be added and also the desired javascript snippet.
Configuration Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
codelanguage |
string |
The programming language used in the snippet.
Default: "javascript" |
|
script |
string |
Mandatory. The inline script to be executed. If the script starts with "file://", then the script file is executed. Default: "" |
Input
None
Output
None
Creating a JavaScript Extension
- Create an extension of the JavaScript operator and add the desired input and output ports.
- Add your JavaScript snippet as an inline "script" parameter in the extension configuration or put the code into a separate file and use the prefix "file://" to specify the file name.
Basic Example
var counter = 0
$.setPortCallback("input",onInput)
function onInput(ctx,s) {
counter++
$.output(counter)
}
Generators
Generators are functions that are executed before the event processing loop starts.
var counter = 0
$.addGenerator(gen)
$.addGenerator(gen)
function gen(ctx) {
for (var i = 0; i < 3; i++ ) {
$.output(counter)
counter++
}
}
This example produces the values 0,1,2,3,4,5 on the output port "output".
Timers
This example produces the values 0,1,2,3,4,5 on the output port "output".
var counter = 0
$.addTimer("1s",t1)
function t1(ctx) {
$.output(counter)
counter++
}
This example produces the values 0,1,2... on port "output" until graph shutdown.
Shutdown Handlers
This example produces the values 0,1,2... on port "output" until graph shutdown.
var counter = 0
$.setPortCallback("input",onInput)
function onInput(ctx,s) {
counter++
}
function shutdown(ctx) {
$.output(counter)
}
$.addShutdownHandler(shutdown)
$.addShutdownHandler(shutdown)
This example produces values 7,7 if 7 values have been provided on the "input" port.
Configuration API
Configuration API provides the ability to read the configuration provided via a graph description.
function gen(ctx) {
$.str($.config.getString("str"))
$.int($.config.getInt("int"))
$.bool(""+$.config.getBool("bool"))
$.msg({Attributes: $.config.getObject("obj")})
$.msg({Attributes: $.config.asObject()})
}
$.addGenerator(gen)
This example reads the configuration of the operator and provides the given values on the output ports.
Error Handling
The fail function provides the ability to terminate a processing loop in case of an internal error.
var counter = 0
$.setPortCallback("input",onInput)
function onInput(ctx,s) {
counter++
if (counter > 1 ) {
$.fail("Failure in counter")
}
$.output(counter)
}
This example terminates the loop while processing the second input on "input" port.
