47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
class Events {
|
|
constructor () {
|
|
this.events = {};
|
|
|
|
this.audioEvents = [
|
|
'abort', 'canplay', 'canplaythrough', 'durationchange', 'emptied', 'ended', 'error',
|
|
'loadeddata', 'loadedmetadata', 'loadstart', 'mozaudioavailable', 'pause', 'play',
|
|
'playing', 'progress', 'ratechange', 'seeked', 'seeking', 'stalled', 'suspend',
|
|
'timeupdate', 'volumechange', 'waiting'
|
|
];
|
|
this.playerEvents = [
|
|
'destroy', 'switchaudio', 'addaudio', 'removeaudio', 'notice_show', 'notice_hide'
|
|
];
|
|
}
|
|
|
|
on (name, callback) {
|
|
if (this.type(name) && typeof callback === 'function') {
|
|
if (!this.events[name]) {
|
|
this.events[name] = [];
|
|
}
|
|
this.events[name].push(callback);
|
|
}
|
|
}
|
|
|
|
trigger (name, data) {
|
|
if (this.events[name] && this.events[name].length) {
|
|
for (let i = 0; i < this.events[name].length; i++) {
|
|
this.events[name][i](data);
|
|
}
|
|
}
|
|
}
|
|
|
|
type (name) {
|
|
if (this.playerEvents.indexOf(name) !== -1) {
|
|
return 'player';
|
|
}
|
|
else if (this.audioEvents.indexOf(name) !== -1) {
|
|
return 'audio';
|
|
}
|
|
|
|
console.error(`Unknown event name: ${name}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export default Events;
|