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

pull-lend

v2.1.6

Published

Lends one value at a time from a stream. Re-lends in case of errors.

Downloads

11

Readme

Build Status

pull-lend

Lends one value at a time from a stream. Re-lends in case of errors.

A client borrows a value, processes it, and returns the result to the stream later.

  • Supports multiple concurrent borrowers
  • Produces results in the order in which the sink reads the values
  • If a borrower returns an error rather than a result, the value is transparently lent to another borrower, continuing until a result is returned

Useful for delegating processing to a dynamic number of concurrent, cooperative, but unreliable clients.

Quick Example

var pull = require('pull-stream')
var lend = require('pull-lend')

var lender = lend()

function minus (err, x, cb) {
  if (err) throw err
  setTimeout(function () {
    cb(null, -x)
  }, 500)
}

function square (err, x, cb) {
  if (err) throw err
  cb(null, x * x)
}

function crash (err, x, cb) {
  if (err) throw err
  cb(true)
}

// Prints 0,1,2,-0,-1,4
pull(
  pull.count(2),
  pull.through(console.log),
  lender,
  pull.through(console.log),
  pull.drain()
)

lender.lend(minus)
lender.lend(crash)
lender.lend(minus)
lender.lend(square)

// Prints 'closed with: true'
lender.lend(function (err) {
  if (err) console.log('closed with: ' + err)
})

Signature

The following signature follows the js module signature syntax and conventions. All callbacks ('cb') have the '(err, value)' signature.

lend: () =>
lender: {
    sink: (read: (abort, cb)),
    lend: (borrower: (err, value, cb)),
    source: (abort, cb)
}

Properties

  1. Each read is first initiated by a lend (but each lend does not necessarily imply a read).
  2. Multiple values may be lent concurrently by calling lend multiple times.
  3. Once lend has been called:
    3.1 the borrower will eventually be called either with a value or an err;
    3.2 all borrowers will be called before the stream closes.
  4. The source produces results in the order in which the values were read by the sink.
  5. If a borrower returns an error, its input value will be given to another borrower later.
  6. When the borrower is called, err is truthy iff:
    6.1 the lender is not connected yet;
    6.2 the lender was closed by the source;
    6.3 all available values have been borrowed and all results have been sourced.
  7. For N values available for borrowing, it takes N successful borrowers and 1 extra lend call to close the lender.
  8. If a borrower calls its cb multiple times, all subsequent calls after the first will be ignored.

Debugging

You can obtain a trace of the internal events of the module by activating the logging using the DEBUG=pull-lend environment variable (see debug).

You can also obtain the internal state of the module at a specific point in time by calling the _state() method. It returns an object with the following properties:

    {
      reading: Boolean,    // Currently reading a value from upstream
      aborted: Boolean,    // Aborted from downstream
      ended: Boolean,      // Upstream ended
      last: Number,        // Last index of the stream (0 until ended = true)
      readNb: Number,      // Number of values read from upstream
      sourcedNb: Number,   // Number of values sourced downstream
      lentNb: Number,      // Number of values lent that have not returned yet
      pendingNb: Number,   // Number of values returned not yet sourced
      delegatedNb: Number, // Number of values returned because of an error,
                           // awaiting to be lent again
      deferredNb: Number   // Number of borrowers waiting for a value
    }

The module maintains the following invariant:

    readNb - sourcedNb = lentNb + pendingNb + delegatedNb

The output of the _state() method should not be relied on for regular operations because it depends on the implementation of the module and may change in the future.