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
|
#pragma mark - BlockMethodSignature
struct NSBlockLiteral { void *isa; int flags; int reserved; void (*invoke)(void *, ...); struct block_descriptor { unsigned long int reserved; unsigned long int size; void (*copy_helper)(void *dst, void *src); void (*dispose_helper)(void *src); const char *signature; } *descriptor; };
typedef NS_OPTIONS(NSUInteger, NSBlockDescriptionFlags) { NSBlockDescriptionFlagsHasCopyDispose = (1 << 25), NSBlockDescriptionFlagsHasCtor = (1 << 26), NSBlockDescriptionFlagsIsGlobal = (1 << 28), NSBlockDescriptionFlagsHasStret = (1 << 29), NSBlockDescriptionFlagsHasSignature = (1 << 30) };
static NSMethodSignature *NSMethodSignatureForBlock(id block) { if (!block) return nil; struct NSBlockLiteral *blockRef = (__bridge struct NSBlockLiteral *)block; NSBlockDescriptionFlags flags = (NSBlockDescriptionFlags)blockRef->flags; if (flags & NSBlockDescriptionFlagsHasSignature) { void *signatureLocation = blockRef->descriptor; signatureLocation += sizeof(unsigned long int); signatureLocation += sizeof(unsigned long int); if (flags & NSBlockDescriptionFlagsHasCopyDispose) { signatureLocation += sizeof(void(*)(void *dst, void *src)); signatureLocation += sizeof(void (*)(void *src)); } const char *signature = (*(const char **)signatureLocation); return [NSMethodSignature signatureWithObjCTypes:signature]; } return 0; }
#pragma mark - Context
@interface CCMFutureContext : NSObject
+ (instancetype)contextWithQueue:(CCMDispatchQueue *)queue parent:(CCMFutureContext *)parent;
+ (CCMFutureContext *)currentContext; + (void)setCurrentContext:(CCMFutureContext *)context;
@property (nonatomic, strong, readonly) CCMDispatchQueue *dispatchQueue; @property (nonatomic, strong, readonly) CCMFutureContext *parentContext;
@end
void CCMFutureBeginContext(CCMDispatchQueue *queue) { CCMFutureContext *context = [CCMFutureContext contextWithQueue:queue parent:[CCMFutureContext currentContext]]; [CCMFutureContext setCurrentContext:context]; }
void CCMFutureEndContext() { CCMFutureContext *context = [CCMFutureContext currentContext].parentContext; [CCMFutureContext setCurrentContext:context]; }
@implementation CCMFutureContext
+ (instancetype)contextWithQueue:(CCMDispatchQueue *)queue parent:(CCMFutureContext *)parent { CCMFutureContext *context = [CCMFutureContext new]; context->_dispatchQueue = queue; context->_parentContext = parent; return context; }
+ (void)setCurrentContext:(CCMFutureContext *)context { if (context == nil) { [[NSThread currentThread].threadDictionary removeObjectForKey:@"CCMCurrentFutureContext"]; } else { [NSThread currentThread].threadDictionary[@"CCMCurrentFutureContext"] = context; } }
+ (CCMFutureContext *)currentContext { CCMFutureContext *context = [NSThread currentThread].threadDictionary[@"CCMCurrentFutureContext"]; if (context == nil) { context = [self contextWithQueue:[CCMDispatchQueue main] parent:nil]; [self setCurrentContext:context]; } return context; }
@end
#pragma mark - Future
typedef void(^CCMFutureBlockWrapper)(id value);
@interface CCMFuture ()
@property (nonatomic, strong) id futureValue; @property (nonatomic, strong, readonly) CCMDispatchQueue *dispatchQueue; @property (nonatomic, strong, readonly) NSMutableArray<CCMFutureBlockWrapper> *blocks;
- (void)tryExecuteCallbacks;
@end
@implementation CCMFuture
- (instancetype)init { if (self = [super init]) { _dispatchQueue = [CCMDispatchQueue serialQueueWithName:@"queue.util.future"]; _blocks = [NSMutableArray new]; } return self; }
#define ccm_future_complete_block_case(type, value) \ case type: \ completeBlock(nil, value); \ break; \
- (instancetype)onComplete:(void(^)())completeBlock { CCMDispatchQueue *q = [CCMFutureContext currentContext].dispatchQueue; CCMFutureBlockWrapper c = ^(id value) { [q async:^{ if ([value isKindOfClass:[NSError class]]) { completeBlock(value, nil); } else if (value == [NSNull null]) { completeBlock(nil, nil); } else if (value != nil) { NSMethodSignature *methodSignature = NSMethodSignatureForBlock(completeBlock); if (methodSignature == nil) { completeBlock(nil, value); } else { if (methodSignature.numberOfArguments < 3) { completeBlock(); return ; } const char type = [methodSignature getArgumentTypeAtIndex:2][0]; switch (type) { ccm_future_complete_block_case('@', value) ccm_future_complete_block_case('c', [value charValue]) ccm_future_complete_block_case('i', [value intValue]) ccm_future_complete_block_case('s', [value shortValue]) ccm_future_complete_block_case('l', [value longValue]) ccm_future_complete_block_case('q', [value longValue]) ccm_future_complete_block_case('C', [value unsignedCharValue]) ccm_future_complete_block_case('I', [value unsignedIntValue]) ccm_future_complete_block_case('S', [value unsignedShortValue]) ccm_future_complete_block_case('L', [value unsignedLongValue]) ccm_future_complete_block_case('Q', [value unsignedLongLongValue]) ccm_future_complete_block_case('f', [value floatValue]) ccm_future_complete_block_case('d', [value doubleValue]) ccm_future_complete_block_case('B', [value boolValue]) default: break; } } } }]; }; [self.dispatchQueue sync:^{ [self.blocks addObject:[c copy]]; }]; [self tryExecuteCallbacks]; return self; }
#define ccm_future_success_block_case(type, value) \ case type: \ successBlock(value); \ break; \
- (instancetype)onSuccess:(void(^)())successBlock { return [self onComplete:^(NSError *error, id value) { if (value) { NSMethodSignature *methodSignature = NSMethodSignatureForBlock(successBlock); if (methodSignature == nil) { successBlock(value); } else { if (methodSignature.numberOfArguments < 2) { successBlock(); return ; } const char type = [methodSignature getArgumentTypeAtIndex:1][0]; switch (type) { ccm_future_success_block_case('@', value) ccm_future_success_block_case('c', [value charValue]) ccm_future_success_block_case('i', [value intValue]) ccm_future_success_block_case('s', [value shortValue]) ccm_future_success_block_case('l', [value longValue]) ccm_future_success_block_case('q', [value longValue]) ccm_future_success_block_case('C', [value unsignedCharValue]) ccm_future_success_block_case('I', [value unsignedIntValue]) ccm_future_success_block_case('S', [value unsignedShortValue]) ccm_future_success_block_case('L', [value unsignedLongValue]) ccm_future_success_block_case('Q', [value unsignedLongLongValue]) ccm_future_success_block_case('f', [value floatValue]) ccm_future_success_block_case('d', [value doubleValue]) ccm_future_success_block_case('B', [value boolValue]) default: break; } } } }]; }
- (instancetype)onFailure:(void(^)(NSError *))failureBlock { return [self onComplete:^(NSError *error, id value) { if (error) { failureBlock(error); } }]; }
- (void)tryExecuteCallbacks { [self.dispatchQueue async:^{ if (self.futureValue == nil) return; __block id value = self.futureValue; [self.blocks enumerateObjectsUsingBlock:^(CCMFutureBlockWrapper block, NSUInteger idx, BOOL *stop) { block(value); }]; [self.blocks removeAllObjects]; }]; }
@end
@implementation CCMFuture (Private)
- (void)success:(id)value { if (self.futureValue != nil) { return; } [self.dispatchQueue async:^{ if (self.futureValue != nil) { return; } if (value == nil) { self.futureValue = [NSNull null]; } else { self.futureValue = value; } [self tryExecuteCallbacks]; }]; }
- (void)failure:(NSError *)error { NSAssert(error != nil, @"error can not be a nil"); if (self.futureValue != nil) { return; } [self.dispatchQueue async:^{ if (self.futureValue != nil) { return; } self.futureValue = error; [self tryExecuteCallbacks]; }]; }
@end
|