UNPKG

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