An asynchronous event listener and emitter that follows the singleton pattern. The singleton pattern allows lifecycle events to be emitted from deep within a library and still be consumed by any other library or tool. It allows other developers to react to certain situations or events in your library without them having to manually call the method themselves.

An example might be transforming metadata before it is deployed to an environment. As long as an event was emitted from the deploy library and you were listening on that event in the same process, you could transform the metadata before the deploy regardless of where in the code that metadata was initiated.

Example

// Listen for an event in a plugin hook
Lifecycle.getInstance().on('deploy-metadata', transformMetadata)

// Deep in the deploy code, fire the event for all libraries and plugins to hear.
Lifecycle.getInstance().emit('deploy-metadata', metadataToBeDeployed);

// if you don't need to await anything
use `void Lifecycle.getInstance().emit('deploy-metadata', metadataToBeDeployed)` ;

Hierarchy

  • Lifecycle

Properties

telemetryEventName: "telemetry" = 'telemetry'
warningEventName: "warning" = 'warning'

Methods

  • Emit a given event, causing all callback functions to be run in the order they were registered

    Type Parameters

    • T = AnyJson

    Parameters

    • eventName: string

      The name of the event to emit

    • data: T

      The argument to be passed to the callback function

    Returns Promise<void>

  • Emit a telemetry event, causing all callback functions to be run in the order they were registered

    Parameters

    • data: AnyJson

      The data to emit

    Returns Promise<void>

  • Emit a warning event, causing all callback functions to be run in the order they were registered

    Parameters

    • warning: string

    Returns Promise<void>

  • Get an array of listeners (callback functions) for a given event

    Parameters

    • eventName: string

      The name of the event to get listeners of

    Returns callback[]

  • Create a new listener for a given event

    Type Parameters

    • T = AnyJson

    Parameters

    • eventName: string

      The name of the event that is being listened for

    • cb: ((data) => Promise<void>)

      The callback function to run when the event is emitted

        • (data): Promise<void>
        • Parameters

          • data: T

          Returns Promise<void>

    • Optional uniqueListenerIdentifier: string

      A unique identifier for the listener. If a listener with the same identifier is already registered, a new one will not be added

    Returns void

  • Create a listener for the telemetry event

    Parameters

    • cb: ((data) => Promise<void>)

      The callback function to run when the event is emitted

        • (data): Promise<void>
        • Parameters

          • data: Record<string, unknown>

          Returns Promise<void>

    Returns void

  • Create a listener for the warning event

    Parameters

    • cb: ((warning) => Promise<void>)

      The callback function to run when the event is emitted

        • (warning): Promise<void>
        • Parameters

          • warning: string

          Returns Promise<void>

    Returns void

  • Remove all listeners for a given event

    Parameters

    • eventName: string

      The name of the event to remove listeners of

    Returns void

  • return the package.json version of the sfdx-core library.

    Returns string