forked from cappuccino/cappuccino
/
CPOperationQueue.j
304 lines (273 loc) · 7.39 KB
/
CPOperationQueue.j
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* CPOperationQueue.j
*
* Created by Johannes Fahrenkrug.
* Copyright 2009, Springenwerk.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import "CPArray.j"
@import "CPFunctionOperation.j"
@import "CPInvocationOperation.j"
@import "CPObject.j"
@import "CPOperation.j"
@import "CPString.j"
@import "CPTimer.j"
// the global queue (mainQueue)
var cpOperationMainQueue = nil;
/*!
@class CPOperationQueue
@brief Represents an operation queue that can run CPOperations
*/
@implementation CPOperationQueue : CPObject
{
CPArray _operations;
BOOL _suspended;
CPString _name @accessors(property=name);
CPTimer _timer;
}
- (id)init
{
self = [super init];
if (self)
{
_operations = [[CPArray alloc] init];
_suspended = NO;
_currentlyModifyingOps = NO;
_timer = [CPTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(_runNextOpsInQueue)
userInfo:nil
repeats:YES];
}
return self;
}
- (void)_runNextOpsInQueue
{
if (!_suspended && [self operationCount] > 0)
{
var i = 0,
count = [_operations count];
for (; i < count; i++)
{
var op = [_operations objectAtIndex:i];
if ([op isReady] && ![op isCancelled] && ![op isFinished] && ![op isExecuting])
{
[op start];
}
}
}
}
- (void)_enableTimer:(BOOL)enable
{
if (!enable)
{
if (_timer)
{
[_timer invalidate];
_timer = nil;
}
}
else
{
if (!_timer)
{
_timer = [CPTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(_runNextOpsInQueue)
userInfo:nil
repeats:YES];
}
}
}
/*!
Adds the specified operation object to the receiver.
@param anOperation the operation that should be scheduled for execution
*/
- (void)addOperation:(CPOperation)anOperation
{
[self willChangeValueForKey:@"operations"];
[self willChangeValueForKey:@"operationCount"];
[_operations addObject:anOperation];
[self _sortOpsByPriority:_operations];
[self didChangeValueForKey:@"operations"];
[self didChangeValueForKey:@"operationCount"];
}
/*!
Adds the specified array of operations to the queue.
@param ops The array of CPOperation objects that you want to add to the receiver.
@param wait If YES, the method only returns once all of the specified operations finish executing. If NO, the operations are added to the queue and control returns immediately to the caller.
*/
- (void)addOperations:(CPArray)ops waitUntilFinished:(BOOL)wait
{
if (ops)
{
if (wait)
{
[self _sortOpsByPriority:ops];
[self _runOpsSynchronously:ops];
}
[_operations addObjectsFromArray:ops];
[self _sortOpsByPriority:_operations];
}
}
/*!
Wraps the given js function in a CPOperation and adds it to the queue
@param aFunction the JS function to add
*/
- (void)addOperationWithFunction:(JSObject)aFunction
{
[self addOperation:[CPFunctionOperation functionOperationWithFunction:aFunction]];
}
- (CPArray)operations
{
return _operations;
}
- (int)operationCount
{
if (_operations)
{
return [_operations count];
}
return 0;
}
/*!
Cancels all queued and executing operations.
*/
- (void)cancelAllOperations
{
if (_operations)
{
var i = 0,
count = [_operations count];
for (; i < count; i++)
{
[[_operations objectAtIndex:i] cancel];
}
}
}
/*!
Blocks until all of the receiver’s queued and executing operations finish executing.
*/
- (void)waitUntilAllOperationsAreFinished
{
// lets first stop the timer so it won't interfere
[self _enableTimer:NO];
[self _runOpsSynchronously:_operations];
if (!_suspended)
{
[self _enableTimer:YES];
}
}
/*!
Returns the maximum number of concurrent operations that the receiver can execute.
Always returns 1 because JS doesn't have threads
*/
- (int)maxConcurrentOperationCount
{
return 1;
}
/*!
Modifies the execution of pending operations
@param suspend if YES, queue execution is suspended. If NO, it is resumed
*/
- (void)setSuspended:(BOOL)suspend
{
_suspended = suspend;
[self _enableTimer:!suspend];
}
/*!
Returns a Boolean value indicating whether the receiver is scheduling queued operations for execution.
*/
- (BOOL)isSuspended
{
return _suspended;
}
- (void)_sortOpsByPriority:(CPArray)someOps
{
if (someOps)
{
[someOps sortUsingFunction:function(lhs, rhs)
{
if ([lhs queuePriority] < [rhs queuePriority])
{
return 1;
}
else
{
if ([lhs queuePriority] > [rhs queuePriority])
{
return -1;
}
else
{
return 0;
}
}
}
context:nil];
}
}
- (void)_runOpsSynchronously:(CPArray)ops
{
if (ops)
{
var keepGoing = YES;
while (keepGoing)
{
var i = 0,
count = [ops count];
keepGoing = NO;
// start the ones that are ready
for (; i < count; i++)
{
var op = [ops objectAtIndex:i];
if ([op isReady] && ![op isCancelled] && ![op isFinished] && ![op isExecuting])
{
[op start];
}
}
// make sure they are all done
for (i = 0; i < count; i++)
{
var op = [ops objectAtIndex:i];
if (![op isFinished] && ![op isCancelled])
{
keepGoing = YES;
}
}
}
}
}
/*!
Convenience method for one system wide singleton queue. Returns the same queue as currentQueue.
*/
+ (CPOperationQueue)mainQueue
{
if (!cpOperationMainQueue)
{
cpOperationMainQueue = [[CPOperationQueue alloc] init];
[cpOperationMainQueue setName:@"main"];
}
return cpOperationMainQueue;
}
/*!
Convenience method for one system wide singleton queue. Returns the same queue as mainQueue.
*/
+ (CPOperationQueue)currentQueue
{
return [CPOperationQueue mainQueue];
}
@end