A advanced EventEmitter which allows to declare event as natural function in the inheriting children class, function must start by on prefix to be recognized as an event. The function can define a behavior by default, and user can choose to redefine this behavior, or add an additionnal subscription for this event. In addition you can unsubscribe to multiple events with an AbortController

Example

class Logger extends EventEmitter {
onLog(log:string) { console.log(log); } // behavior by default

test() {
// raise event onLog
this.onLog('test');
}
}

const logger = new Logger();
logger.test(); // displays a log 'test'

// redefine default behavior to display nothing
logger.onLog = () => {}
logger.test(); // displays nothing

// add an additionnal subscription
logger.on('log', console.log);
logger.test(); // displays a log 'test'

// remove the additionnal subscription
logger.off('log', console.log);
logger.test(); // displays nothing

// add two additionnal subscriptions with a AbortController
const controller = new AbortController();
logger.on('log', log => console.log(log), controller);
logger.on('log', log => console.error(log), controller);
logger.test(); // displays a log 'test' + an error 'test'

// Unsubscribe all the subscription with the AbortController
controller.abort();
logger.test(); // displays nothing

Hierarchy

Constructors

Properties

Methods

Constructors

Properties

_event: any
_events: any
log: ILog

Start a log

Param

Returns

a Log object with the levels of log to call

Methods

  • Event unsubscription

    Parameters

    • name: never

      Name of event without the 'on' prefix (ex: 'log' to 'onLog' event declared)

    • event: Function

      Unsubscriber Function, must be the one passed to on or once subscription methods

    Returns boolean

  • Event subscription

    Parameters

    • name: never

      Name of event without the on prefix (ex: log to onLog event declared)

    • event: Function

      Subscriber Function

    • Optional options: {
          signal?: AbortSignal;
      }
      • Optional signal?: AbortSignal

        Optional AbortSignal to stop this or multiple subscriptions in same time

    Returns void

  • Event subscription only one time, once time fired it's automatically unsubscribe

    Parameters

    • name: never

      Name of event without the on prefix (ex: log to onLog event declared)

    • event: Function

      Subscriber Function

    • Optional options: {
          signal?: AbortSignal;
      }
      • Optional signal?: AbortSignal

    Returns void

Generated using TypeDoc