Skip to content

Library for managing events, timeouts and signalling between async calls

Notifications You must be signed in to change notification settings

ceremcem/signal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

image

Description

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

signal = new Signal

signal.wait [timeout,] callback(err, ...args)  # Waits for `signal.go` to call the `callback`.

...

signal.go err, ...args

Reusable signals

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

SignalBranch

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's err argument

signals : Array of that branch's Signal instances where each instance has at least error and response[] properties.

CDN

https://cdn.jsdelivr.net/gh/ceremcem/signal@v0.4/dist/signal.js

Example

In the following example, "hello" output waits for both signals to be emitted:

Ractive Playground

About

Library for managing events, timeouts and signalling between async calls

Resources

Stars

Watchers

Forks

Packages

No packages published