Hilo/build/commonjs/loader/LoadQueue.min.js
2022-06-29 10:45:02 +08:00

6 lines
2.1 KiB
JavaScript

/**
* hilojs 2.0.3 for commonjs
* Copyright 2016 alibaba.com
* Licensed under the MIT License
*/
function getExtension(e){var r,n,o=/\/?[^\/]+?\.(\w+)\??(\?\S+)?$/i;return(r=e.match(o))&&(n=r[1].toLowerCase()),n||null}var Class=require("../core/Class"),EventMixin=require("../event/EventMixin"),ImageLoader=require("./ImageLoader"),ScriptLoader=require("./ScriptLoader"),LoadQueue=Class.create({Mixes:EventMixin,constructor:function(e){this._source=[],this.add(e)},maxConnections:2,_source:null,_loaded:0,_connections:0,_currentIndex:-1,add:function(e){var r=this;return e&&(e=e instanceof Array?e:[e],r._source=r._source.concat(e)),r},get:function(e){if(e)for(var r=this._source,n=0;n<r.length;n++){var o=r[n];if(o.id===e||o.src===e)return o}return null},getContent:function(e){var r=this.get(e);return r&&r.content},removeContent:function(e){if(e)for(var r=this._source,n=0;n<r.length;n++){var o=r[n];if(o.id===e||o.src===e)return void r.splice(n,1)}},start:function(){var e=this;return e._loadNext(),e},_loadNext:function(){var e=this,r=e._source,n=r.length;if(e._loaded>=n)return void e.fire("complete");if(e._currentIndex<n-1&&e._connections<e.maxConnections){var o=++e._currentIndex,t=r[o],a=e._getLoader(t);if(a){var i=a.onLoad,c=a.onError;a.onLoad=function(r){a.onLoad=i,a.onError=c;var n=i&&i.call(a,r)||r.target;e._onItemLoad(o,n)},a.onError=function(r){a.onLoad=i,a.onError=c,c&&c.call(a,r),e._onItemError(o,r)},e._connections++}e._loadNext(),a&&a.load(t)}},_getLoader:function(e){var r=e.loader;if(r)return r;var n=e.type||getExtension(e.src);switch(n){case"png":case"jpg":case"jpeg":case"gif":case"webp":r=new ImageLoader;break;case"js":case"jsonp":r=new ScriptLoader}return r},_onItemLoad:function(e,r){var n=this,o=n._source[e];o.loaded=!0,o.content=r,n._connections--,n._loaded++,n.fire("load",o),n._loadNext()},_onItemError:function(e,r){var n=this,o=n._source[e];o.error=r,n._connections--,n._loaded++,n.fire("error",o),n._loadNext()},getSize:function(e){for(var r=0,n=this._source,o=0;o<n.length;o++){var t=n[o];r+=(e?t.loaded&&t.size:t.size)||0}return r},getLoaded:function(){return this._loaded},getTotal:function(){return this._source.length}});module.exports=LoadQueue;