Options
All
  • Public
  • Public/Protected
  • All
Menu

Module src/core/event

core/event

This module provides a bunch of helper functions to handle events in more flexibly way.

resolveAfterEvents

The function returns a promise that will be resolved after emitting of all events from the specified emitter.

import { EventEmitter2 as EventEmitter } from 'eventemitter2';
import { resolveAfterEvents } from 'core/event';

const
emitter = new EventEmitter();

resolveAfterEvents(emitter, 'foo', 'bar')
.then(() => console.log('Bang!'));

emitter.emit('foo');
emitter.emit('bar');

createsAsyncSemaphore

The function wraps a callback into a new function that never calls the target until all specified flags are resolved. createsAsyncSemaphore returns a new function that takes a string flag and resolves it. After all, flags are resolved, the last function invokes the target function. If you try to invoke the function after the first time resolving, ii won't be executed.

import { EventEmitter2 as EventEmitter } from 'eventemitter2';
import { createsAsyncSemaphore } from 'core/event';

const semaphore = createsAsyncSemaphore(() => {
console.log('Boom!');
}, 'foo', 'bar');

semaphore('foo');
semaphore('bar') // Boom!

Index

Variables

afterEvents: WarnedFn<[emitter: EventEmitterLike, cb: string | Function, ...events: string[]], Promise<void>> = ...
deprecated
see

resolveAfterEvents

createSyncPromise: WarnedFn<[resolveValue?: unknown, rejectValue?: unknown], Promise<unknown>> = ...

Creates a synchronous promise wrapper for the specified value

deprecated
see

SyncPromise

param resolveValue
param rejectValue
onEverythingReady: WarnedFn<[cb: () => unknown, ...flags: string[]], (flag: string) => void> = ...
deprecated
see

createsAsyncSemaphore

Functions

  • createsAsyncSemaphore<T>(cb: () => T, ...flags: string[]): (flag: string) => CanUndef<T>
  • Wraps a callback into a new function that never calls the target until all specified flags are resolved. The function returns a new function that takes a string flag and resolves it. After all, flags are resolved, the last function invokes the target function. If you try to invoke the function after the first time resolving, ii won't be executed.

    example
    const semaphore = createsAsyncSemaphore(() => {
    console.log('Boom!');
    }, 'foo', 'bar');

    semaphore('foo');
    semaphore('bar'); // 'Boom!'

    // Function already resolved, the target function isn't executed
    semaphore();

    Type parameters

    • T

    Parameters

    • cb: () => T

      callback function that is invoked after resolving all flags

        • (): T
        • Returns T

    • Rest ...flags: string[]

      flags to resolve

    Returns (flag: string) => CanUndef<T>

      • Wraps a callback into a new function that never calls the target until all specified flags are resolved. The function returns a new function that takes a string flag and resolves it. After all, flags are resolved, the last function invokes the target function. If you try to invoke the function after the first time resolving, ii won't be executed.

        example
        const semaphore = createsAsyncSemaphore(() => {
        console.log('Boom!');
        }, 'foo', 'bar');

        semaphore('foo');
        semaphore('bar'); // 'Boom!'

        // Function already resolved, the target function isn't executed
        semaphore();

        Parameters

        • flag: string

        Returns CanUndef<T>

  • resolveAfterEvents(emitter: EventEmitterLike, ...events: string[]): Promise<void>
  • Returns a promise that will be resolved after emitting of all events from the specified emitter

    example
    // The promise will be resolved after two window events
    resolveAfterEvents(window, 'resize', 'scroll').then(() => {
    console.log('Boom!');
    });

    Parameters

    Returns Promise<void>