run-exclusive
Advanced tools
Comparing version 2.1.4 to 2.1.5
{ | ||
"name": "run-exclusive", | ||
"version": "2.1.4", | ||
"version": "2.1.5", | ||
"description": "Generate functions that do not allow parallel executions", | ||
@@ -23,3 +23,4 @@ "repository": { | ||
"promise", | ||
"typescript" | ||
"typescript", | ||
"type-safe" | ||
], | ||
@@ -26,0 +27,0 @@ "homepage": "https://garronej.github.io/run-exclusive", |
168
README.md
@@ -1,3 +0,1 @@ | ||
<p align="center"> | ||
@@ -22,18 +20,41 @@ <img src="https://user-images.githubusercontent.com/6702424/74085997-1d3c1400-4a7f-11ea-9abf-81a4352f827f.png"> | ||
This is a higher-level approach to the problem addressed by [`DirtyHairy/async-mutex`](https://www.npmjs.com/package/async-mutex). | ||
While being fitted for a smaller set of use-cases, this library is way less verbose and much easier to use than `async-mutex` is. | ||
<b>Browserify friendly:</b> | ||
<b>Browserify friendly:</b> | ||
- No polyfills needed ✅ | ||
- Transpiled down to ES3 ✅ | ||
- Ultra light ✅ | ||
- Ultra light ✅ | ||
## Usage | ||
# Try it now | ||
Let us compare a run-exclusive function with a regular function. | ||
Thanks to Stackblitz you can try this lib with within your browser like if you where in VSCode. | ||
### Regular function | ||
<p align="center"> | ||
<img src="https://user-images.githubusercontent.com/6702424/74528376-70531280-4f28-11ea-9545-46d258b74454.png"> | ||
</p> | ||
[__Run the example__](https://stackblitz.com/edit/run-exclusive-hello-world?embed=1&file=index.ts) | ||
# Table of content | ||
- [Try it now](#try-it-now) | ||
- [Table of content](#table-of-content) | ||
- [Documentation](#documentation) | ||
- [``build()``](#build) | ||
- [``createGroupRef()``](#creategroupref) | ||
- [``buildMethod()``](#buildmethod) | ||
- [``buildCb()`` and ``buildMethodCb()``](#buildcb-and-buildmethodcb) | ||
- [Queued calls](#queued-calls) | ||
- [``getQueuedCallCount()``](#getqueuedcallcount) | ||
- [``cancelAllQueuedCalls()``](#cancelallqueuedcalls) | ||
- [``isRunning()``](#isrunning) | ||
- [``getPrComplete()``](#getprcomplete) | ||
# Documentation | ||
## ``build()`` | ||
Let us compare regular functions with `run-exclusive` functions. | ||
````typescript | ||
let alphabet= ""; | ||
@@ -44,3 +65,8 @@ | ||
await new Promise<void>(resolve=> setTimeout(resolve, Math.random()*100)); | ||
await new Promise( | ||
resolve=> setTimeout( | ||
resolve, | ||
Math.random()*100 | ||
) | ||
); | ||
@@ -60,8 +86,5 @@ alphabet+=letter; | ||
```` | ||
Now the same example using ``run-exclusive``: | ||
### Run exclusive function | ||
````typescript | ||
import * as runExclusive from "run-exclusive"; | ||
@@ -74,3 +97,8 @@ | ||
await new Promise<void>(resolve=> setTimeout(resolve, Math.random()*100)); | ||
await new Promise( | ||
resolve=>setTimeout( | ||
resolve, | ||
Math.random()*100 | ||
) | ||
); | ||
@@ -93,7 +121,7 @@ alphabet+=letter; | ||
## ``createGroupRef()`` | ||
## Sharing a unique lock among a group of functions | ||
To share a unique lock among a group of functions. | ||
````typescript | ||
import * as runExclusive from "run-exclusive"; | ||
@@ -128,6 +156,5 @@ | ||
spell("c").then(()=> console.log(alphabet)); //prints "aBc". | ||
```` | ||
## Defining class method | ||
## ``buildMethod()`` | ||
@@ -156,4 +183,4 @@ If you define run exclusive class methods chances are you want the lock to be restricted | ||
let alice= new Student(); | ||
let bob= new Student(); | ||
const alice= new Student(); | ||
const bob= new Student(); | ||
@@ -169,3 +196,3 @@ alice.spell("A"); | ||
## Using callback instead of promises. | ||
## ``buildCb()`` and ``buildMethodCb()`` | ||
@@ -209,48 +236,65 @@ `buildCb()` is the pending of `build()` for creating run exclusive functions that complete by invoking a callback. (Instead of resolving a promise). | ||
## Checking the queuedCalls of a run exclusive function | ||
NOTE: ``runExclusive.buildMethodCb()`` also available. | ||
It is possible to check, for a given run exclusive function, if it is currently | ||
## Queued calls | ||
It is possible to check, for a given run exclusive function, if there is currently | ||
an ongoing execution and how many calls are queued. | ||
It is also possible to cancel the queued calls. | ||
### ``getQueuedCallCount()`` | ||
Get the number of queued call of a run-exclusive function. | ||
Note that if you call a runExclusive function and call this | ||
directly after it will return 0 as there is one function call | ||
execution ongoing but 0 queued. | ||
The classInstanceObject parameter is to provide only for the run-exclusive | ||
function created with 'buildMethod[Cb]. | ||
```typescript | ||
export declare function getQueuedCallCount( | ||
runExclusiveFunction: Function, | ||
classInstanceObject?: Object | ||
): number; | ||
``` | ||
### ``cancelAllQueuedCalls()`` | ||
Cancel all queued calls of a run-exclusive function. | ||
Note that the current running call will not be cancelled. | ||
The classInstanceObject parameter is to provide only for the run-exclusive | ||
function created with 'buildMethod[Cb]. | ||
```typescript | ||
export declare function cancelAllQueuedCalls( | ||
runExclusiveFunction: Function, | ||
classInstanceObject?: Object | ||
): number; | ||
``` | ||
### ``isRunning()`` | ||
Tell if a run-exclusive function has an instance of it's call currently being | ||
performed. | ||
The classInstanceObject parameter is to provide only for the run-exclusive | ||
function created with 'buildMethod[Cb]. | ||
```typescript | ||
export declare function isRunning( | ||
runExclusiveFunction: Function, | ||
classInstanceObject?: Object | ||
): boolean; | ||
``` | ||
### ``getPrComplete()`` | ||
Return a promise that resolve when all the current queued call of a runExclusive functions have completed. | ||
The classInstanceObject parameter is to provide only for the run-exclusive | ||
function created with 'buildMethod[Cb]. | ||
````typescript | ||
/** | ||
* | ||
* Get the number of queued call of a run-exclusive function. | ||
* Note that if you call a runExclusive function and call this | ||
* directly after it will return 0 as there is one function call | ||
* execution ongoing but 0 queued. | ||
* | ||
* The classInstanceObject parameter is to provide only for the run-exclusive | ||
* function created with 'buildMethod[Cb]. | ||
* | ||
* */ | ||
export declare function getQueuedCallCount(runExclusiveFunction: Function, classInstanceObject?: Object): number; | ||
/** | ||
* | ||
* Cancel all queued calls of a run-exclusive function. | ||
* Note that the current running call will not be cancelled. | ||
* | ||
* The classInstanceObject parameter is to provide only for the run-exclusive | ||
* function created with 'buildMethod[Cb]. | ||
* | ||
*/ | ||
export declare function cancelAllQueuedCalls(runExclusiveFunction: Function, classInstanceObject?: Object): number; | ||
/** | ||
* Tell if a run-exclusive function has an instance of it's call currently being | ||
* performed. | ||
* | ||
* The classInstanceObject parameter is to provide only for the run-exclusive | ||
* function created with 'buildMethod[Cb]. | ||
*/ | ||
export declare function isRunning(runExclusiveFunction: Function, classInstanceObject?: Object): boolean; | ||
/** | ||
* Return a promise that resolve when all the current queued call of a runExclusive functions | ||
* have completed. | ||
* | ||
* The classInstanceObject parameter is to provide only for the run-exclusive | ||
* function created with 'buildMethod[Cb]. | ||
*/ | ||
export declare function getPrComplete(runExclusiveFunction: Function, classInstanceObject?: Object): Promise<void>; | ||
export declare function getPrComplete( | ||
runExclusiveFunction: Function, | ||
classInstanceObject?: Object | ||
): Promise<void>; | ||
```` | ||
92794
292