TinyTick logoTinyTick

addTaskRunRunningListener

The addTaskRunRunningListener method registers a listener function with the Manager that will be called whenever the state of a relevant task run changes.

addTaskRunRunningListener(
  taskId: IdOrNull,
  taskRunId: IdOrNull,
  listener: TaskRunRunningListener,
): string
TypeDescription
taskIdIdOrNull

The Id of the task, or null as a wildcard.

taskRunIdIdOrNull

The Id of the task run, or null as a wildcard.

listenerTaskRunRunningListener

The function that will be called whenever a matching task run has changed.

returnsstring

A unique Id for the listener that can later be used to remove it.

The provided listener is a TaskRunRunningListener function, and will be called with a reference to the Manager, the Id of the task, the Id of the task run that was started, how it changed, and the reason for the change.

You can either listen for a run of a single task starting (by specifying the task Id as the method's first parameter) or for a run of any task starting (by providing a null wildcard). You can specify a specific task run Id to listen for with the second parameter, or null to listen for any matching task run starting.

Example

This example registers a listener that responds to all state changes of any run of the 'ping' task run.

import {createManager} from 'tinytick';

const manager = createManager().start();
manager.setTask('ping', async () => await fetch('https://example.org'));

const listenerId = manager.addTaskRunRunningListener(
  'ping',
  null,
  (manager, taskId, taskRunId, running, reason) =>
    console.log(
      `Task '${taskId}'; running: ${running}, reason: ${reason}`,
    ),
);

manager.scheduleTaskRun('ping');
// -> "Task 'ping'; running: false, reason: 0"

// ... wait 100ms for task to start
// -> "Task 'ping'; running: true, reason: 1"
// ... wait 100ms for task to complete
// -> "Task 'ping'; running: undefined, reason: 2"

manager.delListener(listenerId);

Since

v1.2.0