add event proxy

This commit is contained in:
Harvey Zack committed 2018-10-13 17:32:33 +08:00
1 parent 11bc295663
commit a395bc4550
7 files changed
+62 -76

No files matched your search

+37 -44
View File
@@ -409,21 +409,12 @@
value: function eventBind() {
var _this = this;
var proxy = this.art.events.proxy;
var $video = this.art.refs.$video;
var events = mediaElement.events;
var _loop = function _loop(index) {
var eventName = events[index];
$video.addEventListener(eventName, _this.eventFn);
_this.art.destroyEvents.push(function () {
$video.removeEventListener(eventName, _this.eventFn);
});
};
for (var index = 0; index < events.length; index++) {
_loop(index);
}
events.forEach(function (eventName) {
proxy($video, eventName, _this.eventFn);
});
}
}, {
key: "eventFn",
@@ -502,7 +493,7 @@
errorHandle(MediaSource.isTypeSupported(option.mimeCodec), "Unsupported MIME type or codec: ".concat(option.mimeCodec));
this.mediaSource = new MediaSource();
$video.src = URL.createObjectURL(this.mediaSource);
this.art.destroyEvents.push(function () {
this.art.events.destroys.push(function () {
URL.revokeObjectURL($video.src);
});
}
@@ -511,27 +502,15 @@
value: function eventBind() {
var _this = this;
var proxy = this.art.events.proxy;
var instance = mseConfig.instance,
sourceBufferList = mseConfig.sourceBufferList;
instance.events.forEach(function (eventName) {
_this.mediaSource.addEventListener(eventName, _this.mediaSourceEventFn);
_this.art.destroyEvents.push(function () {
_this.mediaSource.removeEventListener(eventName, _this.mediaSourceEventFn);
});
proxy(_this.mediaSource, eventName, _this.mediaSourceEventFn);
});
sourceBufferList.events.forEach(function (eventName) {
_this.mediaSource.sourceBuffers.addEventListener(eventName, _this.sourceBuffersEventFn);
_this.art.destroyEvents.push(function () {
_this.mediaSource.sourceBuffers.removeEventListener(eventName, _this.sourceBuffersEventFn);
});
_this.mediaSource.activeSourceBuffers.addEventListener(eventName, _this.activeSourceBuffersEventFn);
_this.art.destroyEvents.push(function () {
_this.mediaSource.activeSourceBuffers.removeEventListener(eventName, _this.activeSourceBuffersEventFn);
});
proxy(_this.mediaSource.sourceBuffers, eventName, _this.sourceBuffersEventFn);
proxy(_this.mediaSource.activeSourceBuffers, eventName, _this.activeSourceBuffersEventFn);
});
}
}, {
@@ -540,15 +519,12 @@
var _this2 = this;
var option = this.art.option;
var proxy = this.art.events.proxy;
var sourceBuffer = mseConfig.sourceBuffer;
this.art.on('mediaSource:sourceopen', function () {
_this2.sourceBuffer = _this2.mediaSource.addSourceBuffer(option.mimeCodec);
sourceBuffer.events.forEach(function (eventName) {
_this2.sourceBuffer.addEventListener(eventName, _this2.sourceBufferEventFn);
_this2.art.destroyEvents.push(function () {
_this2.sourceBuffer.removeEventListener(eventName, _this2.sourceBufferEventFn);
});
proxy(_this2.sourceBuffer, eventName, _this2.sourceBufferEventFn);
});
});
this.art.on('sourceBuffer:updateend', function () {
@@ -614,11 +590,29 @@
this.art = art;
};
var Events = function Events(art) {
classCallCheck(this, Events);
var Events =
/*#__PURE__*/
function () {
function Events(art) {
classCallCheck(this, Events);
this.art = art;
};
this.art = art;
this.destroys = [];
this.proxy = this.proxy.bind(this);
}
createClass(Events, [{
key: "proxy",
value: function proxy(target, name, callback) {
target.addEventListener(name, callback);
this.destroys.push(function () {
target.removeEventListener(name, callback);
});
}
}]);
return Events;
}();
var Hotkey = function Hotkey(art) {
classCallCheck(this, Hotkey);
@@ -660,9 +654,9 @@
this.refs = {
$container: this.option.container
};
this.destroyEvents = [];
this.template = new Template(this);
this.i18n = new I18n(this);
this.events = new Events(this);
this.player = new Player(this);
this.mse = new Mse(this);
this.controls = new Controls(this);
@@ -670,7 +664,6 @@
this.danmaku = new Danmu(this);
this.subtitle = new Subtitle(this);
this.info = new Info(this);
this.events = new Events(this);
this.hotkey = new Hotkey(this);
this.layers = new Layers(this);
this.id = id++;
@@ -679,10 +672,10 @@
}, {
key: "destroy",
value: function destroy() {
this.destroyEvents.forEach(function (event) {
return event();
this.events.destroys.forEach(function (destroy) {
return destroy();
});
this.refs.container.innerHTML = '';
this.refs.$container.innerHTML = '';
instances.splice(instances.indexOf(this), 1);
}
}], [{
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"artplayer.js","sources":["../../node_modules/@babel/runtime/helpers/classCallCheck.js","../../node_modules/@babel/runtime/helpers/createClass.js","../../node_modules/@babel/runtime/helpers/typeof.js","../../node_modules/@babel/runtime/helpers/assertThisInitialized.js","../../node_modules/@babel/runtime/helpers/possibleConstructorReturn.js","../../node_modules/@babel/runtime/helpers/getPrototypeOf.js","../../node_modules/@babel/runtime/helpers/setPrototypeOf.js","../../node_modules/@babel/runtime/helpers/inherits.js","../../node_modules/@babel/runtime/helpers/isNativeFunction.js","../../node_modules/@babel/runtime/helpers/construct.js","../../node_modules/@babel/runtime/helpers/wrapNativeSuper.js","../../src/utils/ArtPlayerError.js","../../src/utils/index.js","../../src/utils/verification.js","../../node_modules/tiny-emitter/index.js","../../src/template.js","../../src/i18n/zh-CN.js","../../src/i18n/zh-TW.js","../../src/i18n/en.js","../../src/i18n/jp.js","../../src/i18n/index.js","../../src/utils/mediaElement.js","../../src/player.js","../../src/utils/mimeCodeces.js","../../src/utils/mseConfig.js","../../src/mse.js","../../src/controls/index.js","../../src/contextmenu.js","../../src/danmaku.js","../../src/info.js","../../src/subtitle.js","../../src/events.js","../../src/hotkey.js","../../src/layers.js","../../src/index.js"],"sourcesContent":["function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}\n\nmodule.exports = _classCallCheck;","function _defineProperties(target, props) {\n for (var i = 0; i < props.length; i++) {\n var descriptor = props[i];\n descriptor.enumerable = descriptor.enumerable || false;\n descriptor.configurable = true;\n if (\"value\" in descriptor) descriptor.writable = true;\n Object.defineProperty(target, descriptor.key, descriptor);\n }\n}\n\nfunction _createClass(Constructor, protoProps, staticProps) {\n if (protoProps) _defineProperties(Constructor.prototype, protoProps);\n if (staticProps) _defineProperties(Constructor, staticProps);\n return Constructor;\n}\n\nmodule.exports = _createClass;","function _typeof2(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof2(obj); }\n\nfunction _typeof(obj) {\n if (typeof Symbol === \"function\" && _typeof2(Symbol.iterator) === \"symbol\") {\n module.exports = _typeof = function _typeof(obj) {\n return _typeof2(obj);\n };\n } else {\n module.exports = _typeof = function _typeof(obj) {\n return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : _typeof2(obj);\n };\n }\n\n return _typeof(obj);\n}\n\nmodule.exports = _typeof;","function _assertThisInitialized(self) {\n if (self === void 0) {\n throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\");\n }\n\n return self;\n}\n\nmodule.exports = _assertThisInitialized;","var _typeof = require(\"../helpers/typeof\");\n\nvar assertThisInitialized = require(\"./assertThisInitialized\");\n\nfunction _possibleConstructorReturn(self, call) {\n if (call && (_typeof(call) === \"object\" || typeof call === \"function\")) {\n return call;\n }\n\n return assertThisInitialized(self);\n}\n\nmodule.exports = _possibleConstructorReturn;","function _getPrototypeOf(o) {\n module.exports = _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {\n return o.__proto__ || Object.getPrototypeOf(o);\n };\n return _getPrototypeOf(o);\n}\n\nmodule.exports = _getPrototypeOf;","function _setPrototypeOf(o, p) {\n module.exports = _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {\n o.__proto__ = p;\n return o;\n };\n\n return _setPrototypeOf(o, p);\n}\n\nmodule.exports = _setPrototypeOf;","var setPrototypeOf = require(\"./setPrototypeOf\");\n\nfunction _inherits(subClass, superClass) {\n if (typeof superClass !== \"function\" && superClass !== null) {\n throw new TypeError(\"Super expression must either be null or a function\");\n }\n\n subClass.prototype = Object.create(superClass && superClass.prototype, {\n constructor: {\n value: subClass,\n writable: true,\n configurable: true\n }\n });\n if (superClass) setPrototypeOf(subClass, superClass);\n}\n\nmodule.exports = _inherits;","function _isNativeFunction(fn) {\n return Function.toString.call(fn).indexOf(\"[native code]\") !== -1;\n}\n\nmodule.exports = _isNativeFunction;","var setPrototypeOf = require(\"./setPrototypeOf\");\n\nfunction isNativeReflectConstruct() {\n if (typeof Reflect === \"undefined\" |Line truncated
{"version":3,"file":"artplayer.js","sources":["../../node_modules/@babel/runtime/helpers/classCallCheck.js","../../node_modules/@babel/runtime/helpers/createClass.js","../../node_modules/@babel/runtime/helpers/typeof.js","../../node_modules/@babel/runtime/helpers/assertThisInitialized.js","../../node_modules/@babel/runtime/helpers/possibleConstructorReturn.js","../../node_modules/@babel/runtime/helpers/getPrototypeOf.js","../../node_modules/@babel/runtime/helpers/setPrototypeOf.js","../../node_modules/@babel/runtime/helpers/inherits.js","../../node_modules/@babel/runtime/helpers/isNativeFunction.js","../../node_modules/@babel/runtime/helpers/construct.js","../../node_modules/@babel/runtime/helpers/wrapNativeSuper.js","../../src/utils/ArtPlayerError.js","../../src/utils/index.js","../../src/utils/verification.js","../../node_modules/tiny-emitter/index.js","../../src/template.js","../../src/i18n/zh-CN.js","../../src/i18n/zh-TW.js","../../src/i18n/en.js","../../src/i18n/jp.js","../../src/i18n/index.js","../../src/utils/mediaElement.js","../../src/player.js","../../src/utils/mimeCodeces.js","../../src/utils/mseConfig.js","../../src/mse.js","../../src/controls/index.js","../../src/contextmenu.js","../../src/danmaku.js","../../src/info.js","../../src/subtitle.js","../../src/events.js","../../src/hotkey.js","../../src/layers.js","../../src/index.js"],"sourcesContent":["function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}\n\nmodule.exports = _classCallCheck;","function _defineProperties(target, props) {\n for (var i = 0; i < props.length; i++) {\n var descriptor = props[i];\n descriptor.enumerable = descriptor.enumerable || false;\n descriptor.configurable = true;\n if (\"value\" in descriptor) descriptor.writable = true;\n Object.defineProperty(target, descriptor.key, descriptor);\n }\n}\n\nfunction _createClass(Constructor, protoProps, staticProps) {\n if (protoProps) _defineProperties(Constructor.prototype, protoProps);\n if (staticProps) _defineProperties(Constructor, staticProps);\n return Constructor;\n}\n\nmodule.exports = _createClass;","function _typeof2(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof2(obj); }\n\nfunction _typeof(obj) {\n if (typeof Symbol === \"function\" && _typeof2(Symbol.iterator) === \"symbol\") {\n module.exports = _typeof = function _typeof(obj) {\n return _typeof2(obj);\n };\n } else {\n module.exports = _typeof = function _typeof(obj) {\n return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : _typeof2(obj);\n };\n }\n\n return _typeof(obj);\n}\n\nmodule.exports = _typeof;","function _assertThisInitialized(self) {\n if (self === void 0) {\n throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\");\n }\n\n return self;\n}\n\nmodule.exports = _assertThisInitialized;","var _typeof = require(\"../helpers/typeof\");\n\nvar assertThisInitialized = require(\"./assertThisInitialized\");\n\nfunction _possibleConstructorReturn(self, call) {\n if (call && (_typeof(call) === \"object\" || typeof call === \"function\")) {\n return call;\n }\n\n return assertThisInitialized(self);\n}\n\nmodule.exports = _possibleConstructorReturn;","function _getPrototypeOf(o) {\n module.exports = _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {\n return o.__proto__ || Object.getPrototypeOf(o);\n };\n return _getPrototypeOf(o);\n}\n\nmodule.exports = _getPrototypeOf;","function _setPrototypeOf(o, p) {\n module.exports = _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {\n o.__proto__ = p;\n return o;\n };\n\n return _setPrototypeOf(o, p);\n}\n\nmodule.exports = _setPrototypeOf;","var setPrototypeOf = require(\"./setPrototypeOf\");\n\nfunction _inherits(subClass, superClass) {\n if (typeof superClass !== \"function\" && superClass !== null) {\n throw new TypeError(\"Super expression must either be null or a function\");\n }\n\n subClass.prototype = Object.create(superClass && superClass.prototype, {\n constructor: {\n value: subClass,\n writable: true,\n configurable: true\n }\n });\n if (superClass) setPrototypeOf(subClass, superClass);\n}\n\nmodule.exports = _inherits;","function _isNativeFunction(fn) {\n return Function.toString.call(fn).indexOf(\"[native code]\") !== -1;\n}\n\nmodule.exports = _isNativeFunction;","var setPrototypeOf = require(\"./setPrototypeOf\");\n\nfunction isNativeReflectConstruct() {\n if (typeof Reflect === \"undefined\" |Line truncated
+9
View File
@@ -1,5 +1,14 @@
export default class Events {
constructor(art) {
this.art = art;
this.destroys = [];
this.proxy = this.proxy.bind(this);
}
proxy(target, name, callback) {
target.addEventListener(name, callback);
this.destroys.push(() => {
target.removeEventListener(name, callback);
});
}
}
+3 -5
View File
@@ -65,10 +65,9 @@ class Artplayer extends Emitter {
$container: this.option.container
};
this.destroyEvents = [];
this.template = new Template(this);
this.i18n = new I18n(this);
this.events = new Events(this);
this.player = new Player(this);
this.mse = new Mse(this);
this.controls = new Controls(this);
@@ -76,7 +75,6 @@ class Artplayer extends Emitter {
this.danmaku = new Danmaku(this);
this.subtitle = new Subtitle(this);
this.info = new Info(this);
this.events = new Events(this);
this.hotkey = new Hotkey(this);
this.layers = new Layers(this);
@@ -85,8 +83,8 @@ class Artplayer extends Emitter {
}
destroy() {
this.destroyEvents.forEach(event => event());
this.refs.container.innerHTML = '';
this.events.destroys.forEach(destroy => destroy());
this.refs.$container.innerHTML = '';
instances.splice(instances.indexOf(this), 1);
}
}
+7 -18
View File
@@ -51,45 +51,34 @@ export default class Mse {
);
this.mediaSource = new MediaSource();
$video.src = URL.createObjectURL(this.mediaSource);
this.art.destroyEvents.push(() => {
this.art.events.destroys.push(() => {
URL.revokeObjectURL($video.src);
});
}
eventBind() {
const { proxy } = this.art.events;
const { instance, sourceBufferList } = mseConfig;
instance.events.forEach(eventName => {
this.mediaSource.addEventListener(eventName, this.mediaSourceEventFn);
this.art.destroyEvents.push(() => {
this.mediaSource.removeEventListener(eventName, this.mediaSourceEventFn);
});
proxy(this.mediaSource, eventName, this.mediaSourceEventFn);
});
sourceBufferList.events.forEach(eventName => {
this.mediaSource.sourceBuffers.addEventListener(eventName, this.sourceBuffersEventFn);
this.art.destroyEvents.push(() => {
this.mediaSource.sourceBuffers.removeEventListener(eventName, this.sourceBuffersEventFn);
});
this.mediaSource.activeSourceBuffers.addEventListener(eventName, this.activeSourceBuffersEventFn);
this.art.destroyEvents.push(() => {
this.mediaSource.activeSourceBuffers.removeEventListener(eventName, this.activeSourceBuffersEventFn);
});
proxy(this.mediaSource.sourceBuffers, eventName, this.sourceBuffersEventFn);
proxy(this.mediaSource.activeSourceBuffers, eventName, this.activeSourceBuffersEventFn);
});
}
eventStart() {
const { option } = this.art;
const { proxy } = this.art.events;
const { sourceBuffer } = mseConfig;
this.art.on('mediaSource:sourceopen', () => {
this.sourceBuffer = this.mediaSource.addSourceBuffer(option.mimeCodec);
sourceBuffer.events.forEach(eventName => {
this.sourceBuffer.addEventListener(eventName, this.sourceBufferEventFn);
this.art.destroyEvents.push(() => {
this.sourceBuffer.removeEventListener(eventName, this.sourceBufferEventFn);
});
proxy(this.sourceBuffer, eventName, this.sourceBufferEventFn);
});
});
+4 -7
View File
@@ -20,15 +20,12 @@ export default class Player {
}
eventBind() {
const { proxy } = this.art.events;
const { $video } = this.art.refs;
const { events } = mediaElement;
for (let index = 0; index < events.length; index++) {
const eventName = events[index];
$video.addEventListener(eventName, this.eventFn);
this.art.destroyEvents.push(() => {
$video.removeEventListener(eventName, this.eventFn);
});
}
events.forEach(eventName => {
proxy($video, eventName, this.eventFn);
});
}
eventFn(event) {
+1 -1
View File
@@ -16,4 +16,4 @@ export function request(url) {
.catch(err => {
throw new ArtPlayerError(err.message);
});
}
}