-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: restructure events library
- Loading branch information
1 parent
195162f
commit 5eb2f5e
Showing
4 changed files
with
55 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,71 @@ | ||
type Events = 'allIconsLoaded'; | ||
import type { EventType, EventMap, AnyEvent } from './events'; | ||
|
||
export interface Event { | ||
type: Events; | ||
export interface Event<P = unknown> { | ||
payload?: P; | ||
} | ||
|
||
type EventListener<T extends Event> = (event: T) => void; | ||
type EventListener<E extends AnyEvent> = (event: E) => void; | ||
|
||
interface ListenerEntry<T extends Event> { | ||
listener: EventListener<T>; | ||
interface ListenerEntry<E extends AnyEvent> { | ||
listener: EventListener<E>; | ||
once: boolean; | ||
priority: number; | ||
} | ||
|
||
export class EventEmitter { | ||
private listeners: { [key: string]: ListenerEntry<Event>[] } = {}; | ||
private listeners: { [K in EventType]?: ListenerEntry<AnyEvent>[] } = {}; | ||
|
||
on<T extends Event>( | ||
type: T['type'], | ||
listener: EventListener<T>, | ||
priority: number = 0, | ||
on<K extends EventType>( | ||
type: K, | ||
listener: EventListener<EventMap[K]>, | ||
priority = 0, | ||
): void { | ||
this.listeners[type] ??= []; | ||
this.listeners[type].push({ listener, once: false, priority }); | ||
this.listeners[type]?.push({ listener, once: false, priority }); | ||
this.sortListeners(type); | ||
} | ||
|
||
once<T extends Event>( | ||
type: T['type'], | ||
listener: EventListener<T>, | ||
priority: number = 0, | ||
once<K extends EventType>( | ||
type: K, | ||
listener: EventListener<EventMap[K]>, | ||
priority = 0, | ||
): void { | ||
this.listeners[type] ??= []; | ||
this.listeners[type].push({ listener, once: true, priority }); | ||
this.listeners[type]?.push({ listener, once: true, priority }); | ||
this.sortListeners(type); | ||
} | ||
|
||
off<T extends Event>(type: T['type'], listener: EventListener<T>): void { | ||
off<K extends EventType>( | ||
type: K, | ||
listener: EventListener<EventMap[K]>, | ||
): void { | ||
if (!this.listeners[type]) { | ||
return; | ||
} | ||
|
||
this.listeners[type] = this.listeners[type].filter( | ||
this.listeners[type] = this.listeners[type]?.filter( | ||
(entry) => entry.listener !== listener, | ||
); | ||
} | ||
|
||
emit<T extends Event>(event: T): void { | ||
if (!this.listeners[event.type]) { | ||
emit<K extends EventType>(type: K, payload?: EventMap[K]['payload']): void { | ||
const listeners = this.listeners[type]; | ||
if (!listeners) { | ||
return; | ||
} | ||
|
||
this.listeners[event.type].forEach((entry) => { | ||
const event = { payload } as EventMap[K]; | ||
listeners.slice().forEach((entry) => { | ||
entry.listener(event); | ||
if (entry.once) { | ||
this.off(event.type, entry.listener); | ||
this.off(type, entry.listener); | ||
} | ||
}); | ||
} | ||
|
||
private sortListeners(type: string): void { | ||
private sortListeners(type: EventType): void { | ||
if (this.listeners[type]) { | ||
this.listeners[type].sort((a, b) => b.priority - a.priority); | ||
this.listeners[type]?.sort((a, b) => b.priority - a.priority); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { Event } from './event'; | ||
|
||
export interface AllIconsLoadedEvent extends Event { | ||
type: 'allIconsLoaded'; | ||
} | ||
export type AllIconsLoadedEvent = Event<undefined>; | ||
|
||
export type EventMap = { | ||
allIconsLoaded: AllIconsLoadedEvent; | ||
}; | ||
|
||
export type EventType = keyof EventMap; | ||
export type AnyEvent = EventMap[EventType]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters