Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mafintosh/nanoguard
Small module that allows you to guard a call to a function.
https://github.com/mafintosh/nanoguard
Last synced: 12 days ago
JSON representation
Small module that allows you to guard a call to a function.
- Host: GitHub
- URL: https://github.com/mafintosh/nanoguard
- Owner: mafintosh
- License: mit
- Created: 2019-09-12T09:12:57.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-14T14:34:32.000Z (over 4 years ago)
- Last Synced: 2024-10-15T20:20:59.685Z (29 days ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 61
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nanoguard
Small module that allows you to guard a call to a function.
```
npm install nanoguard
```## Usage
``` js
const Nanoguard = require('nanoguard')
const guard = new Nanoguard()guard.wait()
// When the amount of wait() calls reflect continue() calls ready is called
guard.ready(function () {
console.log('Ready to continue!')
})guard.continue()
```## API
#### `const guard = new Nanoguard()`
Make a new guard instance
#### `guard.wait()`
Increment the wait counter.
Non-owners of the guard can use this to defer the ready function of the guard owner.#### `guard.continue()`
Decrement the wait counter on the next tick. If the counter is `0` it calls all pending
ready functions.
If you called wait() you have to call continue() at some point.#### `guard.continueSync()`
Same as `guard.continue()` but decrements in the same tick.
#### `const cont = guard.waitAndContinue()`
Calls wait and returns a function that when called calls continue() once no matter how many times it is called.
#### `guard.ready(fn)`
Pass a function that is called when the wait counter is `0`.
#### `guard.destroy()`
Force sets the wait counter to `0` forever.
Should only be called by the owner of the guard.#### `const bool = guard.waiting`
Boolean indicating if the wait counter is `> 0`.
## License
MIT