懒羊羊
2023-08-30 1ac2bc1590406d9babec036e154d8d08f34a6aa1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* This file is part of the jquery plugin "asyncQueue".
* (c) Sebastien Roch <roch.sebastien@gmail.com>
* @author (parallel) Dmitry Farafonov
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
(function($){
    $.AsyncQueue = function() {
        var that = this,
            queue = [],
            completeFunc,
            failureFunc,
            paused = false,
            lastCallbackData,
            _run,
            _complete,
            inQueue = 0,
            defaultTimeOut = 10;
 
        _run = function() {
            var f = queue.shift();
 
            if (f) {
                inQueue++;
                setTimeout(function(){
                    f.fn.apply(that, [that]);
                
                    if (!f.isParallel)
                        if (paused === false) {
                            _run();
                        }
                    inQueue --;
                    if (inQueue == 0 && queue.length == 0)
                        _complete();
                }, f.timeOut);                
                
                if (f.isParallel)
                    if (paused === false) {
                        _run();
                    }
            }
        };
        
        _complete = function(){
            if (completeFunc)
                    completeFunc.apply(that, [that]);
        };
 
        this.onComplete = function(func) {
            completeFunc = func;
        };
        
        this.onFailure = function(func) {
            failureFunc = func;
        };
 
        this.add = function(func) {
            // TODO: add callback for queue[i] complete
            
            var obj = arguments[0];
            if (obj && Object.prototype.toString.call(obj) === "[object Array]") {
                var fn = arguments[1];
                var timeOut = (typeof(arguments[2]) != "undefined")? arguments[2] : defaultTimeOut;
                if (typeof(fn) == "function") {
                    for(var i = 0; i < obj.length; i++) {
                        var f = function(objx){
                            queue.push({isParallel: true, fn: function(){fn.apply(that, [that, objx]);}, timeOut: timeOut});
                        }(obj[i])
                    }
                }
            } else {
                var fn = arguments[0];
                var timeOut = (typeof(arguments[1]) != "undefined")? arguments[2] : defaultTimeOut;
                queue.push({isParallel: false, fn: func, timeOut: timeOut});
            }
            return this;
        };
        
        this.addParallel = function(func, timeOut) {
            // TODO: add callback for queue[i] complete
            
            queue.push({isParallel: true, fn: func, timeOut: timeOut});
            return this;
        };
 
        this.storeData = function(dataObject) {
            lastCallbackData = dataObject;
            return this;
        };
 
        this.lastCallbackData = function () {
            return lastCallbackData;
        };
 
        this.run = function() {
            paused = false;
            _run();
        };
 
        this.pause = function () {
            paused = true;
            return this;
        };
 
        this.failure = function() {
            paused = true;
            if (failureFunc) {
                var args = [that];
                for(i = 0; i < arguments.length; i++) {
                    args.push(arguments[i]);
                }
                failureFunc.apply(that, args);
            }
        };
        
        this.size = function(){
            return queue.length;
        };
 
        return this;
    }
})(jQuery);