UNPKG

3.73 kBTypeScriptView Raw
1import { AnyJson } from '@salesforce/ts-types';
2type callback = (data: any) => Promise<void>;
3/**
4 * An asynchronous event listener and emitter that follows the singleton pattern. The singleton pattern allows lifecycle
5 * events to be emitted from deep within a library and still be consumed by any other library or tool. It allows other
6 * developers to react to certain situations or events in your library without them having to manually call the method themselves.
7 *
8 * An example might be transforming metadata before it is deployed to an environment. As long as an event was emitted from the
9 * deploy library and you were listening on that event in the same process, you could transform the metadata before the deploy
10 * regardless of where in the code that metadata was initiated.
11 *
12 * @example
13 * ```
14 * // Listen for an event in a plugin hook
15 * Lifecycle.getInstance().on('deploy-metadata', transformMetadata)
16 *
17 * // Deep in the deploy code, fire the event for all libraries and plugins to hear.
18 * Lifecycle.getInstance().emit('deploy-metadata', metadataToBeDeployed);
19 *
20 * // if you don't need to await anything
21 * use `void Lifecycle.getInstance().emit('deploy-metadata', metadataToBeDeployed)` ;
22 * ```
23 */
24export declare class Lifecycle {
25 private readonly listeners;
26 static readonly telemetryEventName = "telemetry";
27 static readonly warningEventName = "warning";
28 private debug;
29 private constructor();
30 /**
31 * return the package.json version of the sfdx-core library.
32 */
33 static staticVersion(): string;
34 /**
35 * Retrieve the singleton instance of this class so that all listeners and emitters can interact from any library or tool
36 */
37 static getInstance(): Lifecycle;
38 /**
39 * return the package.json version of the sfdx-core library.
40 */
41 version(): string;
42 /**
43 * Remove all listeners for a given event
44 *
45 * @param eventName The name of the event to remove listeners of
46 */
47 removeAllListeners(eventName: string): void;
48 /**
49 * Get an array of listeners (callback functions) for a given event
50 *
51 * @param eventName The name of the event to get listeners of
52 */
53 getListeners(eventName: string): callback[];
54 /**
55 * Create a listener for the `telemetry` event
56 *
57 * @param cb The callback function to run when the event is emitted
58 */
59 onTelemetry(cb: (data: Record<string, unknown>) => Promise<void>): void;
60 /**
61 * Create a listener for the `warning` event
62 *
63 * @param cb The callback function to run when the event is emitted
64 */
65 onWarning(cb: (warning: string) => Promise<void>): void;
66 /**
67 * Create a new listener for a given event
68 *
69 * @param eventName The name of the event that is being listened for
70 * @param cb The callback function to run when the event is emitted
71 */
72 on<T = AnyJson>(eventName: string, cb: (data: T) => Promise<void>): void;
73 /**
74 * Emit a `telemetry` event, causing all callback functions to be run in the order they were registered
75 *
76 * @param data The data to emit
77 */
78 emitTelemetry(data: AnyJson): Promise<void>;
79 /**
80 * Emit a `warning` event, causing all callback functions to be run in the order they were registered
81 *
82 * @param data The warning (string) to emit
83 */
84 emitWarning(warning: string): Promise<void>;
85 /**
86 * Emit a given event, causing all callback functions to be run in the order they were registered
87 *
88 * @param eventName The name of the event to emit
89 * @param data The argument to be passed to the callback function
90 */
91 emit<T = AnyJson>(eventName: string, data: T): Promise<void>;
92}
93export {};