playable
Version:
Video player based on HTML5Video
155 lines • 6.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var eventemitter3_1 = require("eventemitter3");
var player_api_decorator_1 = (0, tslib_1.__importDefault)(require("../../core/player-api-decorator"));
var promise_1 = require("../../utils/promise");
var EventEmitterModule = /** @class */ (function (_super) {
(0, tslib_1.__extends)(EventEmitterModule, _super);
function EventEmitterModule() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Attach an event handler function for one or more events
* You can check all events [here](/events)
*
* @param event - The Event name, such as `Playable.UI_EVENTS.PLAY_CLICK`
* @param fn - A function callback to execute when the event is triggered.
* @param context - Value to use as `this` (i.e the reference Object) when executing callback.
*
* @example
* const Playable = require('playable');
* const player = Playable.create();
*
* player.on(Playable.UI_EVENTS.PLAY_CLICK, () => {
* // Will be executed after you will click on play button
* });
*
* // To supply a context value for `this` when the callback is invoked,
* // pass the optional context argument
* player.on(Playable.VIDEO_EVENTS.UPLOAD_STALLED, this.handleStalledUpload, this);
*/
EventEmitterModule.prototype.on = function (event, fn, context) {
return _super.prototype.on.call(this, event, fn, context);
};
/**
* The `.once()` method is identical to `.on()`, except that the handler for a given element and event type is unbound after its first invocation.
*
* @param event - The Event name, such as `Playable.UI_EVENTS.PLAY_CLICK`
* @param fn - A function callback to execute when the event is triggered.
* @param context - Value to use as `this` (i.e the reference Object) when executing callback.
*
* @example
* const Playable = require('playable');
* const player = Playable.create();
*
* player.once(Playable.UI_EVENTS.PLAY_CLICK, () => {
* // Will be executed only one time
* });
*/
EventEmitterModule.prototype.once = function (event, fn, context) {
return _super.prototype.once.call(this, event, fn, context);
};
/**
* Remove an event handler.
*
* @param event - The Event name, such as `Playable.UI_EVENTS.PLAY_CLICK`
* @param fn - Only remove the listeners that match this function.
* @param context - Only remove the listeners that have this context.
* @param once - Only remove one-time listeners.
*
* @example
* const Playable = require('playable');
* const player = Playable.create();
*
* const callback = function() {
* // Code to handle some kind of event
* };
*
* // ... Now callback will be called when some one will pause the video ...
* player.on(Playable.UI_EVENTS.PAUSE, callback);
*
* // ... callback will no longer be called.
* player.off(Playable.UI_EVENTS.PAUSE, callback);
*
* // ... remove all handlers for event UI_EVENTS.PAUSE.
* player.off(Playable.UI_EVENTS.PAUSE);
*/
EventEmitterModule.prototype.off = function (event, fn, context, once) {
return _super.prototype.off.call(this, event, fn, context, once);
};
/**
* Method for binding array of listeners with events inside player.
*
* @example
*
* this._unbindEvents = this._eventEmitter.bindEvents([
* [Playable.VIDEO_EVENTS.STATE_CHANGED, this._processStateChange],
* [Playable.VIDEO_EVENTS.LIVE_STATE_CHANGED, this._processLiveStateChange],
* [Playable.VIDEO_EVENTS.CHUNK_LOADED, this._updateBufferIndicator],
* [Playable.VIDEO_EVENTS.DURATION_UPDATED, this._updateAllIndicators],
* ],
* this,
* );
*
* //...
*
* this._unbindEvents()
*
* @param eventsMap
* @param defaultFnContext
* @returns unbindEvents
*/
EventEmitterModule.prototype.bindEvents = function (eventsMap, defaultFnContext) {
var _this = this;
var events = [];
eventsMap.forEach(function (_a) {
var eventName = _a[0], fn = _a[1], _b = _a[2], fnContext = _b === void 0 ? defaultFnContext : _b;
_this.on(eventName, fn, fnContext);
events.push(function () {
_this.off(eventName, fn, fnContext);
});
});
return function () {
events.forEach(function (unbindEvent) {
unbindEvent();
});
};
};
//Now emit fire events only at the end of current macrotask, as part as next microtask
EventEmitterModule.prototype.emitAsync = function (event) {
var _this = this;
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
//Handle IE11
if (!promise_1.isPromiseAvailable) {
if (setImmediate) {
setImmediate(function () { return _super.prototype.emit.apply(_this, (0, tslib_1.__spreadArray)([event], args, false)); });
}
else {
setTimeout(function () { return _super.prototype.emit.apply(_this, (0, tslib_1.__spreadArray)([event], args, false)); });
}
}
else {
return Promise.resolve().then(function () { return _super.prototype.emit.apply(_this, (0, tslib_1.__spreadArray)([event], args, false)); });
}
};
EventEmitterModule.prototype.destroy = function () {
this.removeAllListeners();
};
EventEmitterModule.moduleName = 'eventEmitter';
(0, tslib_1.__decorate)([
(0, player_api_decorator_1.default)()
], EventEmitterModule.prototype, "on", null);
(0, tslib_1.__decorate)([
(0, player_api_decorator_1.default)()
], EventEmitterModule.prototype, "once", null);
(0, tslib_1.__decorate)([
(0, player_api_decorator_1.default)()
], EventEmitterModule.prototype, "off", null);
return EventEmitterModule;
}(eventemitter3_1.EventEmitter));
exports.default = EventEmitterModule;
//# sourceMappingURL=event-emitter.js.map