src/utils/mutex.ts
Properties |
|
Methods |
|
Private mutex |
Type : Promise<void>
|
Default value : Promise.resolve()
|
Defined in src/utils/mutex.ts:2
|
Public lock | ||||||
lock(callback: () => void)
|
||||||
Defined in src/utils/mutex.ts:4
|
||||||
Type parameters :
|
||||||
Parameters :
Returns :
Promise<T>
|
export class Mutex {
private mutex: Promise<void> = Promise.resolve();
public lock<T>(callback: () => T | Promise<T>): Promise<T> {
// Locking mechanism: Wait for the current mutex chain to resolve,
// then run the callback and capture its result
const resultPromise = this.mutex.then(() => callback());
// Update the mutex to wait for the current operation to finish
this.mutex = resultPromise.then(() => undefined, () => undefined);
// Return the result of the callback, so the caller gets its value
return resultPromise;
}
}
export default Mutex;