This is one of the core libraries used in ScadaJS for managing events, timeouts and signalling between flow branches.
In a process flow, a condition may need to .wait
for an external signal from another
function's callback, from another process or from another network device.
button-press = new Signal
queue.on-receive = (msg) ->
button-press.go err=null, msg.data.index
...
do-something!
do-something-else!
err, button-num <~ button-press.wait
console.log "Button number #{button-num} is pressed!"
signal = new Signal
signal.wait [timeout,] callback(err, ...args) # Waits for `signal.go` to call the `callback`.
...
signal.go err, ...args
If you want to declare .wait
handler once and use the same handler over and over again, create your Signal
instance with {reusable: yes}
option:
signal = new Signal {+reusable}
signal.wait [timeout,] callback(err, ...args) # Waits for `signal.go` to call the `callback`.
...
on-some-condition ->
signal.heartbeat! # this will re-set the signal
When .joined
, returns overall error
and branch signals
branch = new SignalBranch {timeout?}
for myArray
signal = branch.add! # adds a new signal to the branch
# or with a 1 second timeout:
# signal = branch.add 1000ms
...
# do something async here
err, res <~ some-async-operation
# do something with result
signal.go err
err, signals <~ branch.joined
# all async operations are finished at this point.
branch.cancel!: Cancels the execution and properly cleans up the code registered by/after branch.joined
.
err : The error either set by
- SignalBranch's master timeout or
- any of branch signals, either by
- Signal's timeout (defined by
branch.add timeout
) or - by
.go
method'serr
argument
- Signal's timeout (defined by
signals : Array of that branch's Signal
instances where each instance has at least error
and response[]
properties.
https://cdn.jsdelivr.net/gh/ceremcem/signal@v0.4/dist/signal.js
In the following example, "hello" output waits for both signals to be emitted: