/*! * V4Fire Core * https://github.com/V4Fire/Core * * Released under the MIT license * https://github.com/V4Fire/Core/blob/master/LICENSE */ import type Async from 'core/async'; export type Label = string | symbol; export type Group = string; export type Join = boolean | 'replace'; /** * Reason why a task can be marked */ export type MarkReason = 'id' | 'label' | 'group' | 'rgxp' | 'all'; /** * Reason why a task can be killed (cleared) */ export type ClearReason = MarkReason | 'muting'| 'collision'; export interface IdObject { __id__: unknown; } export interface AsyncOptions { /** * Label of a task (the previous task with the same label will be canceled) */ label?: Label; /** * Group name of a task */ group?: Group; /** * Strategy to join competitive tasks (with the same labels): * 1. `true` - all tasks are joined to the first; * 1. `'replace'` - all tasks are joined (replaced) to the last (only for promises). */ join?: Join; } export interface AsyncCbOptions extends AsyncOptions { /** * If true, then a task namespace is marked as promisified * @default `false` */ promise?: boolean; /** * Handler/s of task clearing */ onClear?: CanArray>; /** * Handler/s of task merging: a task should merge to another task with the same label and with "join: true" strategy */ onMerge?: CanArray>; /** * Handler/s of muted task calling. * These handlers are invoked when occurring calling the task if it is muted. */ onMutedCall?: CanArray>; } export interface AsyncCbOptionsSingle extends AsyncCbOptions { /** * If false, then the proxy supports multiple callings * @default `true` */ single?: boolean; } export interface AsyncProxyOptions extends AsyncCbOptionsSingle { /** * Namespace of the proxy */ name?: string; /** * Function to clear memory of the proxy */ clearFn?: ClearFn; } export interface ClearOptions { /** * Label of the task to clear */ label?: Label; /** * Group name of the task to clear */ group?: Group | RegExp; /** * If true, then a cleanup handler of the task is prevented */ preventDefault?: boolean; } export interface ClearOptionsId extends ClearOptions { /** * Identifier of the task to clear */ id?: ID; } export interface ClearProxyOptions extends ClearOptionsId { /** * Namespace of the proxy to clear */ name?: string; } export type StrictClearOptions = Omit | Overwrite; export type StrictClearOptionsId = Omit, 'label'> | Overwrite, {label: Label; group: Group | RegExp}>; export type ProxyCb< A = unknown, R = unknown, CTX extends object = Async > = A extends never ? ((this: CTX) => R) : A extends any[] ? ((this: CTX, ...args: A) => R) : ((this: CTX, e: A) => R) | Function; export type AsyncCb = ProxyCb, void, CTX>; /** * Registered task object */ export interface Task { /** * Task unique identifier */ id: unknown; /** * Raw task object */ obj: unknown; /** * Name of the raw task object */ objName?: string; /** * Group name the task */ group?: string; /** * Label of the task */ label?: Label; /** * True if the task is paused */ paused: boolean; /** * True if the task is muted */ muted: boolean; /** * Queue of pending handlers * (if the task is paused) */ queue: Function[]; /** * List of complete handlers: * * [0] - onFulfilled * [1] - onRejected */ onComplete: Array>>; /** * List of clear handlers */ onClear: Array>; /** * Unregisters the task */ unregister: Function; /** * Function to clear the task */ clearFn?: ClearFn; } /** * Context of a task */ export type TaskCtx = { /** * Task type */ type: string; /** * Link to the registered task */ link: Task; /** * Link to a new task that replaces the current */ replacedBy?: Task; /** * Reason to clear the task */ reason?: ClearReason; } & AsyncOptions & ClearOptionsId; export interface ClearFn extends Function { (id: any, ctx: TaskCtx): void; } export interface BoundFn extends Function { (this: CTX, ...args: any[]): void; } export interface LocalCache { labels: Record; links: Map>; } export interface GlobalCache { root: LocalCache; groups: Dictionary; }