# gate-keeper

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

Latest version **2.0.0** (published 2017-09-07) · WTFPL license · 0 weekly downloads

## Install

```sh
npm install gate-keeper
pnpm add gate-keeper
yarn add gate-keeper
bun add gate-keeper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2017-09-07 |
| First published | 2016-08-27 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | TehShrike |
| Maintainers | tehshrike |
| Keywords | async, mutex |

## Links

- npm: https://www.npmjs.com/package/gate-keeper
- Repository: https://github.com/TehShrike/gate-keeper
- Homepage: https://github.com/TehShrike/gate-keeper#readme
- Issues: https://github.com/TehShrike/gate-keeper/issues
- npm.io page: https://npm.io/package/gate-keeper

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2017-09-07
- 1.2.0 — 2016-09-08
- 1.1.0 — 2016-08-30
- 1.0.2 — 2016-08-28
- 1.0.1 — 2016-08-27
- 1.0.0 — 2016-08-27

## 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](https://github.com/TehShrike/gate-keeper/tree/1.x).

Pairs very well with the [key-master](https://github.com/TehShrike/key-master).

# Install

```sh
npm install gate-keeper
```

`const gateKeeper = require('gate-keeper')`

# Example

<!-- js
const gateKeeper = require('./')
-->

```js
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.

```js
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](http://wtfpl2.com/)

---
_Source: https://npm.io/package/gate-keeper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
