Skip to content

Commit

Permalink
chore: docs / rename listeners to unlisteners
Browse files Browse the repository at this point in the history
  • Loading branch information
braebo committed May 16, 2024
1 parent 9906a6c commit 2e05292
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/lib/utils/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export type EventCallback<T = any> = (...args: T[]) => void
* Represents an event manager that provides methods for adding and removing event listeners.
*/
export class EventManager<EventMap extends Record<string, any>> {
private _listeners = new Map<string, EventCallback>()
private _unlisteners = new Map<string, EventCallback>()
/**
* The event handlers for each registered custom event type, and their respective callbacks.
*/
private _handlers = new Map<
keyof EventMap,
Map<string, EventCallback<EventMap[keyof EventMap]>>
Expand Down Expand Up @@ -92,7 +95,7 @@ export class EventManager<EventMap extends Record<string, any>> {
const id = nanoid()
element.removeEventListener(event, callback as EventCallback, options)
element.addEventListener(event, callback as EventCallback, options)
this._listeners.set(id, () => {
this._unlisteners.set(id, () => {
element.removeEventListener(event, callback as EventCallback, options)
})

Expand All @@ -110,7 +113,7 @@ export class EventManager<EventMap extends Record<string, any>> {
*/
add = (cb: () => void, groupId?: string) => {
const id = nanoid()
this._listeners.set(id, cb)
this._unlisteners.set(id, cb)

if (groupId) this.group(groupId, id)

Expand All @@ -136,16 +139,16 @@ export class EventManager<EventMap extends Record<string, any>> {
* @returns `true` if the listener was removed, `false` if it was not found.
*/
unlisten(id: string): boolean {
this._listeners.get(id)?.()
return this._listeners.delete(id)
this._unlisteners.get(id)?.()
return this._unlisteners.delete(id)
}

/**
* Remove all registered listeners and clears the event manager.
* Calls all cleanup callbacks and clears the event manager.
*/
clear() {
for (const cb of this._listeners.values()) cb()
this._listeners.clear()
for (const cb of this._unlisteners.values()) cb()
this._unlisteners.clear()
this._listenerGroups.clear()
this.clearHandlers()
return this
Expand All @@ -168,9 +171,9 @@ export class EventManager<EventMap extends Record<string, any>> {
const group = this._listenerGroups.get(groupId)
if (group) {
for (const id of group) {
const cb = this._listeners.get(id)
const cb = this._unlisteners.get(id)
if (cb) cb()
this._listeners.delete(id)
this._unlisteners.delete(id)
}
this._listenerGroups.delete(groupId)
}
Expand Down

0 comments on commit 2e05292

Please sign in to comment.