107 lines
6.0 KiB
JavaScript
107 lines
6.0 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
|
|
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
|
|
*
|
|
* All objects in the event handling chain should inherit from this class
|
|
*
|
|
*/
|
|
|
|
var _logger = require('./utils/logger');
|
|
|
|
var _errors = require('./errors');
|
|
|
|
var _events = require('./events');
|
|
|
|
var _events2 = _interopRequireDefault(_events);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
var EventHandler = function () {
|
|
function EventHandler(hls) {
|
|
_classCallCheck(this, EventHandler);
|
|
|
|
this.hls = hls;
|
|
this.onEvent = this.onEvent.bind(this);
|
|
|
|
for (var _len = arguments.length, events = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
events[_key - 1] = arguments[_key];
|
|
}
|
|
|
|
this.handledEvents = events;
|
|
this.useGenericHandler = true;
|
|
|
|
this.registerListeners();
|
|
}
|
|
|
|
_createClass(EventHandler, [{
|
|
key: 'destroy',
|
|
value: function destroy() {
|
|
this.unregisterListeners();
|
|
}
|
|
}, {
|
|
key: 'isEventHandler',
|
|
value: function isEventHandler() {
|
|
return _typeof(this.handledEvents) === 'object' && this.handledEvents.length && typeof this.onEvent === 'function';
|
|
}
|
|
}, {
|
|
key: 'registerListeners',
|
|
value: function registerListeners() {
|
|
if (this.isEventHandler()) {
|
|
this.handledEvents.forEach(function (event) {
|
|
if (event === 'hlsEventGeneric') {
|
|
throw new Error('Forbidden event name: ' + event);
|
|
}
|
|
this.hls.on(event, this.onEvent);
|
|
}, this);
|
|
}
|
|
}
|
|
}, {
|
|
key: 'unregisterListeners',
|
|
value: function unregisterListeners() {
|
|
if (this.isEventHandler()) {
|
|
this.handledEvents.forEach(function (event) {
|
|
this.hls.off(event, this.onEvent);
|
|
}, this);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* arguments: event (string), data (any)
|
|
*/
|
|
|
|
}, {
|
|
key: 'onEvent',
|
|
value: function onEvent(event, data) {
|
|
this.onEventGeneric(event, data);
|
|
}
|
|
}, {
|
|
key: 'onEventGeneric',
|
|
value: function onEventGeneric(event, data) {
|
|
var eventToFunction = function eventToFunction(event, data) {
|
|
var 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) {
|
|
_logger.logger.error('internal error happened while processing ' + event + ':' + err.message);
|
|
this.hls.trigger(_events2.default.ERROR, { type: _errors.ErrorTypes.OTHER_ERROR, details: _errors.ErrorDetails.INTERNAL_EXCEPTION, fatal: false, event: event, err: err });
|
|
}
|
|
}
|
|
}]);
|
|
|
|
return EventHandler;
|
|
}();
|
|
|
|
exports.default = EventHandler; |