Options
All
  • Public
  • Public/Protected
  • All
Menu

Releases 1 queued caller each time it is signaled.

The auto reset signal can be used to create a critical section -- i.e. a block of code that can only be executed by 1 caller at a time. Every other caller will be queued in the order they arrive, and the next caller in the queue will only be allowed to enter the critical section when the previous caller leaves the critical section.

example
import { fetch, createRequest } from '../path/to/datalayer';

const signal = signals.autoReset(true); // start unblocked

const operation = {
method: 'POST',
base: 'my-app',
path: '/some/endpoint'
};

// ensure each network call completes
// before the next call is performed:
export async function networkCall() {
await signal.ready(); // blocks other callers
try {
const data = { ... }; // payload to POST to the endpoint
const request = createRequest(operation, null, data);
const response = await fetch(request);
return response.data;
} finally {
signal.set(); // unblock the next caller
}
}

Hierarchy

Index

Methods

  • ready(): Promise<void>
  • Queues the caller until the signal is placed into a signaled state.

    example
    const signal = signals.autoReset(true); // start unblocked

    export async function doSomething() {
    await signal.ready(); // blocks other callers
    try {
    // critical section here
    } finally {
    signal.set(); // unblock the signal so the next caller can proceed
    }
    }

    Returns Promise<void>

    A Promise that will be resolved when the signal is placed into a signaled state.

  • reset(): void
  • Places the signal into a blocked (unsignaled) state. This begins queueing any future callers.

    NOTE: You often won't need to call this method directly since this signal will reset automatically each time it is set.

    example
    const signal = signals.autoReset(true); // start unblocked

    export async function doSomething() {
    await signal.ready();
    // do stuff here
    signal.set();
    }

    export function cancel() {
    // this perpetually blocks the signal
    // unless set() gets called again
    signal.reset();
    }

    Returns void

  • set(): void
  • Places the signal into a signaled state. This will release only 1 queued caller and then immediately reset the signal.

    example
    const signal = signals.autoReset(true); // start unblocked

    export async function doSomething() {
    await signal.ready(); // blocks other callers
    try {
    // critical section here
    } finally {
    signal.set(); // unblock the signal so the next caller can proceed
    }
    }

    Returns void