Streams

Most of the Supersonic APIs return a promise and the response of the API call can then be achieved registering a callback to the promise. For some of the APIs (eg. those related to geolocation, compass and accelerometer Supersonic provides also alternative versions which return streams that make it easy to track as a time series the values that the API returns.

In the following a stream of compass headings is created and a callback that is called for each value is registered to the stream:

var compassStream = supersonic.device.compass.watchHeading();

compassStream.onValue( function(heading) {
  supersonic.logger.log(
    "Magnetic heading: " + heading.magneticHeading + "\n" +
    "True heading: " + heading.trueHeading + "\n" +
    "Heading accuracy: " + heading.headingAccuracy + "\n" +
    "Timestamp: " + heading.timestamp
  );
 });
compassStream = supersonic.device.compass.watchHeading()

compassStream.onValue (heading) ->
  supersonic.logger.log(
    """
    Magnetic heading: #{heading.magneticHeading}
    True heading: #{heading.trueHeading}
    Heading accuracy: #{heading.headingAccuracy}
    Timestamp: #{heading.timestamp}
    """
  )

Event streams that Supersonic returns can be manipulated (e.g. filtered, combined, ...) easily using Functional Reactive programming library Bacon.js.

Learn more about streams