00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 @import <Foundation/CPObject.j>
00023
00029 CPOperationQueuePriorityVeryLow = -8;
00030
00036 CPOperationQueuePriorityLow = -4;
00037
00043 CPOperationQueuePriorityNormal = 0;
00044
00050 CPOperationQueuePriorityHigh = 4;
00051
00057 CPOperationQueuePriorityVeryHigh = 8;
00058
00059
00066 @implementation CPOperation : CPObject
00067 {
00068 CPArray operations;
00069 BOOL _cancelled;
00070 BOOL _executing;
00071 BOOL _finished;
00072 BOOL _ready;
00073 int _queuePriority;
00074 JSObject _completionFunction;
00075 CPArray _dependencies;
00076 }
00077
00078
00079 - (void)main
00080 {
00081
00082 }
00083
00084 - (id)init
00085 {
00086 if (self = [super init])
00087 {
00088 _cancelled = NO;
00089 _executing = NO;
00090 _finished = NO;
00091 _ready = YES;
00092 _dependencies = [[CPArray alloc] init];
00093 _queuePriority = CPOperationQueuePriorityNormal;
00094 }
00095 return self;
00096 }
00097
00101 - (void)start
00102 {
00103 if (!_cancelled)
00104 {
00105 [self willChangeValueForKey:@"isExecuting"];
00106 _executing = YES;
00107 [self didChangeValueForKey:@"isExecuting"];
00108 [self main];
00109 if (_completionFunction)
00110 {
00111 _completionFunction();
00112 }
00113 [self willChangeValueForKey:@"isExecuting"];
00114 _executing = NO;
00115 [self didChangeValueForKey:@"isExecuting"];
00116 [self willChangeValueForKey:@"isFinished"];
00117 _finished = YES;
00118 [self didChangeValueForKey:@"isFinished"];
00119 }
00120 }
00121
00126 - (BOOL)isCancelled
00127 {
00128 return _cancelled;
00129 }
00130
00135 - (BOOL)isExecuting
00136 {
00137 return _executing;
00138 }
00139
00144 - (BOOL)isFinished
00145 {
00146 return _finished;
00147 }
00148
00153 - (BOOL)isConcurrent
00154 {
00155 return NO;
00156 }
00157
00162 - (BOOL)isReady
00163 {
00164 return _ready;
00165 }
00166
00171 - (JSObject)completionFunction
00172 {
00173 return _completionFunction;
00174 }
00175
00179 - (void)setCompletionFunction:(JSObject)aJavaScriptFunction
00180 {
00181 _completionFunction = aJavaScriptFunction;
00182 }
00183
00188 - (void)addDependency:(CPOperation)anOperation
00189 {
00190 [self willChangeValueForKey:@"dependencies"];
00191 [anOperation addObserver:self
00192 forKeyPath:@"isFinished"
00193 options:(CPKeyValueObservingOptionNew)
00194 context:NULL];
00195 [_dependencies addObject:anOperation];
00196 [self didChangeValueForKey:@"dependencies"];
00197 [self _updateIsReadyState];
00198 }
00199
00204 - (void)removeDependency:(CPOperation)anOperation
00205 {
00206 [self willChangeValueForKey:@"dependencies"];
00207 [_dependencies removeObject:anOperation];
00208 [anOperation removeObserver:self
00209 forKeyPath:@"isFinished"];
00210 [self didChangeValueForKey:@"dependencies"];
00211 [self _updateIsReadyState];
00212 }
00213
00218 - (CPArray)dependencies
00219 {
00220 return _dependencies;
00221 }
00222
00226 - (void)waitUntilFinished
00227 {
00228 }
00229
00233 - (void)cancel
00234 {
00235 [self willChangeValueForKey:@"isCancelled"];
00236 _cancelled = YES;
00237 [self didChangeValueForKey:@"isCancelled"];
00238 }
00239
00244 - (void)setQueuePriority:(int)priority
00245 {
00246 _queuePriority = priority;
00247 }
00248
00253 - (int)queuePriority
00254 {
00255 return _queuePriority;
00256 }
00257
00258
00259 - (void)observeValueForKeyPath:(CPString)keyPath
00260 ofObject:(id)object
00261 change:(CPDictionary)change
00262 context:(void)context
00263 {
00264 if (keyPath == @"isFinished")
00265 {
00266 [self _updateIsReadyState];
00267 }
00268 }
00269
00270 - (void)_updateIsReadyState
00271 {
00272 var newReady = YES;
00273 if (_dependencies && [_dependencies count] > 0)
00274 {
00275 var i = 0;
00276 for (i = 0; i < [_dependencies count]; i++)
00277 {
00278 if (![[_dependencies objectAtIndex:i] isFinished])
00279 {
00280 newReady = NO;
00281 }
00282 }
00283 }
00284
00285 if (newReady != _ready)
00286 {
00287 [self willChangeValueForKey:@"isReady"];
00288 _ready = newReady;
00289 [self didChangeValueForKey:@"isReady"];
00290 }
00291 }
00292
00293 @end