Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Futures can serialize arbitrary blocks.

I also added conveniences for cancelling or completing with a block.
  • Loading branch information...
commit 605c4374a2419f6461819600ab38c4a5e359e909 1 parent f1b7b02
Rob Rix authored

Showing 2 changed files with 41 additions and 20 deletions. Show diff stats Hide diff stats

  1. +7 2 RXFutures/RXFuture.h
  2. +34 18 RXFutures/RXFuture.m
9 RXFutures/RXFuture.h
@@ -14,11 +14,16 @@
14 14 }
15 15
16 16 -(void)onComplete:(void(^)())block;
17   --(void)onCancel:(void(^)())block;
  17 +-(void)complete;
  18 +-(void)complete:(void(^)())block; // shorthand for -onComplete: followed by -complete
18 19
  20 +-(void)onCancel:(void(^)())block;
19 21 -(void)cancel;
20   --(void)complete;
  22 +-(void)cancel:(void(^)())block; // shorthand for -onCancel: followed by -cancel
  23 +
  24 +-(void)performBlock:(void(^)())block;
21 25
  26 +// these properties are only meaningful and safe within blocks passed to -performBlock:
22 27 @property (nonatomic, readonly, assign, getter=isCancelled) BOOL cancelled;
23 28 @property (nonatomic, readonly, assign, getter=isCompleted) BOOL completed;
24 29
52 RXFutures/RXFuture.m
@@ -32,50 +32,66 @@ -(void)dealloc {
32 32 }
33 33
34 34
35   --(void)dispatchBlock:(void(^)())block {
  35 +-(void)dispatchCallback:(void(^)())block {
36 36 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
37 37 }
38 38
39 39
40   --(void)onComplete:(void(^)())block {
41   - dispatch_async(queue, ^{
42   - if(completed)
43   - [self dispatchBlock:block];
44   - else
45   - [completionHandlers addObject:block];
46   - });
  40 +-(void)performBlock:(void(^)())block
  41 +{
  42 + dispatch_async(queue, block);
47 43 }
48 44
  45 +
49 46 -(void)onCancel:(void(^)())block {
50   - dispatch_async(queue, ^{
  47 + [self performBlock:^{
51 48 if(cancelled)
52   - [self dispatchBlock:block];
  49 + [self dispatchCallback:block];
53 50 else
54 51 [cancellationHandlers addObject:block];
55   - });
  52 + }];
56 53 }
57 54
58   -
59 55 -(void)cancel {
60   - dispatch_async(queue, ^{
  56 + [self performBlock:^{
61 57 if(!cancelled && !completed) {
62 58 self.cancelled = YES;
63 59 for(void (^block)() in cancellationHandlers) {
64   - [self dispatchBlock:block];
  60 + [self dispatchCallback:block];
65 61 }
66 62 }
67   - });
  63 + }];
  64 +}
  65 +
  66 +-(void)cancel:(void(^)())block {
  67 + [self onCancel:block];
  68 + [self cancel];
  69 +}
  70 +
  71 +
  72 +-(void)onComplete:(void(^)())block {
  73 + [self performBlock:^{
  74 + if(completed)
  75 + [self dispatchCallback:block];
  76 + else
  77 + [completionHandlers addObject:block];
  78 + }];
68 79 }
69 80
70 81 -(void)complete {
71   - dispatch_async(queue, ^{
  82 + [self performBlock:^{
72 83 if(!cancelled && !completed) {
73 84 self.completed = YES;
74 85 for(void (^block)() in completionHandlers) {
75   - [self dispatchBlock:block];
  86 + [self dispatchCallback:block];
76 87 }
77 88 }
78   - });
  89 + }];
  90 +}
  91 +
  92 +-(void)complete:(void(^)())block {
  93 + [self onComplete:block];
  94 + [self complete];
79 95 }
80 96
81 97 @end

0 comments on commit 605c437

Please sign in to comment.
Something went wrong with that request. Please try again.