2020-01-20 12:22:22 +03:00
|
|
|
/*
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
|
|
|
If not, see http://www.gnu.org/licenses.
|
|
|
|
|
|
|
|
For hls.js Copyright notice and license, see LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 11:06:52 +03:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* All objects in the event handling chain should inherit from this class
|
|
|
|
*
|
|
|
|
*/
|
2020-01-20 08:12:22 +03:00
|
|
|
import Event from './events.js';
|
2020-01-02 11:06:52 +03:00
|
|
|
|
|
|
|
const FORBIDDEN_EVENT_NAMES = {
|
|
|
|
'hlsEventGeneric': true,
|
|
|
|
'hlsHandlerDestroying': true,
|
|
|
|
'hlsHandlerDestroyed': true
|
|
|
|
};
|
|
|
|
|
|
|
|
class EventHandler {
|
2020-01-20 08:12:22 +03:00
|
|
|
constructor(hls, ...events) {
|
2020-01-02 11:06:52 +03:00
|
|
|
this.hls = hls;
|
|
|
|
this.onEvent = this.onEvent.bind(this);
|
|
|
|
this.handledEvents = events;
|
|
|
|
this.useGenericHandler = true;
|
|
|
|
|
|
|
|
this.registerListeners();
|
|
|
|
}
|
|
|
|
|
2020-01-20 08:12:22 +03:00
|
|
|
destroy() {
|
2020-01-02 11:06:52 +03:00
|
|
|
this.onHandlerDestroying();
|
|
|
|
this.unregisterListeners();
|
|
|
|
this.onHandlerDestroyed();
|
|
|
|
}
|
|
|
|
|
2020-01-20 08:12:22 +03:00
|
|
|
onHandlerDestroying() { }
|
|
|
|
onHandlerDestroyed() { }
|
2020-01-02 11:06:52 +03:00
|
|
|
|
2020-01-20 08:12:22 +03:00
|
|
|
isEventHandler() {
|
2020-01-02 11:06:52 +03:00
|
|
|
return typeof this.handledEvents === 'object' && this.handledEvents.length && typeof this.onEvent === 'function';
|
|
|
|
}
|
|
|
|
|
2020-01-20 08:12:22 +03:00
|
|
|
registerListeners() {
|
2020-01-02 11:06:52 +03:00
|
|
|
if (this.isEventHandler()) {
|
|
|
|
this.handledEvents.forEach(function (event) {
|
|
|
|
if (FORBIDDEN_EVENT_NAMES[event]) {
|
|
|
|
throw new Error('Forbidden event-name: ' + event);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hls.on(event, this.onEvent);
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 08:12:22 +03:00
|
|
|
unregisterListeners() {
|
2020-01-02 11:06:52 +03:00
|
|
|
if (this.isEventHandler()) {
|
|
|
|
this.handledEvents.forEach(function (event) {
|
|
|
|
this.hls.off(event, this.onEvent);
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* arguments: event (string), data (any)
|
|
|
|
*/
|
2020-01-20 08:12:22 +03:00
|
|
|
onEvent(event, data) {
|
2020-01-02 11:06:52 +03:00
|
|
|
this.onEventGeneric(event, data);
|
|
|
|
}
|
|
|
|
|
2020-01-20 08:12:22 +03:00
|
|
|
onEventGeneric(event, data) {
|
|
|
|
let eventToFunction = function (event, data) {
|
2020-01-02 11:06:52 +03:00
|
|
|
let funcName = 'on' + event.replace('hls', '');
|
|
|
|
if (typeof this[funcName] !== 'function') {
|
|
|
|
throw new Error(`Event ${event} has no generic handler in this ${this.constructor.name} class (tried ${funcName})`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this[funcName].bind(this, data);
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
eventToFunction.call(this, event, data).call();
|
|
|
|
} catch (err) {
|
2020-01-20 08:12:22 +03:00
|
|
|
console.error(`An internal error happened while handling event ${event}. Error message: "${err.message}". Here is a stacktrace:`, err);
|
2020-01-02 11:06:52 +03:00
|
|
|
this.hls.trigger(Event.ERROR, { type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: false, event: event, err: err });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EventHandler;
|