UNPKG

@orbit/core

Version:

Core library for Orbit - a flexible data access and synchronization layer.

165 lines (164 loc) 5.34 kB
import { Task, Performer } from './task'; import { TaskProcessor } from './task-processor'; import { Bucket } from './bucket'; import { Evented } from './evented'; /** * Settings for a `TaskQueue`. */ export interface TaskQueueSettings<Type = string, Data = unknown, Options = unknown> { /** * Name used for tracking and debugging a task queue. */ name?: string; /** * A bucket in which to persist queue state. */ bucket?: Bucket<Task<Type, Data, Options>[]>; /** * A flag indicating whether tasks should be processed as soon as they are * pushed into a queue. Set to `false` to override the default `true` * behavior. */ autoProcess?: boolean; /** * A flag indicating whether activation should happen as part of * instantiation. Set to `false` to override the default `true` behavior. When * `autoActivate === false`, no tasks reified from the queue's bucket will be * automatically processed as part of queue instantiation, regardless of the * `autoProcess` setting. Invoke `queue.activate()` as a separate step to * finish activation and start processing tasks. */ autoActivate?: boolean; } export interface TaskQueue extends Evented { } /** * `TaskQueue` is a FIFO queue of asynchronous tasks that should be * performed sequentially. * * Tasks are added to the queue with `push`. Each task will be processed by * calling its `process` method. * * By default, task queues will be processed automatically, as soon as tasks * are pushed to them. This can be overridden by setting the `autoProcess` * setting to `false` and calling `process` when you'd like to start * processing. */ export declare class TaskQueue<Type = string, Data = unknown, Options = unknown, Result = unknown> { autoProcess: boolean; private _performer; private _name?; private _bucket?; private _tasks; private _processors; private _error?; private _resolution?; private _resolve?; private _reject?; private _reified; /** * Creates an instance of `TaskQueue`. */ constructor(target: Performer<Type, Data, Options, Result>, settings?: TaskQueueSettings<Type, Data, Options>); activate(): Promise<void>; /** * Name used for tracking / debugging this queue. */ get name(): string | undefined; /** * The object which will `perform` the tasks in this queue. */ get performer(): Performer<Type, Data, Options, Result>; /** * A bucket used to persist the state of this queue. */ get bucket(): Bucket<Task<Type, Data>[]> | undefined; /** * The number of tasks in the queue. */ get length(): number; /** * The tasks in the queue. */ get entries(): Task<Type, Data>[]; /** * The current task being processed (if actively processing), or the next * task to be processed (if not actively processing). */ get current(): Task<Type, Data>; /** * The processor wrapper that is processing the current task (or next task, * if none are being processed). */ get currentProcessor(): TaskProcessor<Type, Data, Options, Result>; /** * If an error occurs while processing a task, processing will be halted, the * `fail` event will be emitted, and this property will reflect the error * encountered. */ get error(): Error | undefined; /** * Is the queue empty? */ get empty(): boolean; /** * Is the queue actively processing a task? */ get processing(): boolean; /** * Resolves when the queue has been fully reified from its associated bucket, * if applicable. */ get reified(): Promise<void>; /** * Push a new task onto the end of the queue. * * If `autoProcess` is enabled, this will automatically trigger processing of * the queue. * * Returns the result of processing the pushed task. */ push(task: Task<Type, Data, Options>): Promise<Result>; /** * Cancels and re-tries processing the current task. * * Returns the result of the retried task. */ retry(): Promise<Result>; /** * Cancels and discards the current task. * * If `autoProcess` is enabled, this will automatically trigger processing of * the queue. */ skip(e?: Error): Promise<void>; /** * Cancels the current task and completely clears the queue. */ clear(e?: Error): Promise<void>; /** * Cancels the current task and removes it, but does not continue processing. * * Returns the canceled and removed task. */ shift(e?: Error): Promise<Task<Type, Data> | undefined>; /** * Cancels processing the current task and inserts a new task at the beginning * of the queue. This new task will be processed next. * * Returns the result of processing the new task. */ unshift(task: Task<Type, Data, Options>): Promise<Result>; /** * Processes all the tasks in the queue. Resolves when the queue is empty. */ process(): Promise<void>; private _settle; private _complete; private _fail; private _cancel; private _settleEach; private _reify; private _loadTasksFromBucket; private _persist; }