UNPKG

xgplayer-dash

Version:
112 lines (111 loc) 3.02 kB
import { inherits as _inherits, createSuper as _createSuper, createClass as _createClass, classCallCheck as _classCallCheck, possibleConstructorReturn as _possibleConstructorReturn, assertThisInitialized as _assertThisInitialized } from "../_virtual/_rollupPluginBabelHelpers.js"; import EventEmitter from "eventemitter3"; var Task = /* @__PURE__ */ function(_EventEmitter) { _inherits(Task2, _EventEmitter); var _super = _createSuper(Task2); function Task2(url, _callback, range) { var _this; _classCallCheck(this, Task2); _this = _super.call(this); _this.url = url; _this.on = false; if (Task2.queue.some(function(item) { return item.url === url; })) { return _possibleConstructorReturn(_this); } var xhr = new XMLHttpRequest(); xhr.target = _assertThisInitialized(_this); xhr.responseType = "arraybuffer"; xhr.open("get", url); if (range) { _this.range = range; _this.id = range.join("-"); xhr.setRequestHeader("Range", "bytes=".concat(range.join("-"))); } xhr.onload = function() { if (xhr.status === 200 || xhr.status === 206) { if (_callback && _callback instanceof Function) { _callback(xhr.response); } } else if (xhr.status === 404) { if (_callback && _callback instanceof Function) { _callback("Not Found"); } } xhr.target.remove(); }; xhr.onerror = function() { xhr.target.remove(); }; xhr.onabort = function() { xhr.target.remove(); }; _this.xhr = xhr; Task2.queue.push(_assertThisInitialized(_this)); _this.update(); return _this; } _createClass(Task2, [{ key: "cancel", value: function cancel() { this.xhr.abort(); } }, { key: "remove", value: function remove() { var _this2 = this; Task2.queue.filter(function(item, idx) { if (item.url === _this2.url) { Task2.queue.splice(idx, 1); return true; } else { return false; } }); this.update(); } }, { key: "update", value: function update() { var Queue = Task2.queue; var sended = Queue.filter(function(item) { return item.on; }); var wait = Queue.filter(function(item) { return !item.on; }); var max = Task2.limit - sended.length; wait.forEach(function(item, idx) { if (idx < max) { item.run(); } }); } }, { key: "run", value: function run() { if (this.xhr.readyState === 1) { this.on = true; this.xhr.send(); } else { this.remove(); } } }], [{ key: "clear", value: function clear() { Task2.queue.forEach(function(item) { if (item.on) { item.cancel(); } }); Task2.queue.length = 0; } }]); return Task2; }(EventEmitter); Task.queue = []; Task.limit = 2; window.Task = Task; export { Task as default };