Skip to content

Commit 9bdeac0

Browse files
committed
Adding ARC support
1 parent f2fbc4c commit 9bdeac0

File tree

4 files changed

+18
-41
lines changed

4 files changed

+18
-41
lines changed

objc-promise.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@
340340
7823C8B8162CE9EB000342B4 /* Debug */ = {
341341
isa = XCBuildConfiguration;
342342
buildSettings = {
343+
CLANG_ENABLE_OBJC_ARC = YES;
343344
DSTROOT = /tmp/objc_promise.dst;
344345
GCC_PRECOMPILE_PREFIX_HEADER = YES;
345346
GCC_PREFIX_HEADER = "objc-promise/objc-promise-Prefix.pch";
@@ -352,6 +353,7 @@
352353
7823C8B9162CE9EB000342B4 /* Release */ = {
353354
isa = XCBuildConfiguration;
354355
buildSettings = {
356+
CLANG_ENABLE_OBJC_ARC = YES;
355357
DSTROOT = /tmp/objc_promise.dst;
356358
GCC_PRECOMPILE_PREFIX_HEADER = YES;
357359
GCC_PREFIX_HEADER = "objc-promise/objc-promise-Prefix.pch";

objc-promise/Deferred.m

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,17 @@ - (void)transitionToState:(PromiseState)state
2121

2222
shouldComplete = YES;
2323

24-
blocksToExecute = [_callbackBindings retain];
24+
blocksToExecute = _callbackBindings;
2525

26-
[_callbackBindings release];
2726
_callbackBindings = nil;
2827
}
2928
}
3029

3130
if (shouldComplete) {
3231
for (bound_block block in blocksToExecute) {
3332
[self executeBlock:block];
34-
35-
Block_release(block);
3633
}
3734
}
38-
39-
[blocksToExecute release];
4035
}
4136

4237
@end
@@ -53,7 +48,7 @@ - (id)init
5348

5449
+ (Deferred *)deferred
5550
{
56-
return [[[Deferred alloc] init] autorelease];
51+
return [[Deferred alloc] init];
5752
}
5853

5954
- (Promise *)promise
@@ -63,7 +58,7 @@ - (Promise *)promise
6358

6459
- (Promise *)resolve:(id)result
6560
{
66-
_result = [result retain];
61+
_result = result;
6762

6863
[self transitionToState:Resolved];
6964

@@ -72,7 +67,7 @@ - (Promise *)resolve:(id)result
7267

7368
- (Promise *)reject:(NSError *)reason
7469
{
75-
_reason = [reason retain];
70+
_reason = reason;
7671

7772
[self transitionToState:Rejected];
7873

objc-promise/Promise.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,11 @@ typedef enum {
5757
- (Promise *)timeout:(NSTimeInterval)interval;
5858

5959
@end
60+
61+
62+
@interface Promise (Private)
63+
64+
- (void)executeBlock:(bound_block)block;
65+
66+
@end
67+

objc-promise/Promise.m

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ - (id)initWithQueue:(dispatch_queue_t)queue
1717
_callbackBindings = [[NSMutableArray alloc] init];
1818
_state = Incomplete;
1919

20-
_queue = [queue retain];
20+
_queue = queue;
2121

2222
_stateLock = [[NSObject alloc] init];
2323
_result = nil;
@@ -32,7 +32,7 @@ - (BOOL)bindOrCallBlock:(bound_block)block
3232

3333
@synchronized (_stateLock) {
3434
if (_state == Incomplete) {
35-
[_callbackBindings addObject:Block_copy(block)];
35+
[_callbackBindings addObject: block];
3636

3737
blockWasBound = YES;
3838
}
@@ -96,22 +96,11 @@ - (id)init
9696

9797
- (void)dealloc
9898
{
99-
[_callbackBindings release];
10099
_callbackBindings = nil;
101-
102-
[_stateLock release];
103100
_stateLock = nil;
104-
105-
[_result release];
106101
_result = nil;
107-
108-
[_reason release];
109102
_reason = nil;
110-
111-
[_queue release];
112103
_queue = nil;
113-
114-
[super dealloc];
115104
}
116105

117106
- (BOOL)isResolved
@@ -174,15 +163,10 @@ - (Promise *)when:(resolved_block)resolvedBlock
174163
{
175164
__block Promise *this = self;
176165

177-
// retain the block until we can call with the result
178-
Block_copy(resolvedBlock);
179-
180166
[this bindOrCallBlock:^{
181167
if (this.isResolved) {
182168
resolvedBlock(this.result);
183169
}
184-
185-
Block_release(resolvedBlock);
186170
}];
187171

188172
return this;
@@ -192,15 +176,10 @@ - (Promise *)failed:(rejected_block)rejectedBlock
192176
{
193177
__block Promise *this = self;
194178

195-
// retain the block until we can call with the result
196-
Block_copy(rejectedBlock);
197-
198179
[this bindOrCallBlock:^{
199180
if (this.isRejected) {
200181
rejectedBlock(this.reason);
201182
}
202-
203-
Block_release(rejectedBlock);
204183
}];
205184

206185
return this;
@@ -210,13 +189,8 @@ - (Promise *)any:(any_block)anyBlock
210189
{
211190
__block Promise *this = self;
212191

213-
// retain the block until we can call with the result
214-
Block_copy(anyBlock);
215-
216192
[this bindOrCallBlock:^{
217193
anyBlock();
218-
219-
Block_release(anyBlock);
220194
}];
221195

222196
return this;
@@ -245,7 +219,7 @@ - (Promise *)on:(dispatch_queue_t)queue
245219

246220
[self chainTo:deferred];
247221

248-
return [deferred autorelease];
222+
return deferred;
249223
}
250224

251225
- (Promise *)onMainQueue
@@ -263,7 +237,7 @@ - (Promise *)timeout:(NSTimeInterval)interval
263237

264238
// use the current dispatch queue if no queue is bound
265239
if (queue == nil) {
266-
queue = dispatch_get_current_queue();
240+
queue = dispatch_get_main_queue();
267241
}
268242

269243
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
@@ -274,7 +248,6 @@ - (Promise *)timeout:(NSTimeInterval)interval
274248
NSLog(@"1. %@", [NSDate date]);
275249
void (^eventHandler)(void) = ^{
276250
dispatch_source_cancel(timer);
277-
dispatch_release(timer);
278251

279252
NSLog(@"2. %@", [NSDate date]);
280253
[toTimeout reject:[NSError errorWithDomain:@"Timeout" code:100 userInfo:nil]];
@@ -284,7 +257,6 @@ - (Promise *)timeout:(NSTimeInterval)interval
284257
dispatch_source_set_event_handler(timer, eventHandler);
285258
dispatch_resume(timer);
286259

287-
[toTimeout release];
288260
NSLog(@"3. %@", [NSDate date]);
289261

290262
return [toTimeout promise];

0 commit comments

Comments
 (0)