npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cycle-delayed-driver

v1.1.0

Published

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

Downloads

5

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

import {run} from '@cycle/run';
import xs from 'xstream';
import {makeDelayedDriver} from 'cycle-delayed-driver';

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

      return xs.empty();
    };
  }

  return null;
}

function main({delayedDriver}) {
  return {
    delayedDriver: xs.periodic(1000)
  }
}

run(main, {
  delayedDriver: 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:

    1. 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).
    2. Forward all values it receives to the inner driver it has created.
    3. 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:

  • driverCreatedStream() can be used to get a stream which will emit a single event and then end. The single value is an object of the following form:

    {
      created: false,
      reason: 'Something terrible has happened!'
    }

    created indicates whether the inner driver was created successfully, and reason details what failed in case of a failure.

  • innerDriverSource() gives access to the source of the inner driver once it has been created. If complex was false when the delayed driver was created, this will yield a simple stream that mimics the source of the inner driver. If complex was true, however, this will be a stream that emits the inner driver's source itself and then complete.

Arguments:

  • innerDriverCreationMethod: function(item) a function that is expected to receive a single argument and either return null or a new driver function.
  • complex: boolean optional argument that is false by default. False indicates the driver created by the delayed driver emits a stream. True indicates it emits a complex object.

Returns:

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