1.0.0 • Published 10 months ago

@esfx/async-barrier v1.0.0

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
10 months ago

@esfx/async-barrier

The @esfx/async-barrier package provides the AsyncBarrier class, an async coordination primitive.

Overview

Installation

npm i @esfx/async-barrier

Usage

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();
// 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

API

You can read more about the API here.

1.0.0

2 years ago

1.0.0-dev.7

2 years ago

1.0.0-pre.42

2 years ago

1.0.0-dev.8

2 years ago

1.0.0-pre.41

2 years ago

1.0.0-dev.5

2 years ago

1.0.0-pre.44

2 years ago

1.0.0-dev.6

2 years ago

1.0.0-pre.43

2 years ago

1.0.0-dev.4

2 years ago

1.0.0-dev.0

2 years ago

1.0.0-pre.40

2 years ago

1.0.0-pre.31

2 years ago

1.0.0-pre.33

2 years ago

1.0.0-pre.35

2 years ago

1.0.0-pre.34

2 years ago

1.0.0-pre.37

2 years ago

1.0.0-pre.36

2 years ago

1.0.0-pre.39

2 years ago

1.0.0-pre.38

2 years ago

1.0.0-pre.30

3 years ago

1.0.0-pre.26

3 years ago

1.0.0-pre.25

3 years ago

1.0.0-pre.28

3 years ago

1.0.0-pre.29

3 years ago

1.0.0-pre.24

3 years ago

1.0.0-pre.23

3 years ago

1.0.0-pre.19

3 years ago

1.0.0-pre.17

3 years ago

1.0.0-pre.16

3 years ago

1.0.0-pre.13

5 years ago

1.0.0-pre.12

5 years ago

1.0.0-pre.11

5 years ago

1.0.0-pre.10

5 years ago

1.0.0-pre.9

5 years ago

1.0.0-pre.8

5 years ago

1.0.0-pre.7

5 years ago

1.0.0-pre.6

5 years ago

1.0.0-pre.5

5 years ago