Options
All
  • Public
  • Public/Protected
  • All
Menu

Class default<CTX>

Class to control asynchronous operations

Type parameters

Hierarchy

Index

Constructors

  • new default<CTX>(ctx?: CTX): default<CTX>

Properties

cache: Dictionary<GlobalCache> = ...

Cache for async operations

context: CTX
deprecated
see

[[Async.ctx]]

ctx: CTX

Context of applying for async handlers

idsMap: WeakMap<object, object> = ...

Map for task identifiers

locked: boolean = false

The lock status. If true, then all new tasks won't be registered.

usedNamespaces: Set<string> = ...

Set of used async namespaces

workerCache: WeakMap<object, boolean> = ...

Cache for initialized workers

linkNames: { eventListener: "eventListener"; eventListenerPromise: "eventListenerPromise"; idleCallback: "idleCallback"; idleCallbackPromise: "idleCallbackPromise"; immediate: "immediate"; immediatePromise: "immediatePromise"; interval: "interval"; intervalPromise: "intervalPromise"; iterable: "iterable"; promise: "promise"; proxy: "proxy"; proxyPromise: "proxyPromise"; request: "request"; timeout: "timeout"; timeoutPromise: "timeoutPromise"; worker: "worker" } = namespaces
deprecated
see

Async.namespaces

Type declaration

  • eventListener: "eventListener"
  • eventListenerPromise: "eventListenerPromise"
  • idleCallback: "idleCallback"
  • idleCallbackPromise: "idleCallbackPromise"
  • immediate: "immediate"
  • immediatePromise: "immediatePromise"
  • interval: "interval"
  • intervalPromise: "intervalPromise"
  • iterable: "iterable"
  • promise: "promise"
  • proxy: "proxy"
  • proxyPromise: "proxyPromise"
  • request: "request"
  • timeout: "timeout"
  • timeoutPromise: "timeoutPromise"
  • worker: "worker"
namespaces: { eventListener: "eventListener"; eventListenerPromise: "eventListenerPromise"; idleCallback: "idleCallback"; idleCallbackPromise: "idleCallbackPromise"; immediate: "immediate"; immediatePromise: "immediatePromise"; interval: "interval"; intervalPromise: "intervalPromise"; iterable: "iterable"; promise: "promise"; proxy: "proxy"; proxyPromise: "proxyPromise"; request: "request"; timeout: "timeout"; timeoutPromise: "timeoutPromise"; worker: "worker" } = namespaces

Map of namespaces for async operations

Type declaration

  • eventListener: "eventListener"
  • eventListenerPromise: "eventListenerPromise"
  • idleCallback: "idleCallback"
  • idleCallbackPromise: "idleCallbackPromise"
  • immediate: "immediate"
  • immediatePromise: "immediatePromise"
  • interval: "interval"
  • intervalPromise: "intervalPromise"
  • iterable: "iterable"
  • promise: "promise"
  • proxy: "proxy"
  • proxyPromise: "proxyPromise"
  • request: "request"
  • timeout: "timeout"
  • timeoutPromise: "timeoutPromise"
  • worker: "worker"

Accessors

  • get linkNames(): { eventListener: "eventListener"; eventListenerPromise: "eventListenerPromise"; idleCallback: "idleCallback"; idleCallbackPromise: "idleCallbackPromise"; immediate: "immediate"; immediatePromise: "immediatePromise"; interval: "interval"; intervalPromise: "intervalPromise"; iterable: "iterable"; promise: "promise"; proxy: "proxy"; proxyPromise: "proxyPromise"; request: "request"; timeout: "timeout"; timeoutPromise: "timeoutPromise"; worker: "worker" }
  • deprecated
    see

    [[Async.namespaces]]

    Returns { eventListener: "eventListener"; eventListenerPromise: "eventListenerPromise"; idleCallback: "idleCallback"; idleCallbackPromise: "idleCallbackPromise"; immediate: "immediate"; immediatePromise: "immediatePromise"; interval: "interval"; intervalPromise: "intervalPromise"; iterable: "iterable"; promise: "promise"; proxy: "proxy"; proxyPromise: "proxyPromise"; request: "request"; timeout: "timeout"; timeoutPromise: "timeoutPromise"; worker: "worker" }

    • eventListener: "eventListener"
    • eventListenerPromise: "eventListenerPromise"
    • idleCallback: "idleCallback"
    • idleCallbackPromise: "idleCallbackPromise"
    • immediate: "immediate"
    • immediatePromise: "immediatePromise"
    • interval: "interval"
    • intervalPromise: "intervalPromise"
    • iterable: "iterable"
    • promise: "promise"
    • proxy: "proxy"
    • proxyPromise: "proxyPromise"
    • request: "request"
    • timeout: "timeout"
    • timeoutPromise: "timeoutPromise"
    • worker: "worker"
  • get namespaces(): { eventListener: "eventListener"; eventListenerPromise: "eventListenerPromise"; idleCallback: "idleCallback"; idleCallbackPromise: "idleCallbackPromise"; immediate: "immediate"; immediatePromise: "immediatePromise"; interval: "interval"; intervalPromise: "intervalPromise"; iterable: "iterable"; promise: "promise"; proxy: "proxy"; proxyPromise: "proxyPromise"; request: "request"; timeout: "timeout"; timeoutPromise: "timeoutPromise"; worker: "worker" }
  • Link to Async.namespaces

    Returns { eventListener: "eventListener"; eventListenerPromise: "eventListenerPromise"; idleCallback: "idleCallback"; idleCallbackPromise: "idleCallbackPromise"; immediate: "immediate"; immediatePromise: "immediatePromise"; interval: "interval"; intervalPromise: "intervalPromise"; iterable: "iterable"; promise: "promise"; proxy: "proxy"; proxyPromise: "proxyPromise"; request: "request"; timeout: "timeout"; timeoutPromise: "timeoutPromise"; worker: "worker" }

    • eventListener: "eventListener"
    • eventListenerPromise: "eventListenerPromise"
    • idleCallback: "idleCallback"
    • idleCallbackPromise: "idleCallbackPromise"
    • immediate: "immediate"
    • immediatePromise: "immediatePromise"
    • interval: "interval"
    • intervalPromise: "intervalPromise"
    • iterable: "iterable"
    • promise: "promise"
    • proxy: "proxy"
    • proxyPromise: "proxyPromise"
    • request: "request"
    • timeout: "timeout"
    • timeoutPromise: "timeoutPromise"
    • worker: "worker"

Methods

  • Cancels the specified iterable object. Notice that cancellation affects only objects that have already been activated by invoking the next method. So, for example, canceled iterable will throw an error on the next invoking of next.

    alias

    Parameters

    • Optional id: AsyncIterable<unknown>

    Returns default<CTX>

  • Cancels the specified iterable or a group of iterable. Notice that cancellation affects only objects that have already been activated by invoking the next method. So, for example, canceled iterable will throw an error on the next invoking of next.

    alias

    Parameters

    • opts: ClearOptionsId<AsyncIterable<unknown>>

      options for the operation

    Returns default<CTX>

  • cancelTask(task: any, name?: string): default<CTX>
  • clearAsync(opts: any, name?: string): default<CTX>
  • Cancels the specified iterable object. Notice that cancellation affects only objects that have already been activated by invoking the next method. So, for example, canceled iterable will throw an error on the next invoking of next.

    Parameters

    • Optional id: Promise<unknown>

    Returns default<CTX>

  • Cancels the specified iterable object. Notice that cancellation affects only objects that have already been activated by invoking the next method. So, for example, canceled iterable will throw an error on the next invoking of next.

    Parameters

    Returns default<CTX>

  • getBaseIterator<T>(iterable: Iterable<T> | AsyncIterable<T>): CanUndef<IterableIterator<T> | AsyncIterableIterator<T>>
  • initCache(name: string, promise?: boolean): GlobalCache
  • iterable<T>(iterable: Iterable<T> | AsyncIterable<T>, opts?: AsyncOptions): AsyncIterable<T> | (AsyncIterable<T> & Iterable<T>)
  • Creates a new asynchronous iterable object from the specified iterable and returns it. If the passed iterable doesn't have Symbol.asyncIterator, it will be created from a synchronous object iterator (the synchronous iterator will also be preserved).

    Notice, until the created promise object isn't executed by invoking the next method, any async operations won't be registered.

    example
    const async = new Async();

    for await (const el of async.iterable([1, 2, 3, 4])) {
    console.log(el);
    }

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T> | AsyncIterable<T>
    • Optional opts: AsyncOptions

    Returns AsyncIterable<T> | (AsyncIterable<T> & Iterable<T>)

  • markAsync(label: string, opts: any, name?: string): default<CTX>
  • markPromise(label: string, id?: Promise<unknown>): default<CTX>
  • markPromise(label: string, opts: ClearOptionsId<Promise<unknown>>): default<CTX>
  • markTask(label: string, task: any, name?: string): default<CTX>
  • Mutes the specified iterable object. Elements that are consumed during the object is muted will be ignored. Notice that muting affects only objects that have already been activated by invoking the next method.

    Parameters

    • Optional id: AsyncIterable<unknown>

    Returns default<CTX>

  • Mutes the specified iterable object or a group of iterable objects. Elements, that are consumed during the object is muted will be ignored. Notice that muting affects only objects that have already been activated by invoking the next method.

    Parameters

    • opts: ClearOptionsId<AsyncIterable<unknown>>

      options for the operation

    Returns default<CTX>

  • Attaches an event listener from the specified event emitter. If the emitter is a function, it is interpreted as the function to attach events. Notice, if you don't provide a group for the operation, it will be taken from the event name.

    Type parameters

    • E = unknown

    • R = unknown

    Parameters

    • emitter: EventEmitterLikeP

      event emitter

    • events: CanArray<string>

      event or list of events (can also specify multiple events by using spaces)

    • handler: ProxyCb<E, R, CTX>

      event handler

    • Rest ...args: unknown[]

    Returns Nullable<EventId>

  • Attaches an event listener from the specified event emitter. If the emitter is a function, it is interpreted as the function to attach events. Notice, if you don't provide a group for the operation, it will be taken from the event name.

    Type parameters

    • E = unknown

    • R = unknown

    Parameters

    • emitter: EventEmitterLikeP

      event emitter

    • events: CanArray<string>

      event or list of events (can also specify multiple events by using spaces)

    • handler: ProxyCb<E, R, CTX>

      event handler

    • opts: AsyncOnOptions<CTX>

      options for the operation

    • Rest ...args: unknown[]

    Returns Nullable<EventId>

  • onPromiseClear(resolve: Function, reject: Function): Function
  • onPromiseMerge(resolve: Function, reject: Function): Function
  • Attaches an event listener from the specified event emitter, but the event is listened only once. If the emitter is a function, it is interpreted as the function to attach events. Notice, if you don't provide a group for the operation, it will be taken from the event name.

    Type parameters

    • E = unknown

    • R = unknown

    Parameters

    • emitter: EventEmitterLikeP

      event emitter

    • events: CanArray<string>

      event or list of events (can also specify multiple events by using spaces)

    • handler: ProxyCb<E, R, CTX>

      event handler

    • Rest ...args: unknown[]

    Returns Nullable<EventId>

  • Attaches an event listener from the specified event emitter, but the event is listened only once. If the emitter is a function, it is interpreted as the function to attach events. Notice, if you don't provide a group for the operation, it will be taken from the event name.

    Type parameters

    • E = unknown

    • R = unknown

    Parameters

    • emitter: EventEmitterLikeP

      event emitter

    • events: CanArray<string>

      event or list of events (can also specify multiple events with a space)

    • handler: ProxyCb<E, R, CTX>

      event handler

    • opts: AsyncOnceOptions<CTX>

      options for the operation

    • Rest ...args: unknown[]

    Returns Nullable<EventId>

  • Creates a new promise that wraps the passed promise and returns it.

    This method doesn't attach any hook or listeners to the object, but if we cancel the operation by using one of Async's methods, like, "cancelPromise", the promise will be rejected.

    The promise can be provided as it is or as a function, that returns a promise.

    example
    const
    async = new Async();

    async.promise(new Promise(() => {
    // ...
    }))

    Type parameters

    • T = unknown

    Parameters

    Returns Promise<T>

  • Returns a promise that is resolved after emitting the specified event. If the emitter is a function, it is interpreted as the function to attach events. Notice, if you don't provide a group for the operation, it will be taken from the event name.

    Type parameters

    • R = unknown

    • E = unknown

    Parameters

    Returns Promise<R>

  • Returns a promise that is resolved after emitting the specified event. If the emitter is a function, it is interpreted as the function to attach events. Notice, if you don't provide a group for the operation, it will be taken from the event name.

    Type parameters

    • R = unknown

    Parameters

    • emitter: EventEmitterLikeP

      event emitter

    • events: CanArray<string>

      event or list of events (can also specify multiple events with a space)

    • Rest ...args: unknown[]

    Returns Promise<R>

  • Creates a new function that wraps the original and returns it.

    This method doesn't attach any hook or listeners to the object, but if we cancel the operation by using one of Async's methods, like, cancelProxy, the target function won't be invoked.

    example
    const
    async = new Async();

    myImage.onload = async.proxy(() => {
    // ...
    });

    Type parameters

    Parameters

    Returns F

  • request<T>(request: (() => PromiseLike<T>) | PromiseLike<T>, opts?: AsyncRequestOptions): Promise<T>
  • Creates a promise that wraps the passed request and returns it.

    This method doesn't attach any hook or listeners to the object, but if we cancel the operation by using one of Async's methods, like, "cancelRequest", the promise will be rejected.

    The request can be provided as a promise or function, that returns a promise. Notice, the method uses Async.promise, but with a different namespace: request instead of promise.

    example
    const async = new Async();
    async.request(fetch('foo/bla'));

    Type parameters

    • T = unknown

    Parameters

    Returns Promise<T>

  • Wraps the specified worker object.

    This method doesn't attach any hook or listeners to the object, but every time the same object is registered, Async will increment the number of links that relate to this object. After, when we try to destroy the worker by using one of Async's methods, like, terminateWorker, it will de-increment values of links. When the number of links is equal to zero, Async will try to call a "real" object destructor by using one of the possible destructor methods from the whitelist or by the specified destructor name, also if the worker is a function, it is interpreted as the destructor.

    example
    const
    async = new Async(),
    el = document.createElement('div');

    $el.appendChild(el);

    // This function will work as the worker destructor
    async.worker(() => el.remove());

    const
    myWorker = new Worker('my-worker.js');

    async.worker(myWorker);

    async.clearAll();

    Type parameters

    Parameters

    Returns T

  • The wrapper takes a link to the "raw" data provider and returns a new object that based on the original, but all async methods and properties are wrapped by Async. Notice, the wrapped methods can take additional Async parameters, like group or label. If you don't provide a group, it will be taken from the provider name.

    example
    import Async from 'core/async';
    import Provider, { provider } from 'core/data';

    @provider('api')
    export default class User extends Provider {
    baseURL = 'user/:id';
    }

    const
    $a = new Async(),
    wrappedProvider = $a.wrapDataProvider(new User());

    wrappedProvider.get({uuid: 1}).then((res) => {
    console.log(res);
    });

    // By default, all wrapped methods have a group name that is equal to the provider name.
    // So we can use it to clear or suspend requests, etc.
    $a.clearAll({group: 'api.User'})

    wrappedProvider.upd({uuid: 1}, {
    // All wrapped methods can take additional Async parameters as the second argument: `group`, `label` and `join`
    group: 'bla',
    label: 'foo',
    join: true,

    // Also, the second argument of the wrapped method can take the original parameters from a provider
    headers: {
    'X-Foo': '1'
    }

    }).then((res) => {
    console.log(res);
    });

    // If we are providing a group to the method, it will be joined with the global group by using the `:` character
    $a.suspendAll({group: 'api.User:bla'});

    // Obviously, we can use a group as RegExp
    $a.muteAll({group: /api\.User/});

    // We can use any methods or properties from the original data provider
    wrappedProvider.dropCache();

    Type parameters

    Parameters

    Returns W

  • wrapEventEmitter<T>(emitter: T, opts?: AsyncOptionsForWrappers): Pick<T, Exclude<keyof T, "addEventListener" | "addListener">> & { addEventListener: T["addEventListener"] extends (...args: any[]) => any ? AddEventListenerLikeFunctionMapper<any[any]> : never; addListener: T["addListener"] extends (...args: any[]) => any ? AddEventListenerLikeFunctionMapper<any[any]> : never } & EventEmitterWrapper<default<default<any>>>
  • The wrapper takes a link to the "raw" event emitter and returns a new object that based on the original, but all async methods and properties are wrapped by Async. Notice, the wrapped methods can take additional Async parameters, like group or label. In addition, the wrapper adds new methods, like "on" or "off", to make the emitter API more standard.

    example
    import Async from 'core/async';

    const
    $a = new Async(),
    wrappedEventEmitter = $a.wrapEventEmitter(window);

    const handler = () => console.log('scroll event');

    // We can safely listen to emitter events,
    // cause all emitter methods, like `addListener` or `on` are wrapped by Async.
    const id = wrappedEventEmitter.addEventListener('scroll', handler, {
    // Notice, the third argument can take Async parameters in addition to the native emitter parameters
    capture: true,
    label: 'label'
    });

    // The wrapper preserves the original API of emitter methods, so we can call something like this
    wrappedEventEmitter.removeEventListener('scroll', handler);

    // Finally, the wrapper adds a bunch of standard methods to the emitter, like `on`, `once`, and other stuff.
    // We can use their instead of the original methods to make our code more universal.
    wrappedEventEmitter.once('resize', (e) => {
    console.log(e);
    }, {group: 'resizers'});

    $a.muteAll({group: 'resizers'});

    // We can use any methods or properties from the original emitter
    console.log(wrappedEventEmitter.name); // window.name

    Type parameters

    Parameters

    Returns Pick<T, Exclude<keyof T, "addEventListener" | "addListener">> & { addEventListener: T["addEventListener"] extends (...args: any[]) => any ? AddEventListenerLikeFunctionMapper<any[any]> : never; addListener: T["addListener"] extends (...args: any[]) => any ? AddEventListenerLikeFunctionMapper<any[any]> : never } & EventEmitterWrapper<default<default<any>>>

  • The wrapper takes a link to the "raw" async storage and returns a new object that based on the original, but all async methods and properties are wrapped by Async. Notice, the wrapped methods can take additional Async parameters, like group or label.

    example
    import Async from 'core/async';
    import { asyncLocal } from 'core/kv-storage';

    const
    $a = new Async(),
    wrappedStorage = $a.wrapStorage(asyncLocal, {group: 'bar'});

    wrappedStorage.set('someKey', 'someValue', {
    // If we are providing a group to the method, it will be joined with the global group by using the `:` character
    group: 'bla',
    label: 'foo',
    join: true,
    }).then(async () => {
    console.log(await wrappedStorage.get('someKey') === 'someValue');
    });

    $a.suspendAll({group: 'bar:bla'});

    // We can provide own global group to namespace, it will be joined with the parent's global group
    const blaStore = wrappedStorage.namespace('[[BLA]]', {group: 'bla'});

    blaStore.clear({group: 'foo'});

    $a.muteAll({group: 'bar:bla:foo'});

    Type parameters

    Parameters

    Returns T extends AsyncStorage ? WrappedAsyncStorage : WrappedAsyncStorageNamespace