-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergey Krasnov
committed
Mar 18, 2024
1 parent
6eb8d12
commit f403b72
Showing
3 changed files
with
55 additions
and
15 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
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,31 +1,69 @@ | ||
export type EventNotifierListener<TData> = (data: TData extends object ? TData : object) => void; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type EventNotifierListener<TData = any> = (data: TData extends object ? TData : object) => void | Promise<void>; | ||
|
||
export type NotifyType<TType> = { type: TType }; | ||
|
||
export type NotifyListenerOptions = { | ||
readonly once?: boolean; | ||
}; | ||
|
||
export class EventNotifier<TEventNotifierData extends Record<string, object | undefined>> { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
protected listenerMap = new Map<keyof TEventNotifierData, Set<EventNotifierListener<any>>>(); | ||
protected listenerMap = new Map< | ||
keyof TEventNotifierData, | ||
Map<EventNotifierListener, NotifyListenerOptions | undefined> | ||
>(); | ||
|
||
public emit<TType extends keyof TEventNotifierData>( | ||
event: TEventNotifierData[TType] extends object ? NotifyType<TType> & TEventNotifierData[TType] : NotifyType<TType>, | ||
): void { | ||
const { type, ...data } = event; | ||
this.listenerMap.get(type)?.forEach((options, listener, map) => { | ||
if (options?.once) map.delete(listener); | ||
void listener(data); | ||
}); | ||
} | ||
|
||
public on<TType extends keyof TEventNotifierData>( | ||
type: TType, | ||
listener: EventNotifierListener<TEventNotifierData[TType]>, | ||
options?: NotifyListenerOptions, | ||
): void { | ||
const listeners = this.listenerMap.get(type) ?? new Set(); | ||
const listeners = this.listenerMap.get(type) ?? new Map<EventNotifierListener, NotifyListenerOptions>(); | ||
listeners.set(listener, options); | ||
this.listenerMap.set(type, listeners); | ||
listeners.add(listener); | ||
} | ||
|
||
public off<TType extends keyof TEventNotifierData>( | ||
type: TType, | ||
listener: EventNotifierListener<TEventNotifierData[TType]>, | ||
): void { | ||
this.listenerMap.get(type)?.delete(listener); | ||
this.listenerMap.get(type)?.forEach((_, currentListener, map) => { | ||
if (currentListener !== listener) return; | ||
map.delete(listener); | ||
}); | ||
} | ||
|
||
public notify<TType extends keyof TEventNotifierData>( | ||
event: TEventNotifierData[TType] extends object ? NotifyType<TType> & TEventNotifierData[TType] : NotifyType<TType>, | ||
): void { | ||
const { type, ...data } = event; | ||
this.listenerMap.get(type)?.forEach((listener) => listener(data)); | ||
public once<TType extends keyof TEventNotifierData>( | ||
type: TType, | ||
listener: EventNotifierListener<TEventNotifierData[TType]>, | ||
options?: Omit<NotifyListenerOptions, 'once'>, | ||
) { | ||
this.on(type, listener, { ...options, once: true }); | ||
} | ||
|
||
public has<TType extends keyof TEventNotifierData>( | ||
type: TType, | ||
listener: EventNotifierListener<TEventNotifierData[TType]>, | ||
): boolean { | ||
const listeners = this.listenerMap.get(type); | ||
return !!listeners && listeners.has(listener); | ||
} | ||
|
||
public delete<TType extends keyof TEventNotifierData>(type: TType): void { | ||
this.listenerMap.delete(type); | ||
} | ||
|
||
public clear(): void { | ||
this.listenerMap.clear(); | ||
} | ||
} |