Use hash private
This commit is contained in:
@@ -35,7 +35,7 @@ export class EventEmitter<T extends EventMap> {
|
|||||||
* The listeners map.
|
* The listeners map.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private listeners: Map<keyof T, Set<ListenerEntry<T[keyof T]>>> = new Map();
|
#listeners: Map<keyof T, Set<ListenerEntry<T[keyof T]>>> = new Map();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a listener for an event.
|
* Add a listener for an event.
|
||||||
@@ -49,8 +49,8 @@ export class EventEmitter<T extends EventMap> {
|
|||||||
const wrappedListener = debounceMilliseconds && debounceMilliseconds > 0 ? this.debounce(listener, debounceMilliseconds) : listener;
|
const wrappedListener = debounceMilliseconds && debounceMilliseconds > 0 ? this.debounce(listener, debounceMilliseconds) : listener;
|
||||||
|
|
||||||
// If the listeners map does not have the event type, create a new set.
|
// If the listeners map does not have the event type, create a new set.
|
||||||
if (!this.listeners.has(type)) {
|
if (!this.#listeners.has(type)) {
|
||||||
this.listeners.set(type, new Set());
|
this.#listeners.set(type, new Set());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a listener entry.
|
// Create a listener entry.
|
||||||
@@ -85,8 +85,8 @@ export class EventEmitter<T extends EventMap> {
|
|||||||
debounceMilliseconds && debounceMilliseconds > 0 ? this.debounce(wrappedListener, debounceMilliseconds) : wrappedListener;
|
debounceMilliseconds && debounceMilliseconds > 0 ? this.debounce(wrappedListener, debounceMilliseconds) : wrappedListener;
|
||||||
|
|
||||||
// If the listeners map does not have the event type, create a new set.
|
// If the listeners map does not have the event type, create a new set.
|
||||||
if (!this.listeners.has(type)) {
|
if (!this.#listeners.has(type)) {
|
||||||
this.listeners.set(type, new Set());
|
this.#listeners.set(type, new Set());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a listener entry.
|
// Create a listener entry.
|
||||||
@@ -111,7 +111,7 @@ export class EventEmitter<T extends EventMap> {
|
|||||||
*/
|
*/
|
||||||
off<K extends keyof T>(type: K, listener: Listener<T[K]>): void {
|
off<K extends keyof T>(type: K, listener: Listener<T[K]>): void {
|
||||||
// Get the listeners for the event type.
|
// Get the listeners for the event type.
|
||||||
const listeners = this.listeners.get(type);
|
const listeners = this.#listeners.get(type);
|
||||||
if (!listeners) return;
|
if (!listeners) return;
|
||||||
|
|
||||||
// Find the listener entry.
|
// Find the listener entry.
|
||||||
@@ -131,7 +131,7 @@ export class EventEmitter<T extends EventMap> {
|
|||||||
*/
|
*/
|
||||||
emit<K extends keyof T>(type: K, payload: T[K]): boolean {
|
emit<K extends keyof T>(type: K, payload: T[K]): boolean {
|
||||||
// Get the listeners for the event type.
|
// Get the listeners for the event type.
|
||||||
const listeners = this.listeners.get(type);
|
const listeners = this.#listeners.get(type);
|
||||||
if (!listeners) return false;
|
if (!listeners) return false;
|
||||||
|
|
||||||
// Freeze the payload to make it readonly.
|
// Freeze the payload to make it readonly.
|
||||||
@@ -150,7 +150,7 @@ export class EventEmitter<T extends EventMap> {
|
|||||||
* Remove all listeners.
|
* Remove all listeners.
|
||||||
*/
|
*/
|
||||||
removeAllListeners(): void {
|
removeAllListeners(): void {
|
||||||
this.listeners.clear();
|
this.#listeners.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user