AsyncBarrier Class
Package: @esfx/async-barrier
Enables multiple tasks to cooperatively work on an algorithm through multiple phases.
Declaration
export declare class AsyncBarrier
Examples
Note
The following example is derived from https://docs.microsoft.com/en-us/dotnet/api/system.threading.barrier?view=net-6.0#examples
import { AsyncBarrier } from "@esfx/async-barrier";
async function main() {
let count = 0;
// Create a barrier with 3 participants and a post-phase action to print results.
// When phase 2 completes, throw an exception to be observed by all participants.
const barrier = new AsyncBarrier(3, b => {
console.log(`Post-phase action: count=${count}, phase=${b.currentPhaseNumber}`);
if (b.currentPhaseNumber === 2) throw new Error("Oops");
});
// Add two participants
barrier.add(2);
barrier.participantCount; // 5
// Remove one participant
barrier.remove();
barrier.participantCount; // 4
const action = async () => {
count++;
// Wait for the current phase to end. During the post-phase action 'count' will be 4
// and 'phase' will be 0.
await barrier.signalAndWait();
count++;
// Wait for the current phase to end. During the post-phase action 'count' will be 8
// and 'phase' will be 1.
await barrier.signalAndWait();
count++;
// When phase 2 ends an exception is thrown to all participants:
try {
await barrier.signalAndWait();
}
catch (e) {
console.log(`Caught error: ${e.message}`);
}
// Wait for the current phase to end. During the post-phase action 'count' will be 16
// and 'phase' will be 3.
await barrier.signalAndWait();
};
// Start 4 async actions to serve as the 4 participants.
await Promise.all([action(), action(), action(), action()]);
}
main().catch(e => console.error(e));
// prints:
// Post-phase action: count=4, phase=0
// Post-phase action: count=8, phase=1
// Post-phase action: count=12, phase=2
// Caught error: Oops
// Post-phase action: count=16, phase=3
Remarks
An AsyncBarrier allows you to coordinate multiple asynchronous operations that should advance from one phase to the next at the same time. Each participant calls and awaits the result of signalAndWait(cancelable) to indicate it has reached the barrier. Once all participants have arrived at the barrier, each participant is resumed and continue processing.
As each phase completes, the currentPhaseNumber is incremented and any registerd post-phase action is executed prior to the participants being released.
You can use add(participantCount) and remove(participantCount) to change the number of expected participants during execution.
Constructors
constructor(participantCount, postPhaseAction)
Initializes a new instance of the Barrier class.
Declaration
constructor(participantCount: number, postPhaseAction?: (barrier: AsyncBarrier) => void | PromiseLike<void>);
Parameters
- postPhaseAction
- (barrier: AsyncBarrier) => void | PromiseLike<void>
An action to execute between each phase.
Properties
currentPhaseNumber
Gets the number of the Barrier's current phase.
Declaration
get currentPhaseNumber(): number;
Property Value
participantCount
Gets the total number of participants in the barrier.
Declaration
get participantCount(): number;
Property Value
remainingParticipants
Gets the number of participants in the barrier that haven't yet signaled in the current phase.
Declaration
get remainingParticipants(): number;
Property Value
Methods
add(participantCount)
Notifies the Barrier there will be additional participants.
Declaration
add(participantCount?: number): void;
Parameters
Returns
remove(participantCount)
Notifies the Barrier there will be fewer participants.
Declaration
remove(participantCount?: number): void;
Parameters
Returns
signalAndWait(cancelable)
Signals that a participant has reached the barrier and waits for all other participants to reach the barrier.
Declaration
signalAndWait(cancelable?: Cancelable): Promise<void>;
Parameters
Returns