forked from bitovi/funcunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
181 lines (167 loc) · 4.81 KB
/
Copy pathqueue.js
File metadata and controls
181 lines (167 loc) · 4.81 KB
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
steal('./core.js', function(FuncUnit) {
/**
* @add FuncUnit
*/
/**
* True when we are in a callback function (something we pass to a FuncUnit plugin).
*/
FuncUnit._incallback = false;
//where we should add things in a callback
var currentPosition = 0,
startedQueue = false;
/**
* @property FuncUnit.timeout timeout
* @parent FuncUnit.static
* A global timeout value for wait commands. Defaults to 10 seconds.
*/
FuncUnit.timeout = 10000;
/**
* @hide
* A queue of methods. Each method in the queue are run in order. After the method is complete, it
* calls FuncUnit._done, which pops the next method off the queue and runs it.
*/
FuncUnit._queue = [];
/**
* @hide
* Logic that determines if this next query needs to be sync, or if we can optimize it.
* Returns false if there are actual actions in the queue, returns true if the only queued methods are
* S methods. If the only method is an S query, remove it from the queue.
*/
FuncUnit._needSyncQuery = function(){
// if only method is query, need sync
if(FuncUnit._queue.length === 1){
if(FuncUnit._queue[0].type === "query"){
FuncUnit._queue = [];
return true;
}
}
// if empty queue, need sync
if(FuncUnit._queue.length === 0){
return true;
}
return false
}
/**
* @hide
* Return last item in the queue.
*/
FuncUnit._lastQueuedItem = function(){
if(!FuncUnit._queue.length){
return null;
}
return FuncUnit._queue[FuncUnit._queue.length-1];
}
/**
* @hide
* Return true if there are already async methods queued. If true, getters need throw errors.
*/
FuncUnit._haveAsyncQueries = function(){
for(var i=0; i < FuncUnit._queue.length; i++){
if(FuncUnit._queue[i].type === "action" || FuncUnit._queue[i].type === "wait")
return true;
}
return false;
}
FuncUnit.
/**
* @parent FuncUnit.static
* @function FuncUnit.add add
* @signature `add(handler)`
* Adds a function to the queue.
* @param {Object} handler An object that contains the method to run along with other properties:
- method : the method to be called. It will be provided a success and error function to call
- success : an optional callback to be called after the function is done
- error : an error message if the command fails
- timeout : the time until success should be called
- bind : an object that will be 'this' of the success
- type: the type of method (optional)
*/
add = function(handler){
//if we are in a callback, add to the current position
if (FuncUnit._incallback) {
FuncUnit._queue.splice(currentPosition, 0, handler);
currentPosition++;
}
else {
//add to the end
FuncUnit._queue.push(handler);
}
//if our queue has just started, stop qunit
//call done to call the next command
if (FuncUnit._queue.length == 1 && ! FuncUnit._incallback) {
FuncUnit.unit.pauseTest();
setTimeout(FuncUnit._done, 13)
}
}
var currentEl;
/**
* @hide
* @parent FuncUnit.static
* @function FuncUnit._done _done
* @signature `_done(handler)`
*
* Every queued method calls this when its complete. It gets the next function from the queue and calls it.
* @param {Object} el the current jQuery collection
* @param {Object} selector
*/
FuncUnit._done = function(el, selector){
var next,
timer,
speed = 0;
if(FuncUnit.speed == "slow"){
speed = 500;
}
if (FuncUnit._queue.length > 0) {
next = FuncUnit._queue.shift();
currentPosition = 0;
// set a timer that will error
//call next method
setTimeout(function(){
timer = setTimeout(function(){
next.stop && next.stop();
if(typeof next.error === "function"){
next.error();
} else {
FuncUnit.unit.assertOK(false, next.error);
}
FuncUnit._done();
},
(next.timeout || FuncUnit.timeout) + speed)
// if the last successful method had a collection, save it
if(el && el.jquery){
currentEl = el;
}
// make the new collection the last successful collection
if(currentEl){
next.bind = currentEl;
}
next.selector = selector;
next.method( //success
function(el){
if(el && el.jquery){
next.bind = el;
}
//make sure we don't create an error
clearTimeout(timer);
//mark in callback so the next set of add get added to the front
FuncUnit._incallback = true;
if (next.success) {
// callback's "this" is the collection
next.success.apply(next.bind, arguments);
}
FuncUnit._incallback = false;
FuncUnit._done(next.bind, next.selector);
}, //error
function(message){
clearTimeout(timer);
FuncUnit.unit.assertOK(false, message);
FuncUnit._done();
})
}, speed);
}
else {
FuncUnit.unit.resumeTest();
}
}
return FuncUnit;
});