Options
All
  • Public
  • Public/Protected
  • All
Menu

Queues callers until the counter reaches 0.

example
export function downloadAll(files = []) {
const signal = signals.countdown(files.length);
files.forEach(file =>
download(file).finally(() =>
signal.decrement()));
return signal.ready();
}
example
import { registry } from '../some/component/registry';

const signal = signals.countdown();

export function loadComponent(component) {
if (component in registry) {
signal.increment();
const script = document.createElement('script');
script.async = true;
script.type = 'text/javascript';
script.src = registry[component];
script.onload = () => signal.decrement(); // countdown
document.body.appendChild(script);
}
return signal.ready();
}

Hierarchy

Index

Methods

  • decrement(count?: number): void
  • Subtracts the specified amount from the counter. When the counter reaches 0 it will be placed into an unblocked state, enabling any queued callers to proceed.

    example
    const signal = signals.countdown(3);

    async function doTask1() {
    // do stuff
    signal.decrement();
    }

    async function doTask2() {
    // do stuff
    signal.decrement();
    }

    async function doTask3() {
    // do stuff
    signal.decrement();
    }

    doTask1();
    doTask2();
    doTask3();

    export function ready() {
    return signal.ready();
    }

    Parameters

    • Optional count: number

      The number to subtract from the internal counter. Must be a positive integer.

    Returns void

  • increment(count?: number): void
  • Places the signal into a blocked state. The signal will need to be decremented by a corresponding amount in order to unblock it.

    example
    const signal = signals.countdown();

    export function appendFilesToDownload(files = []) {
    signal.increment(files.length);
    files.forEach(file =>
    download(file).finally(() =>
    signal.decrement()));
    return signal.ready();
    }

    Parameters

    • Optional count: number

      The number to add to the internal counter. Must be a positive integer.

    Returns void

  • ready(): Promise<void>
  • Queues the caller until the counter reaches 0. Once the counter reaches 0, all callers will be invoked in the order they were queued.

    example
    export function downloadAll(files = []) {
    const signal = signals.countdown(files.length);
    files.forEach(file =>
    download(file).finally(() =>
    signal.decrement()));
    return signal.ready();
    }

    Returns Promise<void>

    A Promise that will be resolved when the counter reaches 0.