Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Mutex implements MutexInterface {
return this._semaphore.runExclusive(() => callback());
}

wrap(func: Function): Function {
return this._semaphore.wrap(func);
}

isLocked(): boolean {
return this._semaphore.isLocked();
}
Expand Down
8 changes: 8 additions & 0 deletions src/Semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class Semaphore implements SemaphoreInterface {
}
}

wrap(func: Function): Function {
return new Proxy(func, {
apply: (target, that, args) => {
return this.runExclusive(() => Promise.resolve(target.apply(that, args)));
},
});
}

isLocked(): boolean {
return this._value <= 0;
}
Expand Down