Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix event dispatcher types #392

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/controls/EventDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ export class EventDispatcher<TEventMap extends {} = {}> {
* @param type The type of event to listen to.
* @param listener The function that gets called when the event is fired.
*/
addEventListener(type: string, listener: EventListener<{}, string, this>): void
addEventListener<T extends Extract<keyof TEventMap, string>>(
type: T,
listener: EventListener<TEventMap[T], T, this>,
): void {
): void
addEventListener<T extends Extract<keyof TEventMap, string>>(
type: T | string,
listener: EventListener<TEventMap[T], T, this> | EventListener<{}, string, this>,
): void
{

if ( this._listeners === undefined ) this._listeners = {};

Expand All @@ -64,9 +70,14 @@ export class EventDispatcher<TEventMap extends {} = {}> {
* @param type The type of event to listen to.
* @param listener The function that gets called when the event is fired.
*/
hasEventListener(type: string, listener: EventListener<{}, string, this>): boolean;
hasEventListener<T extends Extract<keyof TEventMap, string>>(
type: T,
listener: EventListener<TEventMap[T], T, this>,
): boolean;
hasEventListener<T extends Extract<keyof TEventMap, string>>(
type: T | string,
listener: EventListener<TEventMap[T], T, this> | EventListener<{}, string, this>,
): boolean {

if ( this._listeners === undefined ) return false;
Expand All @@ -82,9 +93,14 @@ export class EventDispatcher<TEventMap extends {} = {}> {
* @param type The type of the listener that gets removed.
* @param listener The listener function that gets removed.
*/
removeEventListener(type: string, listener: EventListener<{}, string, this>): void;
removeEventListener<T extends Extract<keyof TEventMap, string>>(
type: T,
listener: EventListener<TEventMap[T], T, this>,
): void
removeEventListener<T extends Extract<keyof TEventMap, string>>(
type: T|string,
listener: EventListener<TEventMap[T], T, this> | EventListener<{}, string, this>,
): void {

if ( this._listeners === undefined ) return;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export * from './math/ColorConverter'
export * from './math/ImprovedNoise'
export * from './math/Octree'
export * from './math/Lut'
export * from './controls/EventDispatcher'
export * from './controls/experimental/CameraControls'
export * from './controls/FirstPersonControls'
export * from './controls/TransformControls'
Expand Down
Loading