LCache缓存类

class LCache{
    constructor(load,exec){
        this.load=load;
        this.exec=exec;
        this.paramsMap={};//记录加载完成的回调
        this.callbackMap={};//记录加载完成的回调
        this.resMap={};//记录url
        this.loadbackMap={};//记录url 加载完成
        this.loadedI=-1;//记录url 加载完成位置
        this.orderI=0;//记录url 当前位置
        this.orderIMap={};//记录num+url=>orderI

        this.numMap={};//记录url
        this.allUrlArr=[];
        this.itemArr={};
    }
    loadAll(arr,callback){
        var the=this;
        var urlArr=[];
        for(var i=0;i<arr.length;i++){
            if(arr[i]){
                urlArr.push(arr[i])
                this.allUrlArr.push(arr[i])
            }
        }
        //缓存回调
        this.paramsMap[this.orderI+urlArr.length]=urlArr;
        this.callbackMap[this.orderI+urlArr.length]=callback;
        console.log('加载资源',urlArr)

        for(var i=this.orderI;i<this.allUrlArr.length;i++){
            this.loadItem(this.allUrlArr[i],i,function (item) {
                the.itemArr[item.orderI]=item;
                if(item.orderI-the.loadedI===1){
                    synchro(item.orderI)
                }
            })
        }
        this.orderI=this.orderI+urlArr.length;
        //同步执行
        function synchro(i){
            if(i<the.allUrlArr.length&&the.itemArr[i]){
                the.loadedI=i;
                the.exec(the.itemArr[i]);
                delete the.itemArr[i];
                if(the.callbackMap[i+1]){
                    var params=the.paramsMap[i+1];
                    the.callbackMap[i+1](params.map(function (url) {
                        return the.resMap[url]
                    }),params);
                    delete the.paramsMap[i+1];
                    delete the.callbackMap[i+1];
                }
                synchro(i + 1);
            }
        }
    }
    loadItem(url,orderI,callback){
        this.numMap[url]=this.numMap[url]||0;
        this.numMap[url]++;
        if(!this.resMap[url]){
            this.loadbackMap[this.numMap[url]+'-'+url]=callback;
            this.orderIMap[this.numMap[url]+'-'+url]=orderI;
            if(this.numMap[url]===1){
                var the=this;
                this.load(url,function (text) {
                    the.resMap[url]=text;
                    for(var i=1;i<the.numMap[url]+1;i++){
                        if(the.loadbackMap[i+'-'+url]){
                            the.loadbackMap[i+'-'+url]({
                                url:url,
                                text:text,
                                orderI:the.orderIMap[i+'-'+url],
                                num:i,
                            });
                            delete the.loadbackMap[i+'-'+url];
                        }
                    }
                })
            }
        }else{
            callback({
                url:url,
                orderI:orderI,
                text:this.resMap[url],
                num:this.numMap[url]
            });
        }
    }

}

function getText(time,callback) {
    setTimeout(function () {
        callback('res'+time)
    },time)
}
function exec(item) {
    console.log(Object.keys(this.resMap).length,this.loadedI,this.orderI)
    console.log('exec',item)
    if(item.num===1){

    }

}
const aLCache=new LCache(getText,exec)

aLCache.loadAll([200,100],function (resMap) {
    console.log('callback 1',resMap)
})
aLCache.loadAll([20,10],function (resMap) {
    console.log('callback 2')
})
aLCache.loadAll([200,100],function (resMap) {
    console.log('callback 3')
})
aLCache.loadAll([20,10],function (resMap) {
    console.log('callback 4')
})

猜你喜欢

转载自www.cnblogs.com/caoke/p/12533850.html