@salesforce/core 5.2.9 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,6 +23,7 @@ type callback = (data: any) => Promise<void>;
23
23
  */
24
24
  export declare class Lifecycle {
25
25
  private readonly listeners;
26
+ private readonly uniqueListeners;
26
27
  static readonly telemetryEventName = "telemetry";
27
28
  static readonly warningEventName = "warning";
28
29
  private logger?;
@@ -68,8 +69,9 @@ export declare class Lifecycle {
68
69
  *
69
70
  * @param eventName The name of the event that is being listened for
70
71
  * @param cb The callback function to run when the event is emitted
72
+ * @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
71
73
  */
72
- on<T = AnyJson>(eventName: string, cb: (data: T) => Promise<void>): void;
74
+ on<T = AnyJson>(eventName: string, cb: (data: T) => Promise<void>, uniqueListenerIdentifier?: string): void;
73
75
  /**
74
76
  * Emit a `telemetry` event, causing all callback functions to be run in the order they were registered
75
77
  *
@@ -35,8 +35,9 @@ const logger_1 = require("./logger/logger");
35
35
  * ```
36
36
  */
37
37
  class Lifecycle {
38
- constructor(listeners = {}) {
38
+ constructor(listeners = {}, uniqueListeners = new Map()) {
39
39
  this.listeners = listeners;
40
+ this.uniqueListeners = uniqueListeners;
40
41
  }
41
42
  /**
42
43
  * return the package.json version of the sfdx-core library.
@@ -74,7 +75,7 @@ class Lifecycle {
74
75
  const oldInstance = global.salesforceCoreLifecycle;
75
76
  // use the newer version and transfer any listeners from the old version
76
77
  // object spread keeps them from being references
77
- global.salesforceCoreLifecycle = new Lifecycle({ ...oldInstance.listeners });
78
+ global.salesforceCoreLifecycle = new Lifecycle({ ...oldInstance.listeners }, oldInstance.uniqueListeners);
78
79
  // clean up any listeners on the old version
79
80
  Object.keys(oldInstance.listeners).map((eventName) => {
80
81
  oldInstance.removeAllListeners(eventName);
@@ -96,6 +97,7 @@ class Lifecycle {
96
97
  */
97
98
  removeAllListeners(eventName) {
98
99
  this.listeners[eventName] = [];
100
+ this.uniqueListeners.delete(eventName);
99
101
  }
100
102
  /**
101
103
  * Get an array of listeners (callback functions) for a given event
@@ -103,7 +105,7 @@ class Lifecycle {
103
105
  * @param eventName The name of the event to get listeners of
104
106
  */
105
107
  getListeners(eventName) {
106
- const listeners = this.listeners[eventName];
108
+ const listeners = this.listeners[eventName]?.concat(Array.from((this.uniqueListeners.get(eventName) ?? []).values()) ?? []);
107
109
  if (listeners) {
108
110
  return listeners;
109
111
  }
@@ -133,8 +135,9 @@ class Lifecycle {
133
135
  *
134
136
  * @param eventName The name of the event that is being listened for
135
137
  * @param cb The callback function to run when the event is emitted
138
+ * @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
136
139
  */
137
- on(eventName, cb) {
140
+ on(eventName, cb, uniqueListenerIdentifier) {
138
141
  const listeners = this.getListeners(eventName);
139
142
  if (listeners.length !== 0) {
140
143
  if (!this.logger) {
@@ -142,8 +145,20 @@ class Lifecycle {
142
145
  }
143
146
  this.logger.debug(`${listeners.length + 1} lifecycle events with the name ${eventName} have now been registered. When this event is emitted all ${listeners.length + 1} listeners will fire.`);
144
147
  }
145
- listeners.push(cb);
146
- this.listeners[eventName] = listeners;
148
+ if (uniqueListenerIdentifier) {
149
+ if (!this.uniqueListeners.has(eventName)) {
150
+ // nobody is listening to the event yet
151
+ this.uniqueListeners.set(eventName, new Map([[uniqueListenerIdentifier, cb]]));
152
+ }
153
+ else if (!this.uniqueListeners.get(eventName)?.has(uniqueListenerIdentifier)) {
154
+ // the unique listener identifier is not already registered
155
+ this.uniqueListeners.get(eventName)?.set(uniqueListenerIdentifier, cb);
156
+ }
157
+ }
158
+ else {
159
+ listeners.push(cb);
160
+ this.listeners[eventName] = listeners;
161
+ }
147
162
  }
148
163
  /**
149
164
  * Emit a `telemetry` event, causing all callback functions to be run in the order they were registered
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/core",
3
- "version": "5.2.9",
3
+ "version": "5.3.0",
4
4
  "description": "Core libraries to interact with SFDX projects, orgs, and APIs.",
5
5
  "main": "lib/exported",
6
6
  "types": "lib/exported.d.ts",
@@ -42,7 +42,7 @@
42
42
  "@salesforce/kit": "^3.0.11",
43
43
  "@salesforce/schemas": "^1.6.0",
44
44
  "@salesforce/ts-types": "^2.0.7",
45
- "@types/semver": "^7.5.1",
45
+ "@types/semver": "^7.5.2",
46
46
  "ajv": "^8.12.0",
47
47
  "change-case": "^4.1.2",
48
48
  "faye": "^1.4.0",
@@ -63,10 +63,10 @@
63
63
  "@salesforce/dev-scripts": "^5.4.2",
64
64
  "@salesforce/prettier-config": "^0.0.3",
65
65
  "@salesforce/ts-sinon": "^1.4.15",
66
- "@types/benchmark": "^2.1.2",
67
- "@types/chai-string": "^1.4.2",
68
- "@types/jsonwebtoken": "9.0.2",
69
- "@types/lodash": "^4.14.197",
66
+ "@types/benchmark": "^2.1.3",
67
+ "@types/chai-string": "^1.4.3",
68
+ "@types/jsonwebtoken": "9.0.3",
69
+ "@types/lodash": "^4.14.199",
70
70
  "@types/proper-lockfile": "^4.1.2",
71
71
  "@types/shelljs": "0.8.12",
72
72
  "@typescript-eslint/eslint-plugin": "^5.62.0",
@@ -74,7 +74,7 @@
74
74
  "benchmark": "^2.1.4",
75
75
  "chai": "^4.3.8",
76
76
  "chai-string": "^1.5.0",
77
- "eslint": "^8.49.0",
77
+ "eslint": "^8.50.0",
78
78
  "eslint-config-prettier": "^8.10.0",
79
79
  "eslint-config-salesforce": "^2.0.2",
80
80
  "eslint-config-salesforce-license": "^0.2.0",
@@ -93,7 +93,7 @@
93
93
  "ts-node": "^10.4.0",
94
94
  "ttypescript": "^1.5.15",
95
95
  "typescript": "^4.9.5",
96
- "wireit": "^0.13.0"
96
+ "wireit": "^0.14.0"
97
97
  },
98
98
  "repository": {
99
99
  "type": "git",