Version0 /
ActionFunctionsThis page is obsolete, see VersionCurrent/Action Functions. An accessor may define some number of action functions that are invoked by the host as described below. None of these functions is required. An accessor may implement any subset. These functions are made available to the host by setting them as fields of the exports object in the accessor script with the corresponding name. For instance, the exports.initialize = function() { send('output', 'Hello World!'); } An execution of an accessor consists of:
initialize()This function, if provided, will be invoked once by the host when the application using the accessor starts up, and possibly again each time the swarmlet is re-initialized. The accessor may get any inputs that have default values, read parameters, and initialize state variables. It may also send outputs, but note that downstream accessors will not see this data in their initialize function. They will see it in an input handler, if they have registered one. Input handlersAn input handler is a function that is invoked when a new input arrives. The accessor calls fire()If provided, the host will invoke this function when any new input is provided, or if there are no inputs at all, then whenever the host chooses to invoke it. wrapup()If provided, the host will invoke this function once when the application shuts down. This function should not send outputs. DiscussionWith these action functions, there is a variety of patterns of accessor behavior. Event reactorAn accessor provides input handlers for one or more inputs. In these handlers, it may read inputs using get(), update state variables, and/or send outputs. Spontaneous accessorThe accessor sends outputs at times of its choosing, not in reaction to inputs. For example, it may send an output in a function invoked after a timeout. The accessor can do this by calling Error conditionsIt is an error to send outputs before the host has invoked initialize or after it has invoked TerminologyAction functions are called "function" even though they are not mathematical functions, because JavaScript calls them functions. They may have side effects. |