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

gate-keeper

v2.0.0

Published

No matter how frequently you request a resource, only make one request at a time

Downloads

731

Readme

The gate-keeper!

If you have some remote resource you want to fetch, and you could be requesting a bunch of times, you probably don't need more than one request going at a time.

This library uses Promises. For the callback version, see the 1.x branch.

Pairs very well with the key-master.

Install

npm install gate-keeper

const gateKeeper = require('gate-keeper')

Example

let calledAlready = false
const get = gateKeeper(function getSomeRemoteResource({ isCancelled }) {
	// this function will only be called once, and in this example
	// won't be called again afterward.
	calledAlready // => false
	
	calledAlready = true
	
	return new Promise(resolve => {
		setTimeout(function() {
			isCancelled() // => false
			resolve('A successful value')
		}, 50)
	})
})

get().then(value => {
	// err => null
	// value => 'A successful value'
})
get().then(value => {
	// value => 'A successful value'
	get.isCurrentlyGetting() // => false
})

get.isCurrentlyGetting() // => true

API

const get = gateKeeper(asyncGetterFunction)

Returns a get function that you can use whenever you want to trigger calling asyncGetterFunction.

asyncGetterFunction should return a promise. Until that promise is resolved or rejected, asyncGetterFunction will not be called again.

Your asyncGetterFunction function will be passed an object with a property named isCancelled, a function you can call that returns true or false depending on whether or not the gatekeeper's cancel method was called while the request was running.

get()

Triggers the asyncGetterFunction if it is not running already. Returns the promise associated with the currently-running getter.

get.isCurrentlyGetting()

Returns true if the asyncGetterFunction is in progress, false otherwise.

get.cancel()

If the asyncGetterFunction is in progress, its results are ignored. Promises that are unresolved when cancel is called will never resolve.

Using with the key-master

Because you probably have more than one remote resource you could be fetching.

const keyMaster = require('key-master')

const fetchers = keyMaster(key => gateKeeper(() => Promise.resolve(key + ' is awesome!')))

fetchers.get('pie')(value => {
	value // => 'pie is awesome!'
})
fetchers.get('cake')(value => {
	value // => 'cake is awesome!'
})
fetchers.get('pie')(value => {
	value // => 'pie is awesome!'
})

License

WTFPL