Alternative API to work with event driven tasks. I used to work with the EventEmitter api but this is a fairly old API that doesn't play too well with typescript. To overcome this limitation I decided to provide this new API, which resemble EventEmitter to some extend, and is inspired by the Angular 2+ EventEmitters. The idea is simple: for each event we create an instance of this class that will keep a list of listeners.

Example

const log = LoggerManager.getLogger("examples.task-runner-simple");

let personGenerated = 0;
const genPerson = (): Person => {
const personNo = ++personGenerated;
return {
name: `John ${personNo}`,
surname: `Doe ${personNo}`,
email: `jdoe${personNo}@mailinator.com`,
username: `john${personNo}`
};
};

const tr = new TaskRunner();
tr.events.taskCompleted.addListener(() => log.debug(`${people.length} people fetched so far`));
tr.waitForCompletion().then(() => log.debug("Execution complete"));

const people: Person[] = [];
for (let i = 0; i < 30; i++) {
tr.submitTask(async () => {
await delay(Math.round(Math.random() * 10));
const p = genPerson();
people.push(p);
});
}

Type Parameters

  • T

Constructors

Properties

listeners: EventListener<T>[] = []

Methods

  • Adds a new listener. All listeners will be notified when an event is dispatched.

    Parameters

    Returns default

    An unregister function. Sometimes it's easier to have a dedicated function to unregister than keeping the listener reference around

  • Emits a single event by notifying all registered listeners. Listeners are invoked one after the other synchronously. If an event listener throws an error processing stops and the error is propagated.

    Parameters

    • data: T

      Data passed to all listeners.

    Returns void

  • Removes a previously registered listener. This is an alias for the removeListener method named like this to resemble the EventEmitter interface.

    Parameters

    Returns void

  • Adds a new listener. This is an alias for the addListener method named like this to resemble the EventEmitter interface.

    Parameters

    Returns default

    An unregister function. Sometimes it's easier to have a dedicated function to unregister than keeping the listener reference around

  • Adds a listener that is automatically unregistered after the first execution.

    Parameters

    Returns default

    An unregister function. Sometimes it's easier to have a dedicated function to unregister than keeping the listener reference around

  • Listeners are normally invoked in the order they were registered, this method allow to add a listener that is invoked before any other. The last listener prepended will always be the first one invoked.

    Parameters

    Returns (() => void)

    An unregister function. Sometimes it's easier to have a dedicated function to unregister than keeping the listener reference around

      • (): void
      • Returns void

Generated using TypeDoc