构建事件代理

This commit is contained in:
Harvey Zack committed 2018-12-18 10:41:34 +08:00
1 parent f4c2e11b86
commit b7e740ae61
6 files changed
+233 -13

No files matched your search

+120 -7
View File
@@ -276,6 +276,11 @@
}
}
var utils = /*#__PURE__*/Object.freeze({
FlvError: FlvError,
errorHandle: errorHandle
});
var CheckSupport = function CheckSupport() {
classCallCheck(this, CheckSupport);
@@ -287,14 +292,96 @@
errorHandle(typeof window.fetch === 'function', "Unsupported 'fetch' method");
};
var CreatMediaSource = function CreatMediaSource(flv) {
classCallCheck(this, CreatMediaSource);
var EventProxy =
/*#__PURE__*/
function () {
function EventProxy() {
classCallCheck(this, EventProxy);
this.mediaSource = new MediaSource();
this.url = URL.createObjectURL(this.mediaSource);
flv.mediaElement.src = this.url;
this.destroyEvents = [];
this.proxy = this.proxy.bind(this);
}
createClass(EventProxy, [{
key: "proxy",
value: function proxy(target, name, callback) {
var option = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
target.addEventListener(name, callback, option);
this.destroyEvents.push(function () {
target.removeEventListener(name, callback, option);
});
}
}, {
key: "destroy",
value: function destroy() {
this.destroyEvents.forEach(function (event) {
return event();
});
}
}]);
return EventProxy;
}();
var config = {
mediaSource: {
propertys: ['activeSourceBuffers', 'duration', 'readyState', 'sourceBuffers'],
methods: ['addSourceBuffer', 'endOfStream', 'removeSourceBuffer', 'clearLiveSeekableRange', 'setLiveSeekableRange'],
events: ['sourceclose', 'sourceended', 'sourceopen']
},
sourceBuffer: {
propertys: ['mode', 'updating', 'buffered', 'timestampOffset', 'audioTracks', 'videoTracks', 'textTracks', 'appendWindowStart', 'appendWindowEnd', 'trackDefaults'],
methods: ['appendBuffer', 'appendStream', 'abort', 'remove'],
events: ['abort', 'error', 'update', 'updateend', 'updatestart']
},
sourceBufferList: {
propertys: ['length'],
events: ['addsourcebuffer', 'removesourcebuffer']
}
};
var CreatMediaSource =
/*#__PURE__*/
function () {
function CreatMediaSource(flv) {
classCallCheck(this, CreatMediaSource);
this.flv = flv;
this.mediaSource = new MediaSource();
var url = URL.createObjectURL(this.mediaSource);
flv.events.destroyEvents.push(function () {
URL.revokeObjectURL(url);
});
flv.mediaElement.src = url;
}
createClass(CreatMediaSource, [{
key: "eventBind",
value: function eventBind() {
var _this = this;
var proxy = this.flv.events.proxy;
config.mediaSource.events.forEach(function (eventName) {
proxy(_this.mediaSource, eventName, function (event) {
_this.art.emit("mediaSource:".concat(event.type), event);
});
});
config.sourceBufferList.events.forEach(function (eventName) {
proxy(_this.mediaSource.sourceBuffers, eventName, function (event) {
_this.art.emit("sourceBuffers:".concat(event.type), event);
});
proxy(_this.mediaSource.activeSourceBuffers, eventName, function (event) {
_this.art.emit("activeSourceBuffers:".concat(event.type), event);
});
});
}
}]);
return CreatMediaSource;
}();
var id = 0;
var Flv =
/*#__PURE__*/
function (_Emitter) {
@@ -306,28 +393,54 @@
classCallCheck(this, Flv);
_this = possibleConstructorReturn(this, getPrototypeOf(Flv).call(this));
errorHandle(mediaElement instanceof HTMLVideoElement, 'The first parameter is not a video tag element');
errorHandle(typeof url === 'string', 'The second parameter is not a string type');
_this.mediaElement = mediaElement;
_this.url = url;
_this.support = new CheckSupport(assertThisInitialized(assertThisInitialized(_this)));
_this.events = new EventProxy(assertThisInitialized(assertThisInitialized(_this)));
_this.mediaSource = new CreatMediaSource(assertThisInitialized(assertThisInitialized(_this)));
id += 1;
_this.id = id;
Flv.instances.push(assertThisInitialized(assertThisInitialized(_this)));
return _this;
}
createClass(Flv, [{
key: "load",
value: function load() {
console.log(this);
console.log(this.id);
}
}, {
key: "destroy",
value: function destroy() {
console.log(this);
this.events.destroy();
Flv.instances.splice(Flv.instances.indexOf(this), 1);
this.emit('destroy');
}
}], [{
key: "version",
get: function get() {
return '1.0.6';
}
}, {
key: "config",
get: function get() {
return config;
}
}, {
key: "utils",
get: function get() {
return utils;
}
}]);
return Flv;
}(tinyEmitter);
Object.defineProperty(Flv, 'instances', {
value: []
});
window.Flv = Flv;
function artplayerPluginFlv(art) {
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"artplayer-plugin-flv.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","../../packages/artplayer-plugin-flv/node_modules/tiny-emitter/index.js","../../node_modules/@babel/runtime/helpers/isNativeFunction.js","../../node_modules/@babel/runtime/helpers/construct.js","../../node_modules/@babel/runtime/helpers/wrapNativeSuper.js","../../packages/artplayer-plugin-flv/src/flv/utils.js","../../packages/artplayer-plugin-flv/src/flv/checkSupport.js","../../packages/artplayer-plugin-flv/src/flv/creatMediaSource.js","../../packages/artplayer-plugin-flv/src/flv/index.js","../../packages/artplayer-plugin-flv/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 E () {\n // Keep this empty so it's easier to inherit from\n // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)\n}\n\nE.prototype = {\n on: function (name, callback, ctx) {\n var e = this.e || (this.e = {});\n\n (e[name] || (e[name] = [])).push({\n fn: callback,\n ctx: ctx\n });\n\n return this;\n },\n\n once: function (name, callback, ctx) {\n var self = this;\n function listener () {\n self.off(name, listener);\n callback.apply(ctx, arguments);\nLine truncated
{"version":3,"file":"artplayer-plugin-flv.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","../../packages/artplayer-plugin-flv/node_modules/tiny-emitter/index.js","../../node_modules/@babel/runtime/helpers/isNativeFunction.js","../../node_modules/@babel/runtime/helpers/construct.js","../../node_modules/@babel/runtime/helpers/wrapNativeSuper.js","../../packages/artplayer-plugin-flv/src/flv/utils.js","../../packages/artplayer-plugin-flv/src/flv/checkSupport.js","../../packages/artplayer-plugin-flv/src/flv/eventProxy.js","../../packages/artplayer-plugin-flv/src/flv/config.js","../../packages/artplayer-plugin-flv/src/flv/creatMediaSource.js","../../packages/artplayer-plugin-flv/src/flv/index.js","../../packages/artplayer-plugin-flv/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 E () {\n // Keep this empty so it's easier to inherit from\n // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)\n}\n\nE.prototype = {\n on: function (name, callback, ctx) {\n var e = this.e || (this.e = {});\n\n (e[name] || (e[name] = [])).push({\n fn: callback,\n ctx: ctx\n });\n\n return this;\n },\n\n once: function (name, callback, ctx) {\n vaLine truncated
@@ -0,0 +1,33 @@
export default {
mediaSource: {
propertys: ['activeSourceBuffers', 'duration', 'readyState', 'sourceBuffers'],
methods: [
'addSourceBuffer',
'endOfStream',
'removeSourceBuffer',
'clearLiveSeekableRange',
'setLiveSeekableRange',
],
events: ['sourceclose', 'sourceended', 'sourceopen'],
},
sourceBuffer: {
propertys: [
'mode',
'updating',
'buffered',
'timestampOffset',
'audioTracks',
'videoTracks',
'textTracks',
'appendWindowStart',
'appendWindowEnd',
'trackDefaults',
],
methods: ['appendBuffer', 'appendStream', 'abort', 'remove'],
events: ['abort', 'error', 'update', 'updateend', 'updatestart'],
},
sourceBufferList: {
propertys: ['length'],
events: ['addsourcebuffer', 'removesourcebuffer'],
},
};
@@ -1,7 +1,34 @@
import config from './config';
export default class CreatMediaSource {
constructor(flv) {
this.flv = flv;
this.mediaSource = new MediaSource();
this.url = URL.createObjectURL(this.mediaSource);
flv.mediaElement.src = this.url;
const url = URL.createObjectURL(this.mediaSource);
flv.events.destroyEvents.push(() => {
URL.revokeObjectURL(url);
});
flv.mediaElement.src = url;
}
}
eventBind() {
const {
events: { proxy },
} = this.flv;
config.mediaSource.events.forEach(eventName => {
proxy(this.mediaSource, eventName, event => {
this.art.emit(`mediaSource:${event.type}`, event);
});
});
config.sourceBufferList.events.forEach(eventName => {
proxy(this.mediaSource.sourceBuffers, eventName, event => {
this.art.emit(`sourceBuffers:${event.type}`, event);
});
proxy(this.mediaSource.activeSourceBuffers, eventName, event => {
this.art.emit(`activeSourceBuffers:${event.type}`, event);
});
});
}
}
@@ -0,0 +1,17 @@
export default class EventProxy {
constructor() {
this.destroyEvents = [];
this.proxy = this.proxy.bind(this);
}
proxy(target, name, callback, option = {}) {
target.addEventListener(name, callback, option);
this.destroyEvents.push(() => {
target.removeEventListener(name, callback, option);
});
}
destroy() {
this.destroyEvents.forEach(event => event());
}
}
+32 -2
View File
@@ -1,24 +1,54 @@
import Emitter from 'tiny-emitter';
import CheckSupport from './checkSupport';
import EventProxy from './eventProxy';
import CreatMediaSource from './creatMediaSource';
import * as utils from './utils';
import config from './config';
let id = 0;
class Flv extends Emitter {
constructor(mediaElement, url) {
super();
utils.errorHandle(mediaElement instanceof HTMLVideoElement, 'The first parameter is not a video tag element');
utils.errorHandle(typeof url === 'string', 'The second parameter is not a string type');
this.mediaElement = mediaElement;
this.url = url;
this.support = new CheckSupport(this);
this.events = new EventProxy(this);
this.mediaSource = new CreatMediaSource(this);
id += 1;
this.id = id;
Flv.instances.push(this);
}
static get version() {
return '__VERSION__';
}
static get config() {
return config;
}
static get utils() {
return utils;
}
load() {
console.log(this);
console.log(this.id);
}
destroy() {
console.log(this);
this.events.destroy();
Flv.instances.splice(Flv.instances.indexOf(this), 1);
this.emit('destroy');
}
}
Object.defineProperty(Flv, 'instances', {
value: [],
});
window.Flv = Flv;
export default Flv;