Skip to content

Commit

Permalink
Improve EventTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar committed Dec 27, 2021
1 parent a81a834 commit b4e24ab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 50 deletions.
92 changes: 45 additions & 47 deletions src/utils/EventTarget.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,72 @@
type EventType =
| 'addBody'
| 'beginContact'
| 'beginShapeContact'
| 'collide'
| 'endContact'
| 'endShapeContact'
| 'removeBody'
| 'postStep'
| 'preStep'
| 'sleep'
| 'sleepy'
| 'wakeup'

type CannonEvent = { type: EventType; target?: EventTarget }
type EventHandler = (event: CannonEvent) => void

/**
* Base class for objects that dispatches events.
*/
export class EventTarget {
private _listeners: Record<string, Function[]> | undefined
private _listeners: Record<EventType, EventHandler[]> = {
addBody: [],
beginContact: [],
beginShapeContact: [],
collide: [],
endContact: [],
endShapeContact: [],
removeBody: [],
postStep: [],
preStep: [],
sleep: [],
sleepy: [],
wakeup: [],
}

/**
* Add an event listener
* @return The self object, for chainability.
*/
addEventListener(type: string, listener: Function): EventTarget {
if (this._listeners === undefined) {
this._listeners = {}
}
const listeners = this._listeners
if (listeners[type] === undefined) {
listeners[type] = []
}
if (!listeners[type].includes(listener)) {
listeners[type].push(listener)
addEventListener(type: EventType, listener: EventHandler): EventTarget {
if (!this._listeners[type].includes(listener)) {
this._listeners[type].push(listener)
}
return this
}

/**
* Check if an event listener is added
*/
hasEventListener(type: string, listener: Function): boolean {
if (this._listeners === undefined) {
return false
}
const listeners = this._listeners
if (listeners[type] !== undefined && listeners[type].includes(listener)) {
return true
}
return false
hasEventListener(type: EventType, listener: EventHandler): boolean {
return this._listeners[type].includes(listener)
}

/**
* Check if any event listener of the given type is added
*/
hasAnyEventListener(type: string): boolean {
if (this._listeners === undefined) {
return false
}
const listeners = this._listeners
return listeners[type] !== undefined
hasAnyEventListener(type: EventType): boolean {
return !!this._listeners[type].length
}

/**
* Remove an event listener
* @return The self object, for chainability.
*/
removeEventListener(type: string, listener: Function): EventTarget {
if (this._listeners === undefined) {
return this
}
const listeners = this._listeners
if (listeners[type] === undefined) {
return this
}
const index = listeners[type].indexOf(listener)
removeEventListener(type: EventType, listener: EventHandler): EventTarget {
const index = this._listeners[type].indexOf(listener)
if (index !== -1) {
listeners[type].splice(index, 1)
this._listeners[type].splice(index, 1)
}
return this
}
Expand All @@ -70,17 +75,10 @@ export class EventTarget {
* Emit an event.
* @return The self object, for chainability.
*/
dispatchEvent(event: any): EventTarget {
if (this._listeners === undefined) {
return this
}
const listeners = this._listeners
const listenerArray = listeners[event.type]
if (listenerArray !== undefined) {
event.target = this
for (let i = 0, l = listenerArray.length; i < l; i++) {
listenerArray[i].call(this, event)
}
dispatchEvent(event: CannonEvent): EventTarget {
event.target = this
for (let i = 0; i < this._listeners[event.type].length; i += 1) {
this._listeners[event.type][i].call(this, event)
}
return this
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Utils {
* @return The modified options object.
*/
static defaults(options: Record<string, any> = {}, defaults: Record<string, any>): Record<string, any> {
for (let key in defaults) {
for (const key in defaults) {
if (!(key in options)) {
options[key] = defaults[key]
}
Expand Down
4 changes: 2 additions & 2 deletions src/world/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,10 +827,10 @@ if (!performance.now) {

// Dispatched after the world has stepped forward in time.
// Reusable event objects to save memory.
const World_step_postStepEvent = { type: 'postStep' }
const World_step_postStepEvent = { type: 'postStep' as const }

// Dispatched before the world steps forward in time.
const World_step_preStepEvent = { type: 'preStep' }
const World_step_preStepEvent = { type: 'preStep' as const }

type CollideEvent = {
type: typeof Body.COLLIDE_EVENT_NAME
Expand Down

0 comments on commit b4e24ab

Please sign in to comment.