API  0.9.10
CPOperation.j
Go to the documentation of this file.
1 /*
2  * CPOperation.j
3  *
4  * Created by Johannes Fahrenkrug.
5  * Copyright 2009, Springenwerk.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 
24 
31 
38 
45 
52 
59 
60 
66 @implementation CPOperation : CPObject
67 {
68  CPArray operations;
69  BOOL _cancelled;
70  BOOL _executing;
71  BOOL _finished;
72  BOOL _ready;
73  int _queuePriority;
74  JSObject _completionFunction;
75  CPArray _dependencies;
76 }
77 
78 - (void)main
79 {
80  // should be overridden in child class
81 }
82 
83 - (id)init
84 {
85  self = [super init];
86 
87  if (self)
88  {
89  _cancelled = NO;
90  _executing = NO;
91  _finished = NO;
92  _ready = YES;
93  _dependencies = [[CPArray alloc] init];
94  _queuePriority = CPOperationQueuePriorityNormal;
95  }
96  return self;
97 }
98 
102 - (void)start
103 {
104  if (!_cancelled)
105  {
106  [self willChangeValueForKey:@"isExecuting"];
107  _executing = YES;
108  [self didChangeValueForKey:@"isExecuting"];
109  [self main];
110  if (_completionFunction)
111  {
112  _completionFunction();
113  }
114  [self willChangeValueForKey:@"isExecuting"];
115  _executing = NO;
116  [self didChangeValueForKey:@"isExecuting"];
117  }
118 
119  [self willChangeValueForKey:@"isFinished"];
120  _finished = YES;
121  [self didChangeValueForKey:@"isFinished"];
122 }
123 
128 - (BOOL)isCancelled
129 {
130  return _cancelled;
131 }
132 
137 - (BOOL)isExecuting
138 {
139  return _executing;
140 }
141 
146 - (BOOL)isFinished
147 {
148  return _finished;
149 }
150 
156 {
157  return NO;
158 }
159 
164 - (BOOL)isReady
165 {
166  return _ready;
167 }
168 
174 {
175  return _completionFunction;
176 }
177 
181 - (void)setCompletionFunction:(JSObject)aJavaScriptFunction
182 {
183  _completionFunction = aJavaScriptFunction;
184 }
185 
190 - (void)addDependency:(CPOperation)anOperation
191 {
192  [self willChangeValueForKey:@"dependencies"];
193  [anOperation addObserver:self
194  forKeyPath:@"isFinished"
195  options:(CPKeyValueObservingOptionNew)
196  context:NULL];
197  [_dependencies addObject:anOperation];
198  [self didChangeValueForKey:@"dependencies"];
199  [self _updateIsReadyState];
200 }
201 
206 - (void)removeDependency:(CPOperation)anOperation
207 {
208  [self willChangeValueForKey:@"dependencies"];
209  [_dependencies removeObject:anOperation];
210  [anOperation removeObserver:self
211  forKeyPath:@"isFinished"];
212  [self didChangeValueForKey:@"dependencies"];
213  [self _updateIsReadyState];
214 }
215 
220 - (CPArray)dependencies
221 {
222  return _dependencies;
223 }
224 
229 {
230 }
231 
235 - (void)cancel
236 {
237  [self willChangeValueForKey:@"isCancelled"];
238  _cancelled = YES;
239  [self didChangeValueForKey:@"isCancelled"];
240 }
241 
246 - (void)setQueuePriority:(int)priority
247 {
248  _queuePriority = priority;
249 }
250 
256 {
257  return _queuePriority;
258 }
259 
260 // We need to observe the "isFinished" key of our dependent operations so we can update our own "isReady" state
261 - (void)observeValueForKeyPath:(CPString)keyPath
262  ofObject:(id)object
263  change:(CPDictionary)change
264  context:(void)context
265 {
266  if (keyPath == @"isFinished")
267  {
268  [self _updateIsReadyState];
269  }
270 }
271 
272 - (void)_updateIsReadyState
273 {
274  var newReady = YES;
275  if (_dependencies && [_dependencies count] > 0)
276  {
277  var i = 0;
278  for (i = 0; i < [_dependencies count]; i++)
279  {
280  if (![[_dependencies objectAtIndex:i] isFinished])
281  {
282  newReady = NO;
283  }
284  }
285  }
286 
287  if (newReady != _ready)
288  {
289  [self willChangeValueForKey:@"isReady"];
290  _ready = newReady;
291  [self didChangeValueForKey:@"isReady"];
292  }
293 }
294 
295 @end
void willChangeValueForKey:(CPString aKey)
BOOL isExecuting()
Definition: CPOperation.j:137
CPArray operations
Definition: CPOperation.h:4
CPOperationQueuePriorityVeryHigh
Definition: CPOperation.j:58
BOOL isConcurrent()
Definition: CPOperation.j:155
global CPKeyValueObservingOptionNew CPOperationQueuePriorityVeryLow
Definition: CPOperation.j:30
BOOL isReady()
Definition: CPOperation.j:164
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPArray dependencies()
Definition: CPOperation.j:220
void waitUntilFinished()
Definition: CPOperation.j:228
An immutable string (collection of characters).
Definition: CPString.h:2
BOOL isFinished()
Definition: CPOperation.j:146
void addObserver:forKeyPath:options:context:(id anObserver, [forKeyPath] CPString aPath, [options] CPKeyValueObservingOptions options, [context] id aContext)
CPOperationQueuePriorityLow
Definition: CPOperation.j:37
CPKeyValueObservingOptionNew
void didChangeValueForKey:(CPString aKey)
Represents an operation that can be run in an CPOperationQueue.
Definition: CPOperation.h:2
void removeObserver:forKeyPath:(id anObserver, [forKeyPath] CPString aPath)
id init()
Definition: CPObject.j:145
JSObject completionFunction()
Definition: CPOperation.j:173
BOOL isCancelled()
Definition: CPOperation.j:128
CPOperationQueuePriorityNormal
Definition: CPOperation.j:44
CPOperationQueuePriorityHigh
Definition: CPOperation.j:51
int queuePriority()
Definition: CPOperation.j:255