Single task within the task management system.

Type Parameters

Hierarchy

Constructors

Properties

_progress: number = 0

Progress of the task (range 0 to 1).

0
_result: Optional<TSpec["TResult"]> = ...

Task result, wrapped in an Optional object.

_status: TaskStatus = "idle"

Current status of the task.

"idle"
builder: TaskBuilder<TSpec>

Builder used to create the task.

data: TSpec["TData"]

Input data for the task.

id: string

Unique identifier for the task.

logger: Logger = ...

Logger for task-related events.

name: string

Name of the task.

Accessors

  • get progress(): number
  • Progress of the task (range 0 to 1).

    Returns number

    0
    
  • set progress(progress: number): void
  • Updates the task progress.

    Parameters

    • progress: number

      New progress value (0 to 1).

    Returns void

    progress - When progress changes.

    param - When the progress parameter changes.

  • get result(): Optional<TSpec["TResult"]>
  • Task result, wrapped in an Optional object.

    Returns Optional<TSpec["TResult"]>

  • set result(result: Optional<TSpec["TResult"]>): void
  • Sets the task result.

    Parameters

    • result: Optional<TSpec["TResult"]>

      Result to set.

    Returns void

    param - When the result parameter changes.

Methods

  • Removes all listeners for all event types, as well as all global listeners.

    Returns this

    The EventEmitter instance itself, allowing for method chaining.

    emitter.clear(); // No more event listeners remain
    
  • Emits an event of a specific type, invoking all registered listeners for that event type with the provided data. Also calls any global event listeners with a GlobalEvent object.

    Type Parameters

    Parameters

    • type: TType

      The identifier for the event type to emit.

    • ...data: TaskEvents[TType] extends void ? [] : [data: TaskEvents[TType]]

      The data to pass to the event listeners. The type of this data is defined by the corresponding value in TEvents.

    Returns this

    The EventEmitter instance itself, allowing for method chaining.

    emitter.emit("data", "Sample data");
    emitter.emit("loaded");
    emitter.emit("error", new Error("Oh no!"));
  • Executes the task.

    Returns Promise<Optional<TSpec["TResult"]>>

    Result wrapped in Optional.

    If the task is not in "idle" state or if execution fails.

  • Checks if the task status matches any of the provided statuses.

    Parameters

    • ...statuses: TaskStatus[]

      List of statuses to check.

    Returns boolean

    true if the current status matches one of the provided statuses.

  • Removes a previously registered event listener for a specified event type. Use this method to unregister listeners when they are no longer needed, preventing potential memory leaks.

    Type Parameters

    Parameters

    Returns this

    The EventEmitter instance itself, allowing for method chaining.

    const onError = (error: Error) => console.error(error);

    emitter.on("error", onError);

    // ...

    emitter.off("error", onError);
  • Removes a previously registered global event listener.

    Parameters

    Returns this

    The EventEmitter instance itself, allowing for method chaining.

    const globalListener = (event: GlobalEvent<TEvents>) => {
    console.log(`Event of type ${String(event.type)} received`, event.data);
    };

    emitter.onAll(globalListener);

    // ...

    emitter.offAll(globalListener);
  • Adds an event listener for a specified event type. This method allows you to specify which event you are interested in listening to and to provide a callback function that will be executed whenever that event is emitted.

    Type Parameters

    Parameters

    • type: TType

      The identifier for the event type to listen for.

    • listener: EventListener<TaskEvents, TaskEvents[TType], Task<TSpec>>

      The callback function that will be called when the event is emitted.

    Returns this

    The EventEmitter instance itself, allowing for method chaining.

    emitter.on("data", (data: string) => {
    console.log("Data", data);
    });

    emitter.on("loaded", function () {
    console.log(
    "Emitter loaded",
    this // EventEmitter<MyEvents>
    );
    });

    emitter.on("error", (error: Error) => {
    console.error(`Error: ${error.message}`);
    });
  • Adds a global event listener that is called for every emitted event.

    Parameters

    Returns this

    The EventEmitter instance itself, allowing for method chaining.

    emitter.onAll((event: GlobalEvent<TEvents>) => {
    console.log(`Event of type ${String(event.type)} received`, event.data);
    });
  • Sets the task progress.

    Parameters

    • progress: number

      Progress value to set.

    Returns Task<TSpec>

    Instance of the task for chaining.

    progress - When progress changes.

    param - When the progress parameter changes.

  • Sets the task result and updates the status to success.

    Parameters

    • result: Optional<TSpec["TResult"]>

      Result to set.

    Returns this

    Instance of the task for chaining.

    param - When the result parameter changes.

  • Sets the task status.

    Parameters

    Returns this

    Instance of the task for chaining.

    param - Emits when the status parameter changes.

  • Converts the task to a string representation.

    Parameters

    • Optionalpretty: boolean

      If true, formats the string for readability.

    Returns string

    String representation of the task.