@@ -51,7 +51,7 @@ type PromiseResolveType = (value: void | PromiseLike<void>) => void;
5151 */
5252export class ZeroBackpressureSemaphore < T , UncaughtErrorType = Error > {
5353 private readonly _availableSlotsStack : Array < number > ;
54- private readonly _slots : Array < Promise < T > | null > ;
54+ private readonly _slots : Array < Promise < T > | undefined > ;
5555
5656 // Slot availability indicator:
5757 // A pending `_waitForAvailableSlot` promise indicates "all slots are taken". Its resolve
@@ -83,7 +83,7 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
8383 this . _availableSlotsStack [ i ] = i ;
8484 }
8585
86- this . _slots = new Array ( maxConcurrentJobs ) . fill ( null ) ;
86+ this . _slots = new Array ( maxConcurrentJobs ) . fill ( undefined ) ;
8787 }
8888
8989 /**
@@ -186,7 +186,7 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
186186 * @returns A promise that resolves when all currently executing jobs are completed.
187187 */
188188 public async waitForAllExecutingJobsToComplete ( ) : Promise < void > {
189- const pendingJobs = this . _slots . filter ( job => job !== null ) ;
189+ const pendingJobs = this . _slots . filter ( job => job !== undefined ) ;
190190 if ( pendingJobs . length > 0 ) {
191191 await Promise . allSettled ( pendingJobs ) ;
192192 }
@@ -195,7 +195,7 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
195195 /**
196196 * waitForAvailability
197197 *
198- * This method resolves once at least one slot (slot) is available for job execution.
198+ * This method resolves once at least one slot is available for job execution.
199199 * In other words, it resolves when the semaphore is available to trigger a new job immediately.
200200 *
201201 * ### Example Use Case
@@ -209,6 +209,13 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
209209 * To prevent such potential backpressure, users can utilize the `waitForAvailability` method
210210 * before consuming the next message.
211211 *
212+ * ### Rarely Needed
213+ * This method can be useful when the system is experiencing high load (as indicated by CPU and/or memory
214+ * usage metrics), and you want to pause further async operations until an available job slot opens up.
215+ * However, the same effect can be achieved with `startExecution` alone if the async logic
216+ * (which you intend to delay until availability) is performed *inside the job* rather than as a
217+ * preliminary step. Therefore, `waitForAvailability` is more of a design choice than a necessity.
218+ *
212219 * @returns A promise that resolves once at least one slot is available.
213220 */
214221 public async waitForAvailability ( ) : Promise < void > {
@@ -296,7 +303,7 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
296303 // Triggered by `startExecution`: A background job.
297304 this . _uncaughtErrors . push ( err ) ;
298305 } finally {
299- this . _slots [ allottedSlot ] = null ;
306+ this . _slots [ allottedSlot ] = undefined ;
300307 this . _availableSlotsStack . push ( allottedSlot ) ;
301308
302309 // Handle state change: from unavailable to available.
0 commit comments