Scripting: Consumer Functions

From Waltz
Revision as of 19:25, 25 July 2019 by Patrick Angle (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

In Waltz, some functions may require you to provide a Consumer object to process data. This pattern is generally used when the function you are using may not immediately have a response available to be processed. This can occur when a function needs to perform a task on the network, but the entire show should not wait for the response, only the bit of code that needs to handle the response.

Consumers in Waltz are just JavaScript functions, taking some number of arguments. No return value is expected of a consumer function.

Defining a Consumer Function

For this example, we will work with a Watchout Media Server node, which defines a function getCurrentCue(…) that requires a consumer. The Consumer for that function returns a single WoControlCue, off of which we want to print the name parameter. This can be achieved with the following snippet of code.

$.watchout.getCurrentCue(function(cue) {
    print(cue.name);
});

Notice that we defined a function that takes a single argument, and named that argument cue. The name of the argument is not important and could easily be any other valid name, but the number of arguments is important.

Using a Script Function as a Consumer

Because any function can be a consumer, we can use a Script Function node as our consumer, using its consumer property. In this example, we have a Script Function node that defines a single argument name, and a body of print(cue.name);. We can then produce an identical result to our previous example with the following line of script.

$.watchout.getCurrentCue($.scriptFunction.consumer);
Expressions and Scripting
Concepts Script Expressions · JavaScript
Fundamentals Constructors · Functions · Grammar · Operations · Outputs · Parameters · Referencing Nodes
Advanced Topics Flow Control · Consumer Functions · Loops and Iterations · Reference Chains
Errors Recursion · Script Exceptions · Syntax Errors · Try and Catch