-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
73 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
export default class Evented { | ||
|
||
private _eventCallbacks: { | ||
end: Function[]; | ||
begin: Function[]; | ||
} = { | ||
end: [], | ||
begin: [] | ||
}; | ||
|
||
public on(eventName, callback) { | ||
if (typeof callback !== 'function') { | ||
throw new TypeError(`Callback must be a function`); | ||
} | ||
let callbacks = this._eventCallbacks[eventName]; | ||
if (callbacks !== undefined) { | ||
callbacks.push(callback); | ||
} else { | ||
throw new TypeError(`Cannot on() event ${eventName} because it does not exist`); | ||
} | ||
} | ||
|
||
public off(eventName, callback) { | ||
let callbacks = this._eventCallbacks[eventName]; | ||
if (!eventName || callbacks === undefined) { | ||
throw new TypeError(`Cannot off() event ${eventName} because it does not exist`); | ||
} | ||
let callbackFound = false; | ||
if (callback) { | ||
for (let i = 0; i < callbacks.length; i++) { | ||
if (callbacks[i] === callback) { | ||
callbackFound = true; | ||
callbacks.splice(i, 1); | ||
i--; | ||
} | ||
} | ||
} | ||
if (!callbackFound) { | ||
throw new TypeError(`Cannot off() callback that does not exist`); | ||
} | ||
} | ||
|
||
/** | ||
Trigger an event. Supports up to two arguments. Designed around | ||
triggering transition events from one run loop instance to the | ||
next, which requires an argument for the first instance and then | ||
an argument for the next instance. | ||
@protected | ||
@method _trigger | ||
@param {String} eventName | ||
@param {any} arg1 | ||
@param {any} arg2 | ||
*/ | ||
protected _trigger<T, U>(eventName: string, arg1: T, arg2: U) { | ||
let callbacks = this._eventCallbacks[eventName]; | ||
if (callbacks !== undefined) { | ||
for (let i = 0; i < callbacks.length; i++) { | ||
callbacks[i](arg1, arg2); | ||
} | ||
} | ||
} | ||
|
||
} |
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