-
-
Notifications
You must be signed in to change notification settings - Fork 191
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
feat: Animation improvements fromSpriteSheetCoordinates + EventEmitter #2666
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
export type EventMap = Record<string, any>; | ||
export type EventKey<T extends EventMap> = string & keyof T; | ||
export type Handler<EventType> = (event: EventType) => void; | ||
|
||
/** | ||
* Interface that represents a handle to a subscription that can be closed | ||
*/ | ||
export interface Subscription { | ||
close(): void; | ||
} | ||
|
||
/** | ||
* Excalibur's typed event emitter, this allows events to be sent with any string to Type mapping | ||
*/ | ||
export class EventEmitter<T extends EventMap> { | ||
private _paused = false; | ||
private _listeners: Record<string, Handler<any>[]> = {}; | ||
private _listenersOnce: Record<string, Handler<any>[]> = {}; | ||
private _pipes: EventEmitter<any>[] = []; | ||
|
||
on<K extends EventKey<T>>(eventName: K, fn: Handler<T[K]>): Subscription; | ||
on(eventName: string, fn: Handler<unknown>): Subscription; | ||
on<K extends EventKey<T> | string>(eventName: K, fn: Handler<T[K]>): Subscription { | ||
this._listeners[eventName] = this._listeners[eventName] ?? []; | ||
this._listeners[eventName].push(fn); | ||
return { | ||
close: () => this.off(eventName, fn) | ||
}; | ||
} | ||
|
||
once<K extends EventKey<T>>(eventName: K, fn: Handler<T[K]>): Subscription; | ||
once(eventName: string, fn: Handler<unknown>): Subscription; | ||
once<K extends EventKey<T> | string>(eventName: K, fn: Handler<T[K]>): Subscription { | ||
this._listenersOnce[eventName] = this._listenersOnce[eventName] ?? []; | ||
this._listenersOnce[eventName].push(fn); | ||
return { | ||
close: () => this.off(eventName, fn) | ||
}; | ||
} | ||
|
||
off<K extends EventKey<T>>(eventName: K, fn: Handler<T[K]>): void; | ||
off(eventName: string, fn: Handler<unknown>): void; | ||
off(eventName: string): void; | ||
off<K extends EventKey<T> | string>(eventName: K, fn?: Handler<T[K]>): void { | ||
if (fn) { | ||
const listenerIndex = this._listeners[eventName]?.indexOf(fn); | ||
if (listenerIndex > -1) { | ||
this._listeners[eventName]?.splice(listenerIndex, 1); | ||
} | ||
const onceIndex = this._listenersOnce[eventName]?.indexOf(fn); | ||
if (onceIndex > -1) { | ||
this._listenersOnce[eventName]?.splice(onceIndex, 1); | ||
} | ||
} else { | ||
delete this._listeners[eventName]; | ||
} | ||
} | ||
|
||
emit<K extends EventKey<T>>(eventName: K, event: T[K]): void; | ||
emit(eventName: string, event?: any): void; | ||
emit<K extends EventKey<T> | string>(eventName: K, event?: T[K]): void { | ||
if (this._paused) { | ||
return; | ||
} | ||
this._listeners[eventName]?.forEach((fn) => fn(event)); | ||
const onces = this._listenersOnce[eventName]; | ||
this._listenersOnce[eventName] = []; | ||
if (onces) { | ||
onces.forEach((fn) => fn(event)); | ||
} | ||
this._pipes.forEach((pipe) => { | ||
pipe.emit(eventName, event); | ||
}); | ||
} | ||
|
||
pipe(emitter: EventEmitter<any>): Subscription { | ||
if (this === emitter) { | ||
throw Error('Cannot pipe to self'); | ||
} | ||
this._pipes.push(emitter); | ||
return { | ||
close: () => { | ||
let i = -1; | ||
if ((i = this._pipes.indexOf(emitter)) > -1) { | ||
this._pipes.splice(i, 1); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
unpipe(emitter: EventEmitter<any>): void { | ||
let i = -1; | ||
if ((i = this._pipes.indexOf(emitter)) > -1) { | ||
this._pipes.splice(i, 1); | ||
} | ||
} | ||
|
||
pause(): void { | ||
this._paused = true; | ||
} | ||
|
||
unpause(): void { | ||
this._paused = false; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @eonarheim! I saw this code some days ago... And I was a bit curious of why this way 😅
could it be rewritten in this way or there's something I don't see?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tonivj5 good catch, I like yours better! (I'd support a PR 😄)
I'm not sure why I did that, normally I'd do what you suggested (I originally wrote this code a while ago and was holding on to the snippet)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here we go #2707 😄