-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_queue.js
191 lines (171 loc) · 3.04 KB
/
job_queue.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* 工作 Queue (可以將 queue restart)
* @author Benson
* @version 1.0
* @param jqueryObject obj 要綁定的 Queue物件(可以是任何 el)
* @param string name Queue 名稱
* @returns void
*/
function job_queue()
{
var _obj = null;
var _name = '';
var func_first = null;
var func_first_mapping = null;
var func_last = null;
var func_last_mapping = null;
var list_func = [];
var list_func_mapping = [];
var run_num = 0;
this.init = function(obj, name)
{
_obj = obj;
_name = name;
};
/**
* 註冊 Queue (Queue執行中,不會有效果)
* @param function func 要加入Queue的Function
* @returns void
*/
this.reg = function(func, id)
{
if(typeof func !== 'function')
{
return false;
}
list_func.push(func);
list_func_mapping.push($.trim(id));
};
/**
* 註冊為第一個 Queue (Queue執行中,不會有效果)
* @param function func 要加入Queue的Function
* @returns void
*/
this.reg_first = function(func, id)
{
if(typeof func !== 'function')
{
return false;
}
func_first = func;
func_first_mapping = id;
};
/**
* 註冊為最後一個 Queue (Queue執行中,不會有效果)
* @param function func 要加入Queue的Function
* @returns void
*/
this.reg_last = function(func, id)
{
if(typeof func !== 'function')
{
return false;
}
func_last = func;
func_last_mapping = id;
};
/**
* 執行下一個 Queue
* @returns void
*/
this.next = function()
{
if(run_num > _length())
{
run_num = 0;
}
//未出始
if(run_num === 0)
{
_obj.clearQueue(_name);
_obj.queue(_name, func_first);
for(var i=0; i<list_func.length;i++)
{
_obj.queue(_name, list_func[i]);
}
_obj.queue(_name, func_last);
_obj.queue(_name, function(){
run_num = 0;
_obj.clearQueue();
});
}
run_num++;
//執行
_obj.dequeue(_name);
};
/**
* 刪除指定 queue
* @param string id
*/
this.remove = function(id)
{
if(func_first_mapping === id)
{
func_first = null;
return true;
}
if(func_last_mapping === id)
{
func_last = null;
return true;
}
for(i in list_func_mapping)
{
if(list_func_mapping[i] === id)
{
list_func.splice(i, 1);
}
}
};
/**
* 清空並停止 Queue
* @returns void
*/
this.clear = function()
{
run_num = 0;
_obj.clearQueue(_name);
};
/**
* 計算總 queue 數
* @returns int
*/
this.length = function()
{
return _length();
};
/**
* 計算總 queue 數
* @access private
* @returns int
*/
function _length()
{
var leng = list_func.length;
if(typeof func_first === 'function')
{
leng++;
}
if(typeof func_last === 'function')
{
leng++;
}
return leng;
}
/**
* 是否為最後一個 queue
* @returns boolean
*/
this.is_last = function()
{
return _obj.queue(_name).length === 1 && run_num === _length();
};
/**
* 目前已執行的 job 流水號
* @returns int
*/
this.number = function()
{
return run_num;
};
}