-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/FrameMuse/react-modal-global …
…into add-more-handlers-for-`ModalPortal`
- Loading branch information
Showing
9 changed files
with
65 additions
and
246 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2023 Valery Zinchenko | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
*/ | ||
|
||
type EventEmitterListener = (...args: never[]) => void | ||
|
||
class EventEmitter<Events extends Record<EventName, EventEmitterListener>, EventName extends keyof Events = keyof Events> { | ||
private callbacks: Partial<Record<keyof never, Set<EventEmitterListener>>> = {} | ||
|
||
public on<Event extends keyof Events>(event: Event, callback: Events[Event]) { | ||
this.callbacks[event] ??= new Set | ||
this.callbacks[event]?.add(callback as EventEmitterListener) | ||
} | ||
public off<Event extends keyof Events>(event: Event, callback: Events[Event]) { | ||
this.callbacks[event]?.delete(callback as EventEmitterListener) | ||
} | ||
public emit<Event extends keyof Events>(event: Event, ...args: Parameters<Events[Event]>) { | ||
const callbacks = this.callbacks[event] | ||
if (callbacks == null) return | ||
|
||
for (const callback of callbacks) callback(...args) | ||
} | ||
} | ||
|
||
export default EventEmitter |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.