-
-
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 pull request #233 from FrameMuse/228-replace-eventemitter-with-…
…custom-implementation Replace `eventemitter` with custom implementation
- Loading branch information
Showing
5 changed files
with
64 additions
and
154 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -18,7 +18,6 @@ | |
} | ||
}, | ||
"dependencies": { | ||
"eventemitter3": "^5.0.0", | ||
"type-fest": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
|
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