Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(initialCount: number, maxCount?: number) {
if (isMissing(maxCount)) maxCount = MAX_INT32;
if (!isNumber(initialCount)) throw new TypeError("Number expected: initialCount.");
if (!isNumber(maxCount)) throw new TypeError("Number expected: maxCount.");
if ((initialCount |= 0) < 0) throw new RangeError("Argument out of range: initialCount.");
if ((maxCount |= 0) < 1) throw new RangeError("Argument out of range: maxCount.");
if (initialCount > maxCount) throw new RangeError("Argument out of range: initialCount.");
this._currentCount = initialCount;
this._maxCount = maxCount;
}
reset(count?: number): void {
if (isMissing(count)) count = this._initialCount;
if (!isNumber(count)) throw new TypeError("Number expected: count.");
if ((count |= 0) < 0) throw new RangeError("Argument out of range: count.");
this._remainingCount = count;
this._initialCount = count;
if (this._remainingCount > 0) {
this._event.reset();
}
else {
this._event.set();
}
}
constructor(initialCount: number, maxCount?: number) {
if (isMissing(maxCount)) maxCount = MAX_INT32;
if (!isNumber(initialCount)) throw new TypeError("Number expected: initialCount.");
if (!isNumber(maxCount)) throw new TypeError("Number expected: maxCount.");
if ((initialCount |= 0) < 0) throw new RangeError("Argument out of range: initialCount.");
if ((maxCount |= 0) < 1) throw new RangeError("Argument out of range: maxCount.");
if (initialCount > maxCount) throw new RangeError("Argument out of range: initialCount.");
this._currentCount = initialCount;
this._maxCount = maxCount;
}
constructor(participantCount: number, postPhaseAction?: (barrier: AsyncBarrier) => void | PromiseLike) {
if (!isNumber(participantCount)) throw new TypeError("Number expected: participantCount.");
if ((participantCount |= 0) < 0) throw new RangeError("Argument out of range: participantCount.");
if (!isMissing(postPhaseAction) && !isFunction(postPhaseAction)) throw new TypeError("Function expected: postPhaseAction.");
this._participantCount = participantCount;
this._remainingParticipants = participantCount;
this._postPhaseAction = postPhaseAction;
}
constructor(initialCount: number) {
if (!isNumber(initialCount)) throw new TypeError("Number expected: initialCount.");
if ((initialCount |= 0) < 0) throw new RangeError("Argument out of range: initialCount.");
this._initialCount = initialCount;
this._remainingCount = initialCount;
this._event = new AsyncManualResetEvent(initialCount === 0);
}
add(participantCount?: number) {
if (isMissing(participantCount)) participantCount = 1;
if (!isNumber(participantCount)) throw new TypeError("Number expected: participantCount.");
if ((participantCount |= 0) <= 0) throw new RangeError("Argument out of range: participantCount.");
if (this._isExecutingPostPhaseAction) throw new Error("This method may not be called from within the postPhaseAction.");
this._participantCount += participantCount;
this._remainingParticipants += participantCount;
}
add(count?: number): void {
if (isMissing(count)) count = 1;
if (!isNumber(count)) throw new TypeError("Number expected: count.");
if ((count |= 0) <= 0) throw new RangeError("Argument out of range: count.");
if (this._remainingCount === 0) throw new Error("The event is already signaled and cannot be incremented.");
if (this._remainingCount > 0) {
this._remainingCount += count;
}
}
signal(count?: number): boolean {
if (isMissing(count)) count = 1;
if (!isNumber(count)) throw new TypeError("Number expected: count.");
if ((count |= 0) <= 0) throw new RangeError("Argument out of range: count.");
if (count > this._remainingCount) throw new Error("Invalid attempt to decrement the event's count below zero.");
this._remainingCount -= count;
if (this._remainingCount === 0) {
this._event.set();
return true;
}
return false;
}
release(count?: number): void {
if (isMissing(count)) count = 1;
if (!isNumber(count)) throw new TypeError("Number expected: count.");
if ((count |= 0) < 1) throw new RangeError("Argument out of range: count.");
if (this._maxCount - this._currentCount < count) throw new RangeError("Argument out of range: count.");
while (count > 0) {
count--;
if (!this._waiters.resolveOne()) {
this._currentCount++;
}
}
}
}
remove(participantCount?: number) {
if (isMissing(participantCount)) participantCount = 1;
if (!isNumber(participantCount)) throw new TypeError("Number expected: participantCount.");
if ((participantCount |= 0) <= 0) throw new RangeError("Argument out of range: participantCount.");
if (this._participantCount < participantCount) throw new RangeError("Argument out of range: participantCount.");
if (this._isExecutingPostPhaseAction) throw new Error("This method may not be called from within the postPhaseAction.");
this._participantCount -= participantCount;
this._remainingParticipants -= participantCount;
if (this._participantCount === 0) {
this._finishPhase();
}
}