cyclejs-community/cycle-delayed-driver

Name: cycle-delayed-driver

Owner: Cycle.js Community

Description: Create a driver in the future as a response to a specific event

Created: 2017-02-05 10:34:34.0

Updated: 2017-03-14 12:42:45.0

Pushed: 2017-10-16 18:27:42.0

Homepage: null

Size: 128

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

cycle-delayed-driver Build Status npm version

Create a driver in the future as a response to a specific event.

Installation
npm install cycle-delayed-driver --save
Example
rt {run} from '@cycle/run';
rt xs from 'xstream';
rt {makeDelayedDriver} from 'cycle-delayed-driver';

tion loggerOnFive(item) {
 (item == 5) {
return function(sink$) {
  sink$.addListener({next: (item) => {console.log(item)}});

  return xs.empty();
};


turn null;


tion main({delayedDriver}) {
turn {
delayedDriver: xs.periodic(1000)



main, {
layedDriver: makeDelayedDriver(loggerOnFive)

The above will cause all numbers starting at 6 to be incrementally logged to the console each second.

But why?

Achieving the above is rather trivial without using the delayed driver, which is more useful in cases where you can only set up the drivers that you need in response to something else happening. For an in-depth and practicle example, have a look here.

How does this work

The delayed driver operates in the following manner:

  1. Once created, it will pass all values it receives to the method you specified when creating the driver (this will be loggerOnFive in the example above). We will refer to this method as the “inner driver creation method”.

  2. For each value it receives the inner driver creation method either returns null, in which case nothing happens, or it returns a new driver. At which point the delayed driver will do several things:

  3. Emit a value indicating that the inner driver was successfully created (the delayed driver's source is complex. See the API documentation later for more details).

  4. Forward all values it receives to the inner driver it has created.

  5. Emit all values emitted by the inner driver's source. If the inner driver's source is complex, the delayed driver can be instructed to emit the source itself.

In the event that the inner driver was not created and the stream fed into the delayed driver completes, the delayed driver will emit a value indicating that it has failed to create the inner driver.

API
makeDelayedDriver(innerDriverCreationMethod, complex = false)

A factory for the delayed driver function.

Receives an innerDriverCreationMethod which it will use to try and create a driver in response to incoming values. complex is used to indicate whether the source of the driver that might be created by innerDriverCreationMethod is complex or not.

The input to this driver is any stream at all, the values of which are fed to innerDriverCreationMethod as they arrive. Once the inner driver is successfully created this stream will be forwarded to the inner driver. The output of this driver is a set of two streams:

Arguments: Returns:

function(sink$) the delayed driver function. Expects a stream of values which may cause an internal driver to be created.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.