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

lazy-enqueue

v0.6.0

Published

A library to make your enqueue function lazily

Downloads

4

Readme

Intro

A lightweight and no dependencies library to make your enqueue function lazily.

Install

npm i --save lazy-enqueue

Usage

import lazy from 'lazy-enqueue'

const queue = []
const enqueue = queue.unshift.bind(queue)
const lazilyEnqueue = lazy(enqueue, {
    delay: 1000,
    bufferSize: 3,
    will: data => {
        console.group()
        console.timeEnd('time')
        console.log('[will] data:', data)
    },
    success: (value, data) => {
        if (value > 3) queue.pop()
        console.log('[success] queue:', queue)
        console.groupEnd()
    },
    failure: (err, data) => {
        console.warn('[failure] data:', data, err)
    }
})

console.time('time')
lazilyEnqueue(0);lazilyEnqueue(1)
setTimeout(() => {
    lazilyEnqueue(2);lazilyEnqueue(3);lazilyEnqueue(4);lazilyEnqueue(5)
}, 2000)

API

lazy(enqueue, [options])

import lazy from 'lazy-enqueue'

Create a lazily higher order function of the original enqueue function.

Arguments

  1. enqueue (Function)

    The original enqueue function.

  2. [options] (Object)

    • delay (Number|Promise|Function): The global delay value.

    • will (Function): The global hook, called before the enqueue action.

    • success (Function): The global hook, called after successfully enqueue.

    • failure (Function): The global hook, called if failed with any reason.

    • bufferSize (Number): The size of buffer. Default Infinity.

Returns

(lazilyEnqueue): A lazily higher order function.

delay(value)

const {delay} = lazilyEnqueue(data)

Set private delay value for the current enqueue action. In other words, override the global delay value.

All of the functions(api) return by (lazilyEnqueue) support chain calls, like this: delay(100).hook('will', fn).hook('success', fn)

Arguments

value (Number|Promise|Function)

If this value is a promise, the return value(if has) will override the original data, witch be passed to the (lazilyEnqueue) function.

If this value is a function, it will be called with specified parameters, witch passed to the (lazilyEnqueue) function, and the return value should be a number or promise.

hook(name, fn)

const {hook} = lazilyEnqueue(data)

Add private hooks for the current enqueue action. These private hooks called before the global hooks.

Arguments

  1. name (String)

    The hook name, there are only three optional value: will, success, failure.

  2. fn (Function)

    The hook handler.

    If the hook name is will, the parameters of this hook function is same to the parameters passed to the (lazilyEnqueue) function, and the return value(if has) will be passed to the next will hook as its parameter. In the end, the latest return value will be pushed to the queue (override the original data).

    If the hook name is success, the first parameter of this hook function is the return value of the original enqueue function, and the remaining parameters is same to the parameters passed to the (lazilyEnqueue) function.

    If the hook name is failure, the first parameter of this hook function is the error information. and the remaining parameters is same to the parameters passed to the (lazilyEnqueue) function.

will(fn)

const {will} = lazilyEnqueue(data)

It's same to hook('will', fn).

done(onSuccess, [onFailure])

const {done} = lazilyEnqueue(data)

It's same to hook('success', onSuccess).hook('failure', onFailure).