├── RWPromise ├── Class │ ├── RWThenable.h │ ├── Internal │ │ ├── timeout.m │ │ ├── map.m │ │ ├── filter.m │ │ ├── then.m │ │ ├── catch.m │ │ ├── finally.m │ │ ├── reduce.m │ │ ├── RWPromise+Error.m │ │ ├── progress.m │ │ ├── after.m │ │ ├── RWPromise+Internal.h │ │ ├── retry.m │ │ ├── all.m │ │ └── race.m │ ├── RWPromise.h │ └── RWPromise.m ├── ViewController.h ├── AppDelegate.h ├── main.m ├── ViewController.m ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── tests.m ├── Info.plist ├── Base.lproj │ ├── Main.storyboard │ └── LaunchScreen.storyboard └── AppDelegate.m ├── RWPromiseKit.podspec ├── RWPromiseTests ├── Info.plist └── RWPromiseTests.m ├── LICENSE ├── .gitignore ├── README.md └── RWPromise.xcodeproj ├── xcuserdata └── yuguo.xcuserdatad │ └── xcschemes │ └── RWPromise.xcscheme └── project.pbxproj /RWPromise/Class/RWThenable.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import 7 | #import "RWPromise.h" 8 | 9 | 10 | @protocol RWThenable 11 | 12 | @property (nonatomic) RWPromiseBlock then; 13 | @end -------------------------------------------------------------------------------- /RWPromise/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // RWPromise 4 | // 5 | // Created by yuguo on 3/12/16. 6 | // Copyright (c) 2016 RW. All rights reserved. 7 | // 8 | 9 | 10 | #import 11 | 12 | 13 | @interface ViewController : UIViewController 14 | 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /RWPromise/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // RWPromise 4 | // 5 | // Created by yuguo on 3/12/16. 6 | // Copyright (c) 2016 RW. All rights reserved. 7 | // 8 | 9 | 10 | #import 11 | 12 | 13 | @interface AppDelegate : UIResponder 14 | 15 | @property (strong, nonatomic) UIWindow *window; 16 | 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /RWPromise/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // RWPromise 4 | // 5 | // Created by yuguo on 3/12/16. 6 | // Copyright (c) 2016 RW. All rights reserved. 7 | // 8 | 9 | 10 | #import 11 | #import "AppDelegate.h" 12 | 13 | 14 | int main(int argc, char * argv[]) { 15 | @autoreleasepool { 16 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /RWPromise/Class/Internal/timeout.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @class RWAllPromise; 9 | 10 | @implementation RWPromise (timeout) 11 | - (RWPromise *(^)(NSTimeInterval))timeout 12 | { 13 | return ^RWPromise *(NSTimeInterval timeInSec) { 14 | __weak RWPromise *newPromise = [RWPromise race:@[self,[RWPromise timer:timeInSec]]]; 15 | return newPromise; 16 | }; 17 | } 18 | @end -------------------------------------------------------------------------------- /RWPromise/Class/Internal/map.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @implementation RWPromise (map) 9 | + (RWPromise *) map:(NSArray *) array :(RWMapFuncBlock) mapFunc 10 | { 11 | NSMutableArray* promises = @[].mutableCopy; 12 | 13 | [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) { 14 | [promises addObject:mapFunc(obj)]; 15 | }]; 16 | return [RWPromise all:promises]; 17 | } 18 | @end 19 | -------------------------------------------------------------------------------- /RWPromise/Class/Internal/filter.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @implementation RWPromise (filter) 9 | + (RWPromise *) filter:(NSArray *) array :(RWFilterFuncBlock) filterFunc 10 | { 11 | NSMutableArray* promises = @[].mutableCopy; 12 | 13 | [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) { 14 | if (filterFunc(obj)) { 15 | [promises addObject:[RWPromise resolve:obj]]; 16 | } 17 | }]; 18 | return [RWPromise all:promises]; 19 | } 20 | @end -------------------------------------------------------------------------------- /RWPromise/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // RWPromise 4 | // 5 | // Created by yuguo on 3/12/16. 6 | // Copyright (c) 2016 RW. All rights reserved. 7 | // 8 | 9 | 10 | #import "ViewController.h" 11 | 12 | 13 | @interface ViewController () 14 | 15 | @end 16 | 17 | @implementation ViewController 18 | 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | // Do any additional setup after loading the view, typically from a nib. 23 | } 24 | 25 | - (void)didReceiveMemoryWarning { 26 | [super didReceiveMemoryWarning]; 27 | // Dispose of any resources that can be recreated. 28 | } 29 | 30 | 31 | @end -------------------------------------------------------------------------------- /RWPromise/Class/Internal/then.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @implementation RWPromise (then) 9 | - (RWPromise *(^)(RWRunBlock))then { 10 | __weak RWPromise *wSelf = self; 11 | return ^RWPromise *(RWRunBlock thenBlock) { 12 | __weak RWPromise *newPromise = nil; 13 | newPromise = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 14 | __strong RWPromise* sSelf = wSelf; 15 | resolve(sSelf); 16 | }]; 17 | newPromise.thenBlock = thenBlock; 18 | return newPromise; 19 | }; 20 | } 21 | @end -------------------------------------------------------------------------------- /RWPromise/Class/Internal/catch.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @implementation RWPromise (catch) 9 | - (RWPromise *(^)(RWErrorBlock))catch { 10 | __weak RWPromise *wSelf = self; 11 | return ^RWPromise *(RWErrorBlock catchBlock) { 12 | __weak RWPromise *newPromise = nil; 13 | newPromise = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 14 | __strong RWPromise* sSelf = wSelf; 15 | resolve(sSelf); 16 | }]; 17 | newPromise.catchBlock = catchBlock; 18 | return newPromise; 19 | }; 20 | } 21 | @end -------------------------------------------------------------------------------- /RWPromiseKit.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "RWPromiseKit" 3 | s.version = "0.2.0" 4 | s.license = { :type => 'MIT' } 5 | s.summary = "A light-weighted Promise library for Objective-C" 6 | s.description = "A light-weighted Promise library for Objective-C" 7 | s.homepage = "https://github.com/deput/RWPromiseKit" 8 | 9 | s.author = { "deput" => "canopus4u@outlook.com" } 10 | s.source = { :git => "https://github.com/deput/RWPromiseKit.git", :branch => "master"} 11 | 12 | s.platform = :ios, '7.0' 13 | s.ios.deployment_target = '7.0' 14 | s.requires_arc = true 15 | 16 | s.frameworks = 'Foundation' 17 | s.source_files = "RWPromise/Class/**/*.{h,m}" 18 | 19 | end -------------------------------------------------------------------------------- /RWPromise/Class/Internal/finally.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @implementation RWPromise (finally) 9 | - (void (^)(dispatch_block_t))finally 10 | { 11 | __weak RWPromise *wSelf = self; 12 | return ^(dispatch_block_t runBlock) { 13 | __weak RWPromise *newPromise = nil; 14 | newPromise = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 15 | resolve(wSelf); 16 | }]; 17 | newPromise.thenBlock = ^id(id value){ 18 | runBlock(); 19 | return nil; 20 | }; 21 | newPromise.catchBlock = ^(NSError * error){ 22 | runBlock(); 23 | }; 24 | }; 25 | } 26 | @end 27 | 28 | -------------------------------------------------------------------------------- /RWPromise/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /RWPromise/Class/Internal/reduce.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @implementation RWPromise (reduce) 9 | + (RWPromise *) reduce:(NSArray *) array :(RWReduceFuncBlock) reduceFunc initialValue:(id)initialValue 10 | { 11 | if (array.count == 0) { 12 | return nil; 13 | } 14 | __block RWPromise * p = reduceFunc(array[0],initialValue); 15 | [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 16 | if (idx > 0) { 17 | p = p.then(^id(id value){ 18 | return reduceFunc(obj,value); 19 | }); 20 | } 21 | }]; 22 | 23 | return p.then(^id(id res){ 24 | return res; 25 | }); 26 | } 27 | @end -------------------------------------------------------------------------------- /RWPromiseTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | CFBundleIdentifier 7 | $(PRODUCT_BUNDLE_IDENTIFIER) 8 | CFBundleInfoDictionaryVersion 9 | 6.0 10 | CFBundleSignature 11 | ???? 12 | 13 | CFBundleExecutable 14 | $(EXECUTABLE_NAME) 15 | 16 | CFBundleName 17 | $(PRODUCT_NAME) 18 | 19 | CFBundleDevelopmentRegion 20 | en 21 | 22 | CFBundleShortVersionString 23 | 1.0 24 | CFBundleVersion 25 | 1 26 | 27 | CFBundlePackageType 28 | BNDL 29 | 30 | 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Guo Yu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /RWPromise/Class/Internal/RWPromise+Error.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | 9 | static NSString * RWPromiseErrorDomain = @"RWPromiseErrorDomain"; 10 | 11 | @implementation RWPromise (Error) 12 | + (NSError *)errorWithException:(NSException *)exception { 13 | return [NSError errorWithDomain:RWPromiseErrorDomain code:RWPromiseRuntimeError userInfo:@{@"excepiton":exception}]; 14 | } 15 | 16 | + (NSError *)errorOfReject:(NSError *)actualError { 17 | return [NSError errorWithDomain:RWPromiseErrorDomain code:RWPromiseRejectError userInfo:@{@"error":actualError}]; 18 | } 19 | 20 | + (NSError *)errorWithValue:(id)value { 21 | return [NSError errorWithDomain:RWPromiseErrorDomain code:RWPromiseRejectError userInfo:@{@"value":value}]; 22 | } 23 | 24 | + (NSError *)errorWithReason:(NSString *)reason { 25 | return [NSError errorWithDomain:RWPromiseErrorDomain code:RWPromiseRejectError userInfo:@{@"reason":reason}]; 26 | } 27 | 28 | + (NSError *)errorWithUserInfo:(NSDictionary *)userInfo { 29 | return [NSError errorWithDomain:RWPromiseErrorDomain code:RWPromiseRejectError userInfo:userInfo]; 30 | } 31 | 32 | @end -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata 19 | 20 | ## Other 21 | *.xccheckout 22 | *.moved-aside 23 | *.xcuserstate 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | 30 | # CocoaPods 31 | # 32 | # We recommend against adding the Pods directory to your .gitignore. However 33 | # you should judge for yourself, the pros and cons are mentioned at: 34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 35 | # 36 | # Pods/ 37 | 38 | # Carthage 39 | # 40 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 41 | # Carthage/Checkouts 42 | 43 | Carthage/Build 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md 51 | 52 | fastlane/report.xml 53 | fastlane/screenshots 54 | -------------------------------------------------------------------------------- /RWPromise/tests.m: -------------------------------------------------------------------------------- 1 | #import "RWPromise.h" 2 | #import "RWThenable.h" 3 | 4 | 5 | @interface MyObject :NSObject 6 | @end 7 | 8 | @implementation MyObject 9 | 10 | - (RWPromiseBlock)then { 11 | return ^(ResolveHandler resolve, RejectHandler reject) { 12 | 13 | }; 14 | } 15 | 16 | 17 | @end 18 | 19 | __unused __attribute__((constructor)) static void _() { 20 | // @autoreleasepool { 21 | // [RWProgressPromise promise:^(ResolveHandler resolve, RejectHandler reject, ProgressHandler progress) { 22 | // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 23 | // progress(0.5,@"Hi"); 24 | // 25 | // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 26 | // progress(0.5,@"BYE"); 27 | // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 28 | // resolve(@"Over"); 29 | // }); 30 | // }); 31 | // }); 32 | // }].progress(^(double propotion, id value){ 33 | // 34 | // NSLog(@"%@",value); 35 | // }).then(^id(id value){ 36 | // NSLog(@"%@",value); 37 | // return nil; 38 | // }); 39 | // } 40 | 41 | } -------------------------------------------------------------------------------- /RWPromise/Class/Internal/progress.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @implementation RWProgressPromise 9 | { 10 | //ProgressHandler _progressHandler; 11 | } 12 | 13 | + (RWProgressPromise *)promise:(RWProgressPromiseBlock)block 14 | { 15 | RWProgressPromise* progressPromise = [[RWProgressPromise alloc] init]; 16 | __weak RWProgressPromise* weakPromise = progressPromise; 17 | 18 | [progressPromise privateInitialize]; 19 | [progressPromise setProgressHandler: ^(double proportion, id value){ 20 | weakPromise.progressBlock(proportion, value); 21 | }]; 22 | 23 | progressPromise.promiseBlock = ^(ResolveHandler resolve, RejectHandler reject){ 24 | block(resolve,reject,weakPromise.progressHandler); 25 | }; 26 | [progressPromise run]; 27 | 28 | return progressPromise; 29 | } 30 | 31 | - (RWPromise *(^)(ProgressHandler)) progress 32 | { 33 | __weak RWProgressPromise *wSelf = self; 34 | return ^RWPromise *(ProgressHandler progressBlock){ 35 | wSelf.progressBlock = progressBlock; 36 | return wSelf; 37 | }; 38 | } 39 | 40 | - (void) setProgressHandler:(ProgressHandler)progressHandler 41 | { 42 | _progressHandler = progressHandler; 43 | } 44 | 45 | - (void) progress:(double) proportion :(id)value 46 | { 47 | self.progressHandler(proportion,value); 48 | } 49 | @end -------------------------------------------------------------------------------- /RWPromise/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | CFBundleIdentifier 7 | $(PRODUCT_BUNDLE_IDENTIFIER) 8 | CFBundleInfoDictionaryVersion 9 | 6.0 10 | CFBundleSignature 11 | ???? 12 | 13 | CFBundleExecutable 14 | $(EXECUTABLE_NAME) 15 | 16 | CFBundleName 17 | $(PRODUCT_NAME) 18 | 19 | CFBundleDevelopmentRegion 20 | en 21 | 22 | CFBundleShortVersionString 23 | 1.0 24 | CFBundleVersion 25 | 1 26 | 27 | CFBundlePackageType 28 | APPL 29 | 30 | LSRequiresIPhoneOS 31 | 32 | UIRequiredDeviceCapabilities 33 | 34 | armv7 35 | 36 | UILaunchStoryboardName 37 | LaunchScreen 38 | 39 | UISupportedInterfaceOrientations 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationLandscapeLeft 43 | UIInterfaceOrientationLandscapeRight 44 | 45 | UIMainStoryboardFile 46 | Main 47 | 48 | -------------------------------------------------------------------------------- /RWPromise/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /RWPromise/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /RWPromise/Class/Internal/after.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @interface RWAfterPromise : RWPromise 9 | 10 | @end 11 | 12 | @implementation RWAfterPromise 13 | 14 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 15 | if ([keyPath isEqualToString:@"state"]) { 16 | RWPromiseState newState = [change[NSKeyValueChangeNewKey] unsignedIntegerValue]; 17 | if (newState == RWPromiseStateRejected) { 18 | [object removeObserver:self forKeyPath:@"state"]; 19 | self.rejectBlock([(RWPromise *) object error]); 20 | } else if (newState == RWPromiseStateResolved) { 21 | [object removeObserver:self forKeyPath:@"state"]; 22 | self.value = [(RWPromise *) object value]; 23 | @try { 24 | if (self.thenBlock) { 25 | self.thenBlock([(RWPromise *) object value]); 26 | } 27 | } @catch (NSException *e) { 28 | self.rejectBlock([RWPromise errorWithException:e]); 29 | } 30 | } 31 | } 32 | } 33 | @end 34 | 35 | @implementation RWPromise (after) 36 | 37 | - (RWPromise *(^)(NSTimeInterval))after { 38 | __weak RWPromise *wSelf = self; 39 | return ^RWPromise *(NSTimeInterval timeInSec) { 40 | __weak RWPromise *newPromise = nil; 41 | newPromise = [[RWAfterPromise alloc] init:^(ResolveHandler resolve, RejectHandler reject) { 42 | resolve(wSelf); 43 | }]; 44 | newPromise.thenBlock = ^id(id value) { 45 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (timeInSec * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 46 | newPromise.resolveBlock(newPromise.value); 47 | }); 48 | return @{@"Timeup" : @(timeInSec)}; 49 | }; 50 | return newPromise; 51 | }; 52 | } 53 | @end -------------------------------------------------------------------------------- /RWPromise/Class/Internal/RWPromise+Internal.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise.h" 7 | 8 | #define STATE_PROTECT if (sSelf.state != RWPromiseStatePending) return; 9 | 10 | typedef NS_ENUM(NSUInteger, RWPromiseState) { 11 | RWPromiseStatePending = 0, 12 | RWPromiseStateResolved = 1, 13 | RWPromiseStateRejected = 2 14 | }; 15 | 16 | @interface RWPromise () 17 | 18 | @property(nonatomic) id value; 19 | 20 | @property(nonatomic) NSError *error; 21 | 22 | @property(atomic, assign) RWPromiseState state; 23 | 24 | @property(nonatomic, strong) id strongSelf; 25 | 26 | @property(nonatomic, strong) RWPromise *depPromise; 27 | 28 | @property(nonatomic, copy) ResolveHandler resolveBlock; 29 | 30 | @property(nonatomic, copy) RejectHandler rejectBlock; 31 | 32 | @property(nonatomic, copy) RWPromiseBlock promiseBlock; 33 | 34 | @property(nonatomic, copy) RejectHandler catchBlock; 35 | 36 | @property(nonatomic, copy) RWRunBlock thenBlock; 37 | 38 | @property(nonatomic, copy) NSString *identifier; 39 | 40 | @property(nonatomic, strong) id valueKeptForRetry; 41 | 42 | - (instancetype)init:(RWPromiseBlock)initBlock; 43 | 44 | - (void)keepAlive; 45 | 46 | - (void)loseControl; 47 | 48 | - (void)run; 49 | 50 | - (void) privateInitialize; 51 | @end 52 | 53 | 54 | typedef NS_ENUM(NSUInteger, RWPromiseErrorCode) { 55 | RWPromiseNoError = 0, 56 | RWPromiseRuntimeError = 1, 57 | RWPromiseRejectError= 2 58 | }; 59 | 60 | @interface RWPromise (Error) 61 | 62 | + (NSError *) errorWithException:(NSException *)exception; 63 | + (NSError *) errorOfReject:(NSError *)actualError; 64 | + (NSError *) errorWithValue:(id)value; 65 | + (NSError *) errorWithReason:(NSString *)reason; 66 | + (NSError *) errorWithUserInfo:(NSDictionary *)userInfo; 67 | @end 68 | 69 | @interface RWProgressPromise () 70 | 71 | @property(nonatomic, copy) ProgressHandler progressBlock; 72 | 73 | - (void) setProgressHandler:(ProgressHandler)progressHandler; 74 | @end 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /RWPromise/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // RWPromise 4 | // 5 | // Created by yuguo on 3/12/16. 6 | // Copyright (c) 2016 RW. All rights reserved. 7 | // 8 | 9 | 10 | #import "AppDelegate.h" 11 | 12 | 13 | @interface AppDelegate () 14 | 15 | @end 16 | 17 | @implementation AppDelegate 18 | 19 | 20 | 21 | 22 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 23 | // Override point for customization after application launch. 24 | return YES; 25 | } 26 | 27 | - (void)applicationWillResignActive:(UIApplication *)application { 28 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 29 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 30 | 31 | } 32 | 33 | - (void)applicationDidEnterBackground:(UIApplication *)application { 34 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 35 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 36 | 37 | } 38 | 39 | - (void)applicationWillEnterForeground:(UIApplication *)application { 40 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 41 | } 42 | 43 | - (void)applicationDidBecomeActive:(UIApplication *)application { 44 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 45 | } 46 | 47 | - (void)applicationWillTerminate:(UIApplication *)application { 48 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 49 | } 50 | 51 | 52 | @end -------------------------------------------------------------------------------- /RWPromise/Class/RWPromise.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import 7 | 8 | typedef id (^RWRunBlock )(id value); 9 | 10 | typedef void (^ResolveHandler )(id value); 11 | 12 | typedef void (^RWErrorBlock )(NSError *error); 13 | 14 | typedef RWErrorBlock RejectHandler; 15 | 16 | typedef void (^RWPromiseBlock )(ResolveHandler resolve, RejectHandler reject); 17 | 18 | typedef void (^ProgressHandler )(double proportion, id value); 19 | 20 | NSError* promiseErrorWithReason(NSString* reason); 21 | 22 | typedef void (^RWProgressPromiseBlock )(ResolveHandler resolve, RejectHandler reject, ProgressHandler progress); 23 | 24 | @interface RWPromise : NSObject 25 | + (RWPromise *)promise:(RWPromiseBlock)block; 26 | 27 | + (RWPromise *)timer:(NSTimeInterval)timeInSec; 28 | 29 | + (RWPromise *)resolve:(id)value; 30 | 31 | + (RWPromise *)reject:(id)value; 32 | 33 | - (void) resolve:(id)value; 34 | 35 | - (void) reject:(NSError *)error; 36 | 37 | @end 38 | 39 | @interface RWPromise (all) 40 | + (RWPromise *)all:(NSArray *)promises; 41 | @end 42 | 43 | @interface RWPromise (race) 44 | + (RWPromise *)race:(NSArray *)promises; 45 | @end 46 | 47 | @interface RWPromise (then) 48 | - (RWPromise *(^)(RWRunBlock))then; 49 | @end 50 | 51 | @interface RWPromise (catch) 52 | - (RWPromise *(^)(RWErrorBlock))catch; 53 | @end 54 | 55 | @interface RWPromise (after) 56 | - (RWPromise *(^)(NSTimeInterval))after; 57 | @end 58 | 59 | @interface RWPromise (finally) 60 | - (void (^)(dispatch_block_t))finally; 61 | @end 62 | 63 | @interface RWPromise (timeout) 64 | - (RWPromise *(^)(NSTimeInterval))timeout; 65 | @end 66 | 67 | @interface RWPromise (retry) 68 | - (RWPromise *(^)(NSUInteger))retry; 69 | @end 70 | 71 | typedef RWPromise* (^RWMapFuncBlock )(id value); 72 | 73 | @interface RWPromise (map) 74 | + (RWPromise *) map:(NSArray *) array :(RWMapFuncBlock) mapFunc; 75 | @end 76 | 77 | typedef BOOL (^RWFilterFuncBlock )(id value); 78 | 79 | @interface RWPromise (filter) 80 | + (RWPromise *) filter:(NSArray *) array :(RWFilterFuncBlock) filterFunc; 81 | @end 82 | 83 | typedef RWPromise* (^RWReduceFuncBlock )(id item, id acc); 84 | 85 | @interface RWPromise (reduce) 86 | + (RWPromise *) reduce:(NSArray *) array :(RWReduceFuncBlock) reduceFunc initialValue:(id)initialValue; 87 | @end 88 | 89 | @interface RWProgressPromise : RWPromise 90 | 91 | + (RWProgressPromise *)promise:(RWProgressPromiseBlock)block; 92 | 93 | @property(nonatomic, readonly) ProgressHandler progressHandler; 94 | 95 | - (RWPromise *(^)(ProgressHandler)) progress; 96 | 97 | - (void) progress:(double) proportion :(id)value; 98 | @end 99 | 100 | 101 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RWPromiseKit 2 | 3 | Desiciption 4 | ------- 5 | 6 | A light-weighted Promise library for Objective-C 7 | 8 | #### About Promise 9 | 10 | >The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future. 11 | [Ref](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) 12 | 13 | In JavaScript, `Promise` handles asynchronising call beatifully: 14 | 15 | 16 | ```js 17 | getJSON("/posts.json").then(function(posts) { 18 | // ... 19 | consume(posts); 20 | }).catch(function(error) { 21 | console.log('something wrong!', error); 22 | }); 23 | ``` 24 | 25 | Usage of RWPromiseKit 26 | ------- 27 | Here is a basic example: 28 | ```objc 29 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 30 | resolve(@"result"); 31 | }]; 32 | p1.then(^id(NSString* value){ 33 | NSLog(@"%@",value); //result 34 | return @"resultOfThen"; 35 | }).then(^id(NSString* value){ 36 | NSLog(@"%@",value); //resultOfThen 37 | NSException *e = [NSException exceptionWithName:@"name" 38 | reason:@"reason" 39 | userInfo:@{}]; 40 | @throw e; 41 | return nil; 42 | }).catch(^(NSError* error){ 43 | NSLog(@"%@",[error description]); //error contains exception 44 | }); 45 | ``` 46 | Using RWPromise is exactly same as using promise in js. `resolve` and `reject` are provided in initial block as input parameters, these two methods are used to change the state of a promise. Block passed in `then` will be invoked when a promise is set to resolved while one in `catch` will be invoked when rejected. 47 | 48 | For more infomation about the API of promise in js, please reference [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) 49 | 50 | ### Suppoted API 51 | #### 0.1.0 52 | - `then` 53 | - `catch` 54 | - `finally` 55 | - `after` 56 | - `retry` 57 | - `timeout` 58 | - `map` 59 | - `filter` 60 | - `reduce` 61 | - `race` 62 | - `all` 63 | - `resolve` 64 | - `reject` 65 | 66 | #### 0.2.0 67 | - `progress` 68 | 69 | 70 | 71 | Installation 72 | ------- 73 | - Cocoapods 74 | ``` 75 | pod 'RWPromiseKit', '0.2.0' 76 | ``` 77 | 78 | - Source code 79 | 80 | Copy all source files from directory `Class` to your project 81 | 82 | Issues and Todo list 83 | ------- 84 | - I simplify the usage of `then` compared with js. Just pass only one handler block to handle when last promise is resolved. To reject, you can raise an expection or return a new promise. 85 | 86 | - ~~some other API: `map`,`filter`,`reduce`~~ 87 | - Integrate with 3rd party lib 88 | - ~~Unit test are not finished~~ 89 | - Complicated test cases. 90 | - Doc with more detail 91 | 92 | 93 | License 94 | ------- 95 | 96 | Licensed under MIT. [Full license here »](LICENSE) 97 | 98 | -------------------------------------------------------------------------------- /RWPromise/Class/Internal/retry.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @interface RWRetryPromise : RWPromise 9 | 10 | @end 11 | 12 | @implementation RWRetryPromise 13 | 14 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 15 | if ([keyPath isEqualToString:@"state"]) { 16 | RWPromiseState newState = [change[NSKeyValueChangeNewKey] unsignedIntegerValue]; 17 | if (newState == RWPromiseStateRejected) { 18 | [object removeObserver:self forKeyPath:@"state"]; 19 | if (self.catchBlock) { 20 | self.catchBlock([(RWPromise *) object error]); 21 | } else { 22 | self.rejectBlock([(RWPromise *) object error]); 23 | } 24 | } else if (newState == RWPromiseStateResolved) { 25 | [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 26 | } 27 | } 28 | } 29 | 30 | @end 31 | 32 | @implementation RWPromise (retry) 33 | - (RWPromise *(^)(NSUInteger))retry { 34 | __weak RWPromise *wSelf = self; 35 | return ^RWPromise *(NSUInteger retryCount) { 36 | RWPromise *newPromise = nil; 37 | newPromise = [[RWRetryPromise alloc] init:^(ResolveHandler resolve, RejectHandler reject) { 38 | __strong RWPromise *sSelf = wSelf; 39 | resolve(sSelf); 40 | }]; 41 | 42 | BOOL thenBlock = NO; 43 | id block = self.promiseBlock; 44 | if (self.thenBlock != nil) { 45 | block = self.thenBlock; 46 | thenBlock = YES; 47 | } 48 | 49 | __weak RWPromise* wPromise = newPromise; 50 | 51 | newPromise.catchBlock = ^(NSError *e){ 52 | static NSUInteger retried = 0; 53 | if (retried++ < retryCount){ 54 | if (thenBlock) { 55 | @autoreleasepool { 56 | __weak RWPromise *retryPromise = nil; 57 | //dispatch_async(dispatch_get_main_queue(), ^{ 58 | retryPromise = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 59 | @try { 60 | id v = ((RWRunBlock)block)(wSelf.valueKeptForRetry); 61 | resolve(v); 62 | } @catch (NSException *exception) { 63 | reject([RWPromise errorWithException:exception]); 64 | } 65 | }]; 66 | wPromise.resolveBlock(retryPromise); 67 | } 68 | }else{ 69 | RWPromise *retryPromise = nil; 70 | retryPromise = [RWPromise promise:block]; 71 | wPromise.resolveBlock(retryPromise); 72 | } 73 | }else{ 74 | wPromise.rejectBlock(e); 75 | } 76 | }; 77 | return newPromise; 78 | }; 79 | } 80 | @end -------------------------------------------------------------------------------- /RWPromise/Class/Internal/all.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @interface RWAllPromise : RWPromise 9 | - (instancetype)initWithAllPromises:(NSArray *)promises; 10 | 11 | @property(nonatomic, strong) NSMutableSet *promises; 12 | @end 13 | 14 | @implementation RWAllPromise { 15 | NSMutableArray *_values; 16 | } 17 | 18 | - (instancetype)initWithAllPromises:(NSArray *)promises { 19 | self = [super init]; 20 | _values = @[].mutableCopy; 21 | self.promises = [NSMutableSet set]; 22 | self.state = RWPromiseStatePending; 23 | [self keepAlive]; 24 | 25 | [promises enumerateObjectsUsingBlock:^(RWPromise *promise, NSUInteger idx, BOOL *stop) { 26 | [promise addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil]; 27 | if (promise.state == RWPromiseStatePending) { 28 | [self.promises addObject:promise]; 29 | } 30 | }]; 31 | 32 | __weak RWPromise *wSelf = self; 33 | self.resolveBlock = ^(id value) { 34 | __strong RWPromise *sSelf = wSelf; 35 | STATE_PROTECT; 36 | if ([value isKindOfClass:[RWPromise class]]) { 37 | 38 | if (((RWPromise *) value).state == RWPromiseStatePending) { 39 | sSelf.depPromise = value; 40 | [(RWPromise*)value addObserver:sSelf forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil]; 41 | } else { 42 | [(RWPromise*)value addObserver:sSelf forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil]; 43 | } 44 | } else { 45 | sSelf.value = value; 46 | sSelf.state = RWPromiseStateResolved; 47 | [sSelf loseControl]; 48 | } 49 | }; 50 | 51 | self.rejectBlock = ^(NSError *error) { 52 | __strong RWPromise *sSelf = wSelf; 53 | STATE_PROTECT; 54 | [sSelf loseControl]; 55 | sSelf.error = error; 56 | NSLog(@"%@-%@", sSelf, [error description]); 57 | sSelf.state = RWPromiseStateRejected; 58 | }; 59 | 60 | return self; 61 | } 62 | 63 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 64 | if ([keyPath isEqualToString:@"state"]) { 65 | RWPromiseState newState = [change[NSKeyValueChangeNewKey] unsignedIntegerValue]; 66 | [object removeObserver:self forKeyPath:@"state"]; 67 | [self.promises removeObject:object]; 68 | if (newState == RWPromiseStateRejected) { 69 | [self.promises enumerateObjectsUsingBlock:^(RWPromise *promise, BOOL *stop) { 70 | [promise removeObserver:self forKeyPath:@"state"]; 71 | }]; 72 | self.rejectBlock([(RWPromise *) object error]); 73 | } else if (newState == RWPromiseStateResolved) { 74 | //[object removeObserver:self forKeyPath:@"state"]; 75 | [_values addObject:[(RWPromise *) object value]]; 76 | } 77 | 78 | if (self.promises.count == 0) { 79 | self.resolveBlock(_values); 80 | } 81 | } 82 | } 83 | 84 | @end 85 | 86 | @implementation RWPromise (all) 87 | 88 | + (RWPromise *)all:(NSArray *)promises { 89 | return [[RWAllPromise alloc] initWithAllPromises:promises]; 90 | } 91 | 92 | @end -------------------------------------------------------------------------------- /RWPromise/Class/Internal/race.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise+Internal.h" 7 | 8 | @interface RWRacePromise : RWPromise 9 | - (instancetype)initWithRacePromises:(NSArray *)promises; 10 | 11 | @property(nonatomic, strong) NSMutableSet *promises; 12 | @end 13 | 14 | @implementation RWRacePromise { 15 | NSMutableArray *_values; 16 | //NSMutableDictionary *_orders; 17 | } 18 | 19 | - (instancetype)initWithRacePromises:(NSArray *)promises { 20 | self = [super init]; 21 | self.promises = [NSMutableSet set]; 22 | _values = @[].mutableCopy; 23 | //_orders = @{}.mutableCopy; 24 | self.state = RWPromiseStatePending; 25 | [self keepAlive]; 26 | 27 | [promises enumerateObjectsUsingBlock:^(RWPromise *promise, NSUInteger idx, BOOL *stop) { 28 | [promise addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil]; 29 | //_orders[[NSValue valueWithNonretainedObject:promise]] = @(idx); 30 | if (promise.state == RWPromiseStatePending) { 31 | [self.promises addObject:promise]; 32 | } else { 33 | 34 | } 35 | }]; 36 | 37 | __weak RWPromise *wSelf = self; 38 | self.resolveBlock = ^(id value) { 39 | __strong RWPromise *sSelf = wSelf; 40 | STATE_PROTECT; 41 | if ([value isKindOfClass:[RWPromise class]]) { 42 | 43 | if (((RWPromise *) value).state == RWPromiseStatePending) { 44 | sSelf.depPromise = value; 45 | [(RWPromise*)value addObserver:sSelf forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil]; 46 | } else { 47 | //sSelf.depPromise = value; 48 | [(RWPromise*)value addObserver:sSelf forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil]; 49 | //[sSelf loseControl]; 50 | } 51 | } else { 52 | sSelf.value = value; 53 | sSelf.state = RWPromiseStateResolved; 54 | [sSelf loseControl]; 55 | } 56 | }; 57 | 58 | self.rejectBlock = ^(NSError *error) { 59 | __strong RWPromise *sSelf = wSelf; 60 | STATE_PROTECT; 61 | [sSelf loseControl]; 62 | sSelf.error = error; 63 | NSLog(@"%@-%@", sSelf, [error description]); 64 | sSelf.state = RWPromiseStateRejected; 65 | }; 66 | 67 | return self; 68 | } 69 | 70 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 71 | if ([keyPath isEqualToString:@"state"]) { 72 | RWPromiseState newState = [change[NSKeyValueChangeNewKey] unsignedIntegerValue]; 73 | [object removeObserver:self forKeyPath:@"state"]; 74 | [self.promises removeObject:object]; 75 | if (newState == RWPromiseStateResolved) { 76 | [self.promises enumerateObjectsUsingBlock:^(RWPromise *promise, BOOL *stop) { 77 | [promise removeObserver:self forKeyPath:@"state"]; 78 | }]; 79 | self.resolveBlock([(RWPromise *) object value]); 80 | } else if (newState == RWPromiseStateRejected) { 81 | [_values addObject:[(RWPromise *) object error]]; 82 | if (self.promises.count == 0) { 83 | self.rejectBlock([RWPromise errorWithUserInfo:@{@"reason":@"No promise wins the race",@"errors":_values}]); 84 | } 85 | } 86 | } 87 | } 88 | 89 | @end 90 | 91 | @implementation RWPromise (race) 92 | 93 | + (RWPromise *)race:(NSArray *)promises { 94 | return [[RWRacePromise alloc] initWithRacePromises:promises]; 95 | } 96 | 97 | @end -------------------------------------------------------------------------------- /RWPromise.xcodeproj/xcuserdata/yuguo.xcuserdatad/xcschemes/RWPromise.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 64 | 66 | 72 | 73 | 74 | 75 | 76 | 77 | 83 | 84 | 90 | 91 | 92 | 93 | 95 | 96 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /RWPromise/Class/RWPromise.m: -------------------------------------------------------------------------------- 1 | // 2 | // Created by deput on 3/12/16. 3 | // Copyright (c) 2016 RW. All rights reserved. 4 | // 5 | 6 | #import "RWPromise.h" 7 | #import "RWPromise+Internal.h" 8 | #import "RWThenable.h" 9 | 10 | @implementation RWPromise 11 | 12 | #pragma mark - Class Methods 13 | 14 | + (RWPromise *)timer:(NSTimeInterval)timeInSec { 15 | return [self promise:^(ResolveHandler resolve, RejectHandler reject) { 16 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (timeInSec * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 17 | resolve(@{@"Timeout" : @(timeInSec)}); 18 | }); 19 | }]; 20 | } 21 | 22 | + (RWPromise *)promise:(RWPromiseBlock)block { 23 | return [[RWPromise alloc] init:block]; 24 | } 25 | 26 | + (RWPromise *)resolve:(id)value { 27 | if ([value isKindOfClass:[RWPromise class]]) { 28 | return value; 29 | } else if ([value conformsToProtocol:@protocol(RWThenable)]) { 30 | return [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 31 | id thenableObj = (id ) value; 32 | thenableObj.then(resolve, reject); 33 | }]; 34 | } else { 35 | return [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 36 | resolve(value); 37 | }]; 38 | } 39 | } 40 | 41 | + (RWPromise *)reject:(id)value { 42 | if ([value isKindOfClass:[RWPromise class]]) { 43 | return value; 44 | } else { 45 | return [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 46 | reject([RWPromise errorWithValue:value]); 47 | }]; 48 | } 49 | } 50 | 51 | #pragma mark - Instance Methods 52 | 53 | - (instancetype)init:(RWPromiseBlock)initBlock { 54 | self = [super init]; 55 | 56 | if (self) { 57 | [self privateInitialize]; 58 | self.promiseBlock = initBlock; 59 | } 60 | 61 | [self run]; 62 | return self; 63 | } 64 | 65 | - (void) privateInitialize 66 | { 67 | if (self) { 68 | #ifdef DEBUG 69 | static int i = 0; 70 | i++; 71 | self.identifier = [@(i) stringValue]; 72 | NSLog(@"%@th promise", self.identifier); 73 | #endif 74 | self.state = RWPromiseStatePending; 75 | [self keepAlive]; 76 | 77 | __weak RWPromise *wSelf = self; 78 | self.resolveBlock = ^(id value) { 79 | __strong RWPromise *sSelf = wSelf; 80 | STATE_PROTECT; 81 | if ([value isKindOfClass:[RWPromise class]]) { 82 | if (((RWPromise *) value).state == RWPromiseStatePending) { 83 | sSelf.depPromise = value; 84 | } 85 | [(RWPromise *) value addObserver:sSelf forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil]; 86 | } else { 87 | sSelf.value = value; 88 | sSelf.state = RWPromiseStateResolved; 89 | [sSelf loseControl]; 90 | } 91 | }; 92 | 93 | self.rejectBlock = ^(NSError *error) { 94 | __strong RWPromise *sSelf = wSelf; 95 | STATE_PROTECT; 96 | [sSelf loseControl]; 97 | sSelf.error = error; 98 | #ifdef DEBUG 99 | NSLog(@"%@-%@", sSelf, [error description]); 100 | #endif 101 | sSelf.state = RWPromiseStateRejected; 102 | }; 103 | } 104 | } 105 | 106 | - (void)keepAlive { 107 | self.strongSelf = self; 108 | } 109 | 110 | - (void)loseControl { 111 | self.strongSelf = nil; 112 | } 113 | 114 | - (void)dealloc { 115 | #ifdef DEBUG 116 | NSLog(@"%@th promise dealloc", self.identifier); 117 | #endif 118 | self.state = self.state; 119 | self.depPromise = nil; 120 | } 121 | 122 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 123 | if ([keyPath isEqualToString:@"state"]) { 124 | RWPromiseState newState = [change[NSKeyValueChangeNewKey] unsignedIntegerValue]; 125 | if (newState == RWPromiseStateRejected) { 126 | [object removeObserver:self forKeyPath:@"state"]; 127 | if (self.catchBlock) { 128 | self.catchBlock([(RWPromise *) object error]); 129 | self.resolveBlock(nil); 130 | } else { 131 | self.rejectBlock([(RWPromise *) object error]); 132 | } 133 | } else if (newState == RWPromiseStateResolved) { 134 | [object removeObserver:self forKeyPath:@"state"]; 135 | @try { 136 | id value = nil; 137 | self.valueKeptForRetry = [(RWPromise *) object value]; 138 | if (self.thenBlock) { 139 | value = self.thenBlock([(RWPromise *) object value]); 140 | } else { 141 | value = [(RWPromise *) object value]; 142 | } 143 | self.thenBlock = nil; 144 | self.resolveBlock(value); 145 | } @catch (NSException *e) { 146 | self.rejectBlock([RWPromise errorWithException:e]); 147 | } 148 | } 149 | } 150 | } 151 | 152 | - (void)run { 153 | if (self.promiseBlock) { 154 | @try { 155 | self.promiseBlock(self.resolveBlock, self.rejectBlock); 156 | } @catch (NSException *e) { 157 | self.rejectBlock([RWPromise errorWithException:e]); 158 | } 159 | } 160 | } 161 | 162 | - (void) resolve:(id)value 163 | { 164 | self.resolveBlock(value); 165 | } 166 | 167 | - (void) reject:(NSError *)error 168 | { 169 | self.rejectBlock(error); 170 | } 171 | @end 172 | 173 | NSError *promiseErrorWithReason(NSString *reason) { 174 | return [RWPromise errorWithReason:reason]; 175 | } 176 | -------------------------------------------------------------------------------- /RWPromiseTests/RWPromiseTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // RWPromiseTests.m 3 | // RWPromiseTests 4 | // 5 | // Created by yuguo on 3/12/16. 6 | // Copyright (c) 2016 RW. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "RWPromise.h" 11 | 12 | @interface RWPromiseTests : XCTestCase 13 | 14 | @end 15 | 16 | @implementation RWPromiseTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown { 24 | // Put teardown code here. This method is called after the invocation of each test method in the class. 25 | [super tearDown]; 26 | } 27 | 28 | - (void)testExample { 29 | // This is an example of a functional test case. 30 | // Use XCTAssert and related functions to verify your tests produce the correct results. 31 | } 32 | 33 | - (void)testPerformanceExample { 34 | // This is an example of a performance test case. 35 | [self measureBlock:^{ 36 | // Put the code you want to measure the time of here. 37 | }]; 38 | } 39 | 40 | 41 | - (void) testPromizeLifeCycle 42 | { 43 | __weak id object = nil; 44 | @autoreleasepool { 45 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 46 | 47 | }]; 48 | object = p1; 49 | } 50 | 51 | XCTAssertNotNil(object,@"Promise won't be dealloced until resolved"); 52 | 53 | @autoreleasepool { 54 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 55 | resolve(nil); 56 | }]; 57 | object = p1; 58 | } 59 | 60 | XCTAssertNil(object,@"Promise will be dealloced until resolved"); 61 | 62 | object = @"Not nil"; 63 | @autoreleasepool { 64 | RWPromise* p1 = [RWProgressPromise promise:^(ResolveHandler resolve, RejectHandler reject, ProgressHandler progress) { 65 | resolve(nil); 66 | }].progress(^(double proportion, id value){ 67 | 68 | }); 69 | object = p1; 70 | } 71 | 72 | XCTAssertNil(object,@"Promise will be dealloced until resolved"); 73 | } 74 | 75 | - (void) testThen 76 | { 77 | NSString* expectedRes = @"res"; 78 | __block NSString* result = nil; 79 | @autoreleasepool { 80 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 81 | resolve(expectedRes); 82 | }]; 83 | p1.then(^id(id value){ 84 | result = value; 85 | return @"Done"; 86 | }); 87 | } 88 | 89 | XCTAssertEqual(result, expectedRes); 90 | 91 | @autoreleasepool { 92 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 93 | resolve(@"1"); 94 | }]; 95 | p1 96 | .then(^id(NSString* value){ 97 | return [value stringByAppendingString:@"2"]; 98 | }) 99 | .then(^id(NSString* value){ 100 | return [value stringByAppendingString:@"3"]; 101 | }) 102 | .then(^id(NSString* value){ 103 | result = value; 104 | return nil; 105 | }); 106 | } 107 | 108 | XCTAssertTrue([result isEqualToString:@"123"]); 109 | } 110 | 111 | - (void) testCatch{ 112 | __block NSError* err = nil; 113 | 114 | @autoreleasepool { 115 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 116 | reject([NSError errorWithDomain:@"mydomain" code:1 userInfo:nil]); 117 | }]; 118 | p1 119 | .catch(^(NSError* error){ 120 | err = error; 121 | }); 122 | } 123 | 124 | XCTAssertTrue([err.domain isEqualToString:@"mydomain"]); 125 | 126 | err = nil; 127 | @autoreleasepool { 128 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 129 | reject([NSError errorWithDomain:@"mydomain" code:1 userInfo:nil]); 130 | }]; 131 | p1 132 | .then(^id(NSString* value){ 133 | return [value stringByAppendingString:@"2"]; 134 | }) 135 | .then(^id(NSString* value){ 136 | return [value stringByAppendingString:@"3"]; 137 | }) 138 | .catch(^(NSError* error){ 139 | err = error; 140 | }); 141 | } 142 | 143 | XCTAssertTrue([err.domain isEqualToString:@"mydomain"]); 144 | 145 | 146 | err = nil; 147 | 148 | __block id res = @"might be nil or not"; 149 | @autoreleasepool { 150 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 151 | reject([NSError errorWithDomain:@"mydomain" code:1 userInfo:nil]); 152 | }]; 153 | p1 154 | .then(^id(NSString* value){ 155 | return [value stringByAppendingString:@"2"]; 156 | }) 157 | .catch(^(NSError* error){ 158 | err = error; 159 | }) 160 | .then(^id(id value){ 161 | res = value; 162 | return nil; 163 | }); 164 | } 165 | 166 | XCTAssertTrue([err.domain isEqualToString:@"mydomain"]); 167 | XCTAssertNil(res); 168 | } 169 | 170 | - (void) testCatch2 171 | { 172 | __block NSError* err = nil; 173 | @autoreleasepool { 174 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 175 | NSException *e = [NSException 176 | exceptionWithName:@"name" 177 | reason:@"reason" 178 | userInfo:@{}]; 179 | @throw e; 180 | }]; 181 | p1 182 | .catch(^(NSError* error){ 183 | err = error; 184 | }); 185 | } 186 | XCTAssertTrue([[[err userInfo][@"excepiton"] name] isEqualToString:@"name"],@"Should catch the exception"); 187 | 188 | 189 | @autoreleasepool { 190 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 191 | resolve(nil); 192 | }]; 193 | p1.then(^id(id value){ 194 | NSString* s = @"s"; 195 | [s doesNotRecognizeSelector:@selector(addObject:)]; 196 | return nil; 197 | }) 198 | .catch(^(NSError* error){ 199 | err = error; 200 | }); 201 | } 202 | XCTAssertTrue([[[err userInfo][@"excepiton"] name] isEqualToString:@"NSInvalidArgumentException"]); 203 | } 204 | 205 | 206 | - (void) testThenNestedPromise{ 207 | __block NSString* result = nil; 208 | @autoreleasepool { 209 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 210 | resolve(@"1"); 211 | }]; 212 | p1.then(^id(NSString* value){ 213 | result = value; 214 | RWPromise* p2 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 215 | resolve(@"2"); 216 | }]; 217 | return p2; 218 | }) 219 | .then(^id(NSString* value){ 220 | result = [result stringByAppendingString:value]; 221 | return nil; 222 | }); 223 | } 224 | 225 | XCTAssertTrue([result isEqualToString:@"12"]); 226 | } 227 | 228 | - (void) testTimeout{ 229 | __weak id object = nil; 230 | @autoreleasepool { 231 | RWPromise* p1 = [RWPromise timer:1]; 232 | p1.then(^id(NSString* value){ 233 | return nil; 234 | }); 235 | object = p1; 236 | } 237 | XCTAssertNotNil(object); 238 | 239 | [NSThread sleepForTimeInterval:1.5]; 240 | // dispatch_semaphore_t sem = dispatch_semaphore_create(0); 241 | // dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC))); 242 | XCTAssertNil(object); 243 | } 244 | 245 | - (void) testAll1 246 | { 247 | __block NSArray* result = nil; 248 | @autoreleasepool { 249 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 250 | resolve(@"1"); 251 | }]; 252 | 253 | RWPromise* p2 = [RWPromise timer:1]; 254 | RWPromise* p3 = [RWPromise timer:2]; 255 | 256 | [RWPromise all:@[p1,p2,p3]].then(^id(NSArray* values){ 257 | result = values; 258 | return nil; 259 | }); 260 | 261 | } 262 | [NSThread sleepForTimeInterval:3]; 263 | XCTAssert([result[0] isEqualToString:@"1"]); 264 | XCTAssert([[result[1] allKeys][0] isEqualToString:@"Timeout"]); 265 | XCTAssert([[result[2] allKeys][0] isEqualToString:@"Timeout"]); 266 | } 267 | 268 | - (void) testAll2 269 | { 270 | __block id result = nil; 271 | @autoreleasepool { 272 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 273 | reject(promiseErrorWithReason(@"on purpose")); 274 | }]; 275 | 276 | RWPromise* p2 = [RWPromise timer:1]; 277 | RWPromise* p3 = [RWPromise timer:2]; 278 | 279 | [RWPromise all:@[p1,p2,p3]].then(^id(NSArray* values){ 280 | result = values; 281 | return nil; 282 | }).catch(^(NSError* error){ 283 | result = error; 284 | }); 285 | 286 | } 287 | [NSThread sleepForTimeInterval:3]; 288 | 289 | XCTAssert([result isKindOfClass:[NSError class]]); 290 | XCTAssert([[result userInfo][@"reason"] isEqualToString:@"on purpose"]); 291 | } 292 | 293 | - (void) testRace1 294 | { 295 | __block NSArray* result = nil; 296 | @autoreleasepool { 297 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 298 | resolve(@"1"); 299 | }]; 300 | 301 | RWPromise* p2 = [RWPromise timer:1]; 302 | RWPromise* p3 = [RWPromise timer:2]; 303 | 304 | [RWPromise race:@[p1,p2,p3]].then(^id(id value){ 305 | result = value; 306 | return nil; 307 | }); 308 | 309 | } 310 | [NSThread sleepForTimeInterval:3]; 311 | XCTAssert([(NSString*)result isEqualToString:@"1"]); 312 | } 313 | 314 | - (void) testRace2 315 | { 316 | __block NSArray* result = nil; 317 | @autoreleasepool { 318 | 319 | RWPromise* p2 = [RWPromise timer:1].then(^id (id value){ 320 | return @"2"; 321 | }); 322 | 323 | RWPromise* p3 = [RWPromise timer:2].then(^id (id value){ 324 | return @"3"; 325 | }); 326 | 327 | [RWPromise race:@[p2,p3]].then(^id(id value){ 328 | result = value; 329 | return nil; 330 | }); 331 | 332 | } 333 | [NSThread sleepForTimeInterval:3]; 334 | XCTAssert([(NSString*)result isEqualToString:@"2"]); 335 | } 336 | 337 | - (void) testRace3 338 | { 339 | __block id result = nil; 340 | @autoreleasepool { 341 | RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 342 | reject(promiseErrorWithReason(@"1")); 343 | }]; 344 | 345 | RWPromise* p2 = [RWPromise timer:1].then(^id (id value){ 346 | NSException *e = [NSException 347 | exceptionWithName:@"name" 348 | reason:@"reason" 349 | userInfo:@{}]; 350 | @throw e; 351 | return @"2"; 352 | }); 353 | 354 | RWPromise* p3 = [RWPromise timer:2].then(^id (id value){ 355 | NSException *e = [NSException 356 | exceptionWithName:@"name" 357 | reason:@"reason" 358 | userInfo:@{}]; 359 | @throw e; 360 | return @"3"; 361 | }); 362 | 363 | [RWPromise race:@[p1,p2,p3]].then(^id(id value){ 364 | result = value; 365 | return nil; 366 | }).catch(^(NSError* error){ 367 | result = error; 368 | }); 369 | 370 | } 371 | [NSThread sleepForTimeInterval:3]; 372 | XCTAssert([result isKindOfClass:[NSError class]]); 373 | } 374 | 375 | - (void) testTimeout2 376 | { 377 | __block id result = nil; 378 | __unused RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 379 | 380 | }].timeout(3).then(^id(id value){ 381 | result = value; 382 | return nil; 383 | }); 384 | XCTAssertNil(result); 385 | [NSThread sleepForTimeInterval:4]; 386 | 387 | XCTAssertEqual([result allKeys][0] , @"Timeout"); 388 | } 389 | 390 | - (void) testAfter 391 | { 392 | __block id result = nil; 393 | __unused RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 394 | resolve(@"1"); 395 | }] 396 | .after(3) 397 | .then(^id(id value){ 398 | result = value; 399 | return nil; 400 | }); 401 | XCTAssertNil(result); 402 | 403 | //[NSThread sleepForTimeInterval:5]; 404 | //XCTAssertEqual(result, @"1"); 405 | } 406 | 407 | 408 | - (void) testFinally 409 | { 410 | __block id result = nil; 411 | @autoreleasepool { 412 | [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 413 | resolve(@"1"); 414 | }].then(^id(id value){ 415 | result = value; 416 | return nil; 417 | }).catch(^(NSError* error){ 418 | 419 | }).finally(^{ 420 | result = @"finally"; 421 | }); 422 | } 423 | 424 | XCTAssertEqual(result, @"finally"); 425 | 426 | @autoreleasepool { 427 | [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 428 | reject(nil); 429 | }].then(^id(id value){ 430 | result = value; 431 | return nil; 432 | }).catch(^(NSError* error){ 433 | 434 | }).finally(^{ 435 | result = @"finally"; 436 | }); 437 | } 438 | 439 | XCTAssertEqual(result, @"finally"); 440 | } 441 | 442 | - (void) testRetry1 443 | { 444 | NSUInteger retryCount = 3; 445 | __block NSMutableArray* res = @[].mutableCopy; 446 | __block NSString* final = nil; 447 | @autoreleasepool { 448 | [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 449 | static NSUInteger triedCount = 0; 450 | triedCount++; 451 | [res addObject:@(triedCount)]; 452 | if (triedCount == retryCount) { 453 | resolve(@"ahha"); 454 | }else{ 455 | reject(promiseErrorWithReason(@"needRetry")); 456 | } 457 | 458 | }] 459 | .retry(retryCount) 460 | .then(^id(id value){ 461 | final = value; 462 | return nil; 463 | }); 464 | } 465 | XCTAssert(res.count == retryCount); 466 | XCTAssertTrue([final isEqualToString:@"ahha"]); 467 | } 468 | 469 | - (void) testRetry2 470 | { 471 | NSUInteger retryCount = 3; 472 | __block NSMutableArray* res = @[].mutableCopy; 473 | __block id final = nil; 474 | @autoreleasepool { 475 | [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 476 | static NSUInteger triedCount = 0; 477 | triedCount++; 478 | [res addObject:@(triedCount)]; 479 | reject(promiseErrorWithReason(@"needRetry")); 480 | }] 481 | .retry(retryCount) 482 | .then(^id(id value){ 483 | final = value; 484 | return nil; 485 | }) 486 | .catch(^(NSError* e){ 487 | final = e; 488 | }); 489 | } 490 | XCTAssert(res.count == retryCount + 1); 491 | XCTAssert([final isKindOfClass:[NSError class]]); 492 | } 493 | 494 | - (void) testRetry3 495 | { 496 | NSUInteger retryCount = 3; 497 | __block NSMutableArray* res = @[].mutableCopy; 498 | __block id final = nil; 499 | @autoreleasepool { 500 | [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) { 501 | resolve(@"1"); 502 | }] 503 | .then(^id(id value){ 504 | static NSUInteger triedCount = 0; 505 | triedCount++; 506 | [res addObject:[value copy]]; 507 | if (triedCount == retryCount) { 508 | return @"hola"; 509 | }else{ 510 | NSException *e = [NSException 511 | exceptionWithName:@"name" 512 | reason:@"reason" 513 | userInfo:@{}]; 514 | @throw e; 515 | return nil; 516 | } 517 | return nil; 518 | }) 519 | .retry(retryCount) 520 | .then(^id(id value){ 521 | final = value; 522 | return nil; 523 | }) 524 | .catch(^(NSError* e){ 525 | final = e; 526 | }); 527 | } 528 | XCTAssert(res.count == retryCount); 529 | XCTAssertTrue([final isEqualToString:@"hola"]); 530 | } 531 | 532 | - (void) testMap1 533 | { 534 | __block NSArray* final = nil; 535 | @autoreleasepool { 536 | [RWPromise map:@[@"1",@"2",@"3"] :^RWPromise *(id value) { 537 | return [RWPromise resolve:value]; 538 | }].then(^id(NSArray* values){ 539 | final = values; 540 | return nil; 541 | }); 542 | } 543 | 544 | XCTAssertTrue([final isKindOfClass:[NSArray class]]); 545 | XCTAssertEqual(3, final.count); 546 | XCTAssertTrue([final containsObject:@"1"]); 547 | XCTAssertTrue([final containsObject:@"2"]); 548 | XCTAssertTrue([final containsObject:@"3"]); 549 | } 550 | 551 | - (void) testMap2 552 | { 553 | __block id final = nil; 554 | @autoreleasepool { 555 | [RWPromise map:@[@"1",@"2",@"3"] :^RWPromise *(id value) { 556 | return [RWPromise reject:value]; 557 | }].then(^id(NSArray* values){ 558 | final = values; 559 | return nil; 560 | }) 561 | .catch(^(NSError* e){ 562 | final = e; 563 | }); 564 | } 565 | 566 | XCTAssert([final isKindOfClass:[NSError class]]); 567 | } 568 | 569 | - (void) testFilter 570 | { 571 | __block NSArray* final = nil; 572 | @autoreleasepool { 573 | [RWPromise filter:@[@1,@2,@3,@4,@5] :^BOOL(NSNumber* number) { 574 | return number.integerValue % 2 == 0; 575 | }] 576 | .then(^id(NSArray* values){ 577 | final = values; 578 | return nil; 579 | }); 580 | } 581 | XCTAssertEqual(2, final.count); 582 | XCTAssertTrue([final containsObject:@2]); 583 | XCTAssertTrue([final containsObject:@4]); 584 | } 585 | 586 | - (void) testReduce1 587 | { 588 | __block NSNumber* final = nil; 589 | @autoreleasepool { 590 | [RWPromise reduce:@[@1,@2,@3,@4,@5] :^RWPromise *(id item, NSNumber* acc) { 591 | return [RWPromise resolve:item].then(^id(NSNumber* number){ 592 | return @(acc.integerValue + number.integerValue); 593 | }); 594 | } initialValue:@(0)] 595 | .then(^id(NSNumber* value){ 596 | final = value; 597 | return nil; 598 | }); 599 | } 600 | XCTAssertTrue([final isEqualToNumber:@15]); 601 | 602 | } 603 | 604 | - (void) testReduce2 605 | { 606 | __block NSNumber* final = nil; 607 | @autoreleasepool { 608 | [RWPromise reduce:@[@1,@2,@3,@4,@5] :^RWPromise *(id item, NSNumber* acc) { 609 | return [RWPromise resolve:item].then(^id(NSNumber* number){ 610 | return @(acc.integerValue * number.integerValue); 611 | }); 612 | } initialValue:@(1)] 613 | .then(^id(NSNumber* value){ 614 | final = value; 615 | return nil; 616 | }); 617 | } 618 | XCTAssertTrue([final isEqualToNumber:@120]); 619 | 620 | } 621 | 622 | 623 | - (void) testProgress 624 | { 625 | __block NSMutableArray* res = @[].mutableCopy; 626 | @autoreleasepool { 627 | RWProgressPromise* p = 628 | [RWProgressPromise promise:^(ResolveHandler resolve, RejectHandler reject, ProgressHandler progress) { 629 | }]; 630 | p.progress(^(double propotion, id value){ 631 | [res addObject:value]; 632 | }).then(^id(id value){ 633 | [res addObject:value]; 634 | return nil; 635 | }); 636 | [p progress:0.f :@1]; 637 | [p progress:1.f :@2]; 638 | [p resolve:@3]; 639 | } 640 | XCTAssertTrue([res[0] isEqualToNumber:@1]); 641 | XCTAssertTrue([res[1] isEqualToNumber:@2]); 642 | XCTAssertTrue([res[2] isEqualToNumber:@3]); 643 | } 644 | 645 | @end 646 | -------------------------------------------------------------------------------- /RWPromise.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 6832A6271CAB05FC0082D8AB /* filter.m in Sources */ = {isa = PBXBuildFile; fileRef = 6832A6261CAB05FC0082D8AB /* filter.m */; }; 11 | 6832A6281CAB05FD0082D8AB /* filter.m in Sources */ = {isa = PBXBuildFile; fileRef = 6832A6261CAB05FC0082D8AB /* filter.m */; }; 12 | 689EBD6F1C9D8E2200C2DF18 /* RWPromise.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCCFC9389B7A3E568E92B /* RWPromise.m */; }; 13 | 689EBD701C9D8E3000C2DF18 /* timeout.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCBAB2DF0E83A838F1383 /* timeout.m */; }; 14 | 689EBD711C9D8E3300C2DF18 /* RWPromise+Error.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC09A8E71431558CD8397 /* RWPromise+Error.m */; }; 15 | 689EBD721C9D8E3800C2DF18 /* finally.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCE9B0DE69A54E1222856 /* finally.m */; }; 16 | 689EBD731C9D8E3B00C2DF18 /* then.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC5A662A4F0376BDFB722 /* then.m */; }; 17 | 689EBD741C9D8E4200C2DF18 /* catch.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCB542F8EBAEA17819D20 /* catch.m */; }; 18 | 689EBD751C9D8E4500C2DF18 /* all.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCEB87431088643966CE2 /* all.m */; }; 19 | 689EBD761C9D8E4900C2DF18 /* race.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC539CF53C196B761FBF6 /* race.m */; }; 20 | 689EBD771C9D8E4C00C2DF18 /* after.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC74D37E376F522586907 /* after.m */; }; 21 | 68A1E0F01D55ED0400773D3D /* progress.m in Sources */ = {isa = PBXBuildFile; fileRef = 68A1E0EF1D55ED0400773D3D /* progress.m */; }; 22 | 6DFDC0A7F34ACBEB08E546E0 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6DFDC71486F2EC0FD9E70FD5 /* LaunchScreen.storyboard */; }; 23 | 6DFDC2189BC8FB06B85610CA /* all.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCEB87431088643966CE2 /* all.m */; }; 24 | 6DFDC224D0271758099F2496 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC862FBF20EF24C6B256D /* AppDelegate.m */; }; 25 | 6DFDC2851C543221DA3650C2 /* RWPromise.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCCFC9389B7A3E568E92B /* RWPromise.m */; }; 26 | 6DFDC2C0E2081FD88DB4727D /* reduce.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC0BE7F6B36C941DDDD2A /* reduce.m */; }; 27 | 6DFDC2E4EF8B8C6ABB5983F2 /* timeout.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCBAB2DF0E83A838F1383 /* timeout.m */; }; 28 | 6DFDC378402B7EA4ABA3B943 /* map.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC106B9E7255B355C958D /* map.m */; }; 29 | 6DFDC38C49179F0832FF1264 /* tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC8856216DFB64250C3A3 /* tests.m */; }; 30 | 6DFDC39F23823910EC9D9930 /* catch.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCB542F8EBAEA17819D20 /* catch.m */; }; 31 | 6DFDC4761A29C16CD47196C6 /* RWPromise+Error.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC09A8E71431558CD8397 /* RWPromise+Error.m */; }; 32 | 6DFDC47DACE5DBED1059824F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6DFDCBB080BF5C9E047DC689 /* Assets.xcassets */; }; 33 | 6DFDC4A0EFF18A902124CA86 /* map.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC106B9E7255B355C958D /* map.m */; }; 34 | 6DFDC53B5FD37261C819685C /* reduce.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC0BE7F6B36C941DDDD2A /* reduce.m */; }; 35 | 6DFDC56D6132A30ACEE328BF /* retry.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC721D1A186654BA10A83 /* retry.m */; }; 36 | 6DFDC685E878F3F12F026641 /* finally.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDCE9B0DE69A54E1222856 /* finally.m */; }; 37 | 6DFDC7F0E64235FC6BE84B9E /* retry.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC721D1A186654BA10A83 /* retry.m */; }; 38 | 6DFDC9AB724AE7648640742E /* RWPromiseTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC33563E36967319AF1B3 /* RWPromiseTests.m */; }; 39 | 6DFDCA6A7D86057531D3A466 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6DFDC2BB8DC17DCE1B8FC82D /* Main.storyboard */; }; 40 | 6DFDCB225497EF3BE1213CAF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC0D7EA675F2CF25C1C17 /* main.m */; }; 41 | 6DFDCC8C32D9D56D30122B9F /* after.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC74D37E376F522586907 /* after.m */; }; 42 | 6DFDCCC66983E77BE9AB5A36 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC7B08D0C350D116E4EB5 /* ViewController.m */; }; 43 | 6DFDCD670750405963BF31AB /* then.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC5A662A4F0376BDFB722 /* then.m */; }; 44 | 6DFDCED0CC94DA6B6D939440 /* race.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DFDC539CF53C196B761FBF6 /* race.m */; }; 45 | /* End PBXBuildFile section */ 46 | 47 | /* Begin PBXContainerItemProxy section */ 48 | 6DFDCE6EDFC2FDFF848DF833 /* PBXContainerItemProxy */ = { 49 | isa = PBXContainerItemProxy; 50 | containerPortal = 6DFDC58F70518388EBEAA73A /* Project object */; 51 | proxyType = 1; 52 | remoteGlobalIDString = 6DFDC99486E95947DF5814E6; 53 | remoteInfo = RWPromise; 54 | }; 55 | /* End PBXContainerItemProxy section */ 56 | 57 | /* Begin PBXFileReference section */ 58 | 6832A6261CAB05FC0082D8AB /* filter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = filter.m; sourceTree = ""; }; 59 | 68A1E0EF1D55ED0400773D3D /* progress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = progress.m; sourceTree = ""; }; 60 | 6DFDC09A8E71431558CD8397 /* RWPromise+Error.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RWPromise+Error.m"; sourceTree = ""; }; 61 | 6DFDC0BE7F6B36C941DDDD2A /* reduce.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = reduce.m; sourceTree = ""; }; 62 | 6DFDC0D7EA675F2CF25C1C17 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 63 | 6DFDC106B9E7255B355C958D /* map.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = map.m; sourceTree = ""; }; 64 | 6DFDC24DCE4F6C9499E866F1 /* RWThenable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RWThenable.h; sourceTree = ""; }; 65 | 6DFDC33563E36967319AF1B3 /* RWPromiseTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RWPromiseTests.m; sourceTree = ""; }; 66 | 6DFDC539CF53C196B761FBF6 /* race.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = race.m; sourceTree = ""; }; 67 | 6DFDC5A662A4F0376BDFB722 /* then.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = then.m; sourceTree = ""; }; 68 | 6DFDC63D4F9DFDA74771EC60 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 69 | 6DFDC721D1A186654BA10A83 /* retry.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = retry.m; sourceTree = ""; }; 70 | 6DFDC74D37E376F522586907 /* after.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = after.m; sourceTree = ""; }; 71 | 6DFDC7B08D0C350D116E4EB5 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 72 | 6DFDC862FBF20EF24C6B256D /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 73 | 6DFDC8856216DFB64250C3A3 /* tests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = tests.m; sourceTree = ""; }; 74 | 6DFDC8AE33410123F95C5E97 /* RWPromise+Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RWPromise+Internal.h"; sourceTree = ""; }; 75 | 6DFDC8FD4CBC3C89553B4832 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 76 | 6DFDC9006B395BBD8F76B0FE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.info; path = Info.plist; sourceTree = ""; }; 77 | 6DFDC90D4673DAD06829961A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.info; path = Info.plist; sourceTree = ""; }; 78 | 6DFDCAFF146FFBFACFF3A206 /* RWPromiseTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RWPromiseTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 79 | 6DFDCB542F8EBAEA17819D20 /* catch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = catch.m; sourceTree = ""; }; 80 | 6DFDCBAB2DF0E83A838F1383 /* timeout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = timeout.m; sourceTree = ""; }; 81 | 6DFDCBB080BF5C9E047DC689 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 82 | 6DFDCBB1F4205C3F489FC5FC /* RWPromise.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RWPromise.h; sourceTree = ""; }; 83 | 6DFDCCA3D8BEE73F654A2BD9 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 84 | 6DFDCCFC9389B7A3E568E92B /* RWPromise.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RWPromise.m; sourceTree = ""; }; 85 | 6DFDCE95AA0A1B48AE140827 /* RWPromise.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RWPromise.app; sourceTree = BUILT_PRODUCTS_DIR; }; 86 | 6DFDCE9B0DE69A54E1222856 /* finally.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = finally.m; sourceTree = ""; }; 87 | 6DFDCEB87431088643966CE2 /* all.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = all.m; sourceTree = ""; }; 88 | 6DFDCF72A63E6C2C61204CCA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 89 | /* End PBXFileReference section */ 90 | 91 | /* Begin PBXFrameworksBuildPhase section */ 92 | 6DFDC7A6DE232D8FF864ED2E /* Frameworks */ = { 93 | isa = PBXFrameworksBuildPhase; 94 | buildActionMask = 2147483647; 95 | files = ( 96 | ); 97 | runOnlyForDeploymentPostprocessing = 0; 98 | }; 99 | 6DFDC9861ED22580425FFD3C /* Frameworks */ = { 100 | isa = PBXFrameworksBuildPhase; 101 | buildActionMask = 2147483647; 102 | files = ( 103 | ); 104 | runOnlyForDeploymentPostprocessing = 0; 105 | }; 106 | /* End PBXFrameworksBuildPhase section */ 107 | 108 | /* Begin PBXGroup section */ 109 | 6DFDC09611D798FE2A6F4277 /* RWPromise */ = { 110 | isa = PBXGroup; 111 | children = ( 112 | 6DFDC9006B395BBD8F76B0FE /* Info.plist */, 113 | 6DFDCBB080BF5C9E047DC689 /* Assets.xcassets */, 114 | 6DFDC71486F2EC0FD9E70FD5 /* LaunchScreen.storyboard */, 115 | 6DFDCE9F7368FF16EEF37E84 /* Supporting Files */, 116 | 6DFDCCA3D8BEE73F654A2BD9 /* AppDelegate.h */, 117 | 6DFDC862FBF20EF24C6B256D /* AppDelegate.m */, 118 | 6DFDC2BB8DC17DCE1B8FC82D /* Main.storyboard */, 119 | 6DFDC63D4F9DFDA74771EC60 /* ViewController.h */, 120 | 6DFDC7B08D0C350D116E4EB5 /* ViewController.m */, 121 | 6DFDCC1EDEA240574781DC75 /* Class */, 122 | ); 123 | path = RWPromise; 124 | sourceTree = ""; 125 | }; 126 | 6DFDC0A3C8479BE9F6E6B2DF /* Internal */ = { 127 | isa = PBXGroup; 128 | children = ( 129 | 6DFDC74D37E376F522586907 /* after.m */, 130 | 6DFDC539CF53C196B761FBF6 /* race.m */, 131 | 6DFDCEB87431088643966CE2 /* all.m */, 132 | 6DFDCB542F8EBAEA17819D20 /* catch.m */, 133 | 6DFDC8AE33410123F95C5E97 /* RWPromise+Internal.h */, 134 | 6DFDC5A662A4F0376BDFB722 /* then.m */, 135 | 6DFDCE9B0DE69A54E1222856 /* finally.m */, 136 | 6DFDCBAB2DF0E83A838F1383 /* timeout.m */, 137 | 6DFDC09A8E71431558CD8397 /* RWPromise+Error.m */, 138 | 6DFDC721D1A186654BA10A83 /* retry.m */, 139 | 6DFDC106B9E7255B355C958D /* map.m */, 140 | 6832A6261CAB05FC0082D8AB /* filter.m */, 141 | 6DFDC0BE7F6B36C941DDDD2A /* reduce.m */, 142 | 68A1E0EF1D55ED0400773D3D /* progress.m */, 143 | ); 144 | path = Internal; 145 | sourceTree = ""; 146 | }; 147 | 6DFDCA351F42BC18B7BD8FEE = { 148 | isa = PBXGroup; 149 | children = ( 150 | 6DFDCDB7ED9B5D7F852CB296 /* Products */, 151 | 6DFDC09611D798FE2A6F4277 /* RWPromise */, 152 | 6DFDCC284151399B503FEABE /* RWPromiseTests */, 153 | ); 154 | sourceTree = ""; 155 | }; 156 | 6DFDCC1EDEA240574781DC75 /* Class */ = { 157 | isa = PBXGroup; 158 | children = ( 159 | 6DFDCBB1F4205C3F489FC5FC /* RWPromise.h */, 160 | 6DFDCCFC9389B7A3E568E92B /* RWPromise.m */, 161 | 6DFDC0A3C8479BE9F6E6B2DF /* Internal */, 162 | 6DFDC24DCE4F6C9499E866F1 /* RWThenable.h */, 163 | ); 164 | path = Class; 165 | sourceTree = ""; 166 | }; 167 | 6DFDCC284151399B503FEABE /* RWPromiseTests */ = { 168 | isa = PBXGroup; 169 | children = ( 170 | 6DFDC90D4673DAD06829961A /* Info.plist */, 171 | 6DFDC33563E36967319AF1B3 /* RWPromiseTests.m */, 172 | ); 173 | path = RWPromiseTests; 174 | sourceTree = ""; 175 | }; 176 | 6DFDCDB7ED9B5D7F852CB296 /* Products */ = { 177 | isa = PBXGroup; 178 | children = ( 179 | 6DFDCE95AA0A1B48AE140827 /* RWPromise.app */, 180 | 6DFDCAFF146FFBFACFF3A206 /* RWPromiseTests.xctest */, 181 | ); 182 | name = Products; 183 | sourceTree = ""; 184 | }; 185 | 6DFDCE9F7368FF16EEF37E84 /* Supporting Files */ = { 186 | isa = PBXGroup; 187 | children = ( 188 | 6DFDC0D7EA675F2CF25C1C17 /* main.m */, 189 | 6DFDC8856216DFB64250C3A3 /* tests.m */, 190 | ); 191 | name = "Supporting Files"; 192 | sourceTree = ""; 193 | }; 194 | /* End PBXGroup section */ 195 | 196 | /* Begin PBXNativeTarget section */ 197 | 6DFDC99486E95947DF5814E6 /* RWPromise */ = { 198 | isa = PBXNativeTarget; 199 | buildConfigurationList = 6DFDC66D485385F715AA0E08 /* Build configuration list for PBXNativeTarget "RWPromise" */; 200 | buildPhases = ( 201 | 6DFDC6B283767383AEB36AB6 /* Sources */, 202 | 6DFDC7A6DE232D8FF864ED2E /* Frameworks */, 203 | 6DFDC813B29E7B727B62BEF8 /* Resources */, 204 | ); 205 | buildRules = ( 206 | ); 207 | dependencies = ( 208 | ); 209 | name = RWPromise; 210 | productName = RWPromise; 211 | productReference = 6DFDCE95AA0A1B48AE140827 /* RWPromise.app */; 212 | productType = "com.apple.product-type.application"; 213 | }; 214 | 6DFDCA2957A208C94FDDB60C /* RWPromiseTests */ = { 215 | isa = PBXNativeTarget; 216 | buildConfigurationList = 6DFDC249F9A3981346D3EEA5 /* Build configuration list for PBXNativeTarget "RWPromiseTests" */; 217 | buildPhases = ( 218 | 6DFDCE1F71BAC25848DA51B4 /* Sources */, 219 | 6DFDC9861ED22580425FFD3C /* Frameworks */, 220 | 6DFDCCE41BEE84853C4031F2 /* Resources */, 221 | ); 222 | buildRules = ( 223 | ); 224 | dependencies = ( 225 | 6DFDC51DD4D7EED7CA293440 /* PBXTargetDependency */, 226 | ); 227 | name = RWPromiseTests; 228 | productName = RWPromiseTests; 229 | productReference = 6DFDCAFF146FFBFACFF3A206 /* RWPromiseTests.xctest */; 230 | productType = "com.apple.product-type.bundle.unit-test"; 231 | }; 232 | /* End PBXNativeTarget section */ 233 | 234 | /* Begin PBXProject section */ 235 | 6DFDC58F70518388EBEAA73A /* Project object */ = { 236 | isa = PBXProject; 237 | attributes = { 238 | LastUpgradeCheck = 0720; 239 | ORGANIZATIONNAME = RW; 240 | }; 241 | buildConfigurationList = 6DFDC34C08D1F4FEAE9C427E /* Build configuration list for PBXProject "RWPromise" */; 242 | compatibilityVersion = "Xcode 3.2"; 243 | developmentRegion = English; 244 | hasScannedForEncodings = 0; 245 | knownRegions = ( 246 | en, 247 | ); 248 | mainGroup = 6DFDCA351F42BC18B7BD8FEE; 249 | productRefGroup = 6DFDCDB7ED9B5D7F852CB296 /* Products */; 250 | projectDirPath = ""; 251 | projectRoot = ""; 252 | targets = ( 253 | 6DFDC99486E95947DF5814E6 /* RWPromise */, 254 | 6DFDCA2957A208C94FDDB60C /* RWPromiseTests */, 255 | ); 256 | }; 257 | /* End PBXProject section */ 258 | 259 | /* Begin PBXResourcesBuildPhase section */ 260 | 6DFDC813B29E7B727B62BEF8 /* Resources */ = { 261 | isa = PBXResourcesBuildPhase; 262 | buildActionMask = 2147483647; 263 | files = ( 264 | 6DFDC47DACE5DBED1059824F /* Assets.xcassets in Resources */, 265 | 6DFDC0A7F34ACBEB08E546E0 /* LaunchScreen.storyboard in Resources */, 266 | 6DFDCA6A7D86057531D3A466 /* Main.storyboard in Resources */, 267 | ); 268 | runOnlyForDeploymentPostprocessing = 0; 269 | }; 270 | 6DFDCCE41BEE84853C4031F2 /* Resources */ = { 271 | isa = PBXResourcesBuildPhase; 272 | buildActionMask = 2147483647; 273 | files = ( 274 | ); 275 | runOnlyForDeploymentPostprocessing = 0; 276 | }; 277 | /* End PBXResourcesBuildPhase section */ 278 | 279 | /* Begin PBXSourcesBuildPhase section */ 280 | 6DFDC6B283767383AEB36AB6 /* Sources */ = { 281 | isa = PBXSourcesBuildPhase; 282 | buildActionMask = 2147483647; 283 | files = ( 284 | 6DFDCB225497EF3BE1213CAF /* main.m in Sources */, 285 | 6DFDC224D0271758099F2496 /* AppDelegate.m in Sources */, 286 | 6DFDCCC66983E77BE9AB5A36 /* ViewController.m in Sources */, 287 | 6DFDC38C49179F0832FF1264 /* tests.m in Sources */, 288 | 6DFDC2851C543221DA3650C2 /* RWPromise.m in Sources */, 289 | 6DFDCC8C32D9D56D30122B9F /* after.m in Sources */, 290 | 6DFDCED0CC94DA6B6D939440 /* race.m in Sources */, 291 | 6DFDC2189BC8FB06B85610CA /* all.m in Sources */, 292 | 6DFDC39F23823910EC9D9930 /* catch.m in Sources */, 293 | 6832A6271CAB05FC0082D8AB /* filter.m in Sources */, 294 | 6DFDCD670750405963BF31AB /* then.m in Sources */, 295 | 6DFDC685E878F3F12F026641 /* finally.m in Sources */, 296 | 68A1E0F01D55ED0400773D3D /* progress.m in Sources */, 297 | 6DFDC2E4EF8B8C6ABB5983F2 /* timeout.m in Sources */, 298 | 6DFDC4761A29C16CD47196C6 /* RWPromise+Error.m in Sources */, 299 | 6DFDC56D6132A30ACEE328BF /* retry.m in Sources */, 300 | 6DFDC4A0EFF18A902124CA86 /* map.m in Sources */, 301 | 6DFDC53B5FD37261C819685C /* reduce.m in Sources */, 302 | ); 303 | runOnlyForDeploymentPostprocessing = 0; 304 | }; 305 | 6DFDCE1F71BAC25848DA51B4 /* Sources */ = { 306 | isa = PBXSourcesBuildPhase; 307 | buildActionMask = 2147483647; 308 | files = ( 309 | 689EBD731C9D8E3B00C2DF18 /* then.m in Sources */, 310 | 689EBD751C9D8E4500C2DF18 /* all.m in Sources */, 311 | 689EBD771C9D8E4C00C2DF18 /* after.m in Sources */, 312 | 689EBD721C9D8E3800C2DF18 /* finally.m in Sources */, 313 | 689EBD761C9D8E4900C2DF18 /* race.m in Sources */, 314 | 689EBD6F1C9D8E2200C2DF18 /* RWPromise.m in Sources */, 315 | 689EBD711C9D8E3300C2DF18 /* RWPromise+Error.m in Sources */, 316 | 6832A6281CAB05FD0082D8AB /* filter.m in Sources */, 317 | 6DFDC9AB724AE7648640742E /* RWPromiseTests.m in Sources */, 318 | 689EBD741C9D8E4200C2DF18 /* catch.m in Sources */, 319 | 689EBD701C9D8E3000C2DF18 /* timeout.m in Sources */, 320 | 6DFDC7F0E64235FC6BE84B9E /* retry.m in Sources */, 321 | 6DFDC378402B7EA4ABA3B943 /* map.m in Sources */, 322 | 6DFDC2C0E2081FD88DB4727D /* reduce.m in Sources */, 323 | ); 324 | runOnlyForDeploymentPostprocessing = 0; 325 | }; 326 | /* End PBXSourcesBuildPhase section */ 327 | 328 | /* Begin PBXTargetDependency section */ 329 | 6DFDC51DD4D7EED7CA293440 /* PBXTargetDependency */ = { 330 | isa = PBXTargetDependency; 331 | target = 6DFDC99486E95947DF5814E6 /* RWPromise */; 332 | targetProxy = 6DFDCE6EDFC2FDFF848DF833 /* PBXContainerItemProxy */; 333 | }; 334 | /* End PBXTargetDependency section */ 335 | 336 | /* Begin PBXVariantGroup section */ 337 | 6DFDC2BB8DC17DCE1B8FC82D /* Main.storyboard */ = { 338 | isa = PBXVariantGroup; 339 | children = ( 340 | 6DFDCF72A63E6C2C61204CCA /* Base */, 341 | ); 342 | name = Main.storyboard; 343 | sourceTree = ""; 344 | }; 345 | 6DFDC71486F2EC0FD9E70FD5 /* LaunchScreen.storyboard */ = { 346 | isa = PBXVariantGroup; 347 | children = ( 348 | 6DFDC8FD4CBC3C89553B4832 /* Base */, 349 | ); 350 | name = LaunchScreen.storyboard; 351 | sourceTree = ""; 352 | }; 353 | /* End PBXVariantGroup section */ 354 | 355 | /* Begin XCBuildConfiguration section */ 356 | 6DFDC1844172E3CD12125B21 /* Release */ = { 357 | isa = XCBuildConfiguration; 358 | buildSettings = { 359 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 360 | INFOPLIST_FILE = RWPromise/Info.plist; 361 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 362 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 363 | PRODUCT_BUNDLE_IDENTIFIER = RW.RWPromise; 364 | PRODUCT_NAME = "$(TARGET_NAME)"; 365 | }; 366 | name = Release; 367 | }; 368 | 6DFDC38C29713EA388ED47F4 /* Debug */ = { 369 | isa = XCBuildConfiguration; 370 | buildSettings = { 371 | ALWAYS_SEARCH_USER_PATHS = NO; 372 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 373 | CLANG_CXX_LIBRARY = "libc++"; 374 | CLANG_ENABLE_MODULES = YES; 375 | CLANG_ENABLE_OBJC_ARC = YES; 376 | CLANG_WARN_BOOL_CONVERSION = YES; 377 | CLANG_WARN_CONSTANT_CONVERSION = YES; 378 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 379 | CLANG_WARN_EMPTY_BODY = YES; 380 | CLANG_WARN_ENUM_CONVERSION = YES; 381 | CLANG_WARN_INT_CONVERSION = YES; 382 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 383 | CLANG_WARN_UNREACHABLE_CODE = YES; 384 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 385 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 386 | COPY_PHASE_STRIP = NO; 387 | DEBUG_INFORMATION_FORMAT = dwarf; 388 | ENABLE_STRICT_OBJC_MSGSEND = YES; 389 | ENABLE_TESTABILITY = YES; 390 | GCC_C_LANGUAGE_STANDARD = gnu99; 391 | GCC_DYNAMIC_NO_PIC = NO; 392 | GCC_NO_COMMON_BLOCKS = YES; 393 | GCC_OPTIMIZATION_LEVEL = 0; 394 | GCC_PREPROCESSOR_DEFINITIONS = ( 395 | "DEBUG=1", 396 | "$(inherited)", 397 | ); 398 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 399 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 400 | GCC_WARN_UNDECLARED_SELECTOR = YES; 401 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 402 | GCC_WARN_UNUSED_FUNCTION = YES; 403 | GCC_WARN_UNUSED_VARIABLE = YES; 404 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 405 | MTL_ENABLE_DEBUG_INFO = YES; 406 | ONLY_ACTIVE_ARCH = YES; 407 | SDKROOT = iphoneos; 408 | }; 409 | name = Debug; 410 | }; 411 | 6DFDC4C72D3FB25E01E1C61E /* Debug */ = { 412 | isa = XCBuildConfiguration; 413 | buildSettings = { 414 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/RWPromise.app/RWPromise"; 415 | INFOPLIST_FILE = RWPromiseTests/Info.plist; 416 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 417 | PRODUCT_BUNDLE_IDENTIFIER = RW.RWPromiseTests; 418 | PRODUCT_NAME = "$(TARGET_NAME)"; 419 | TEST_HOST = "$(BUNDLE_LOADER)"; 420 | }; 421 | name = Debug; 422 | }; 423 | 6DFDC5FDD4AFB7E4AF345AF9 /* Debug */ = { 424 | isa = XCBuildConfiguration; 425 | buildSettings = { 426 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 427 | INFOPLIST_FILE = RWPromise/Info.plist; 428 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 429 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 430 | PRODUCT_BUNDLE_IDENTIFIER = RW.RWPromise; 431 | PRODUCT_NAME = "$(TARGET_NAME)"; 432 | }; 433 | name = Debug; 434 | }; 435 | 6DFDCC210C1BC56A119D1C8E /* Release */ = { 436 | isa = XCBuildConfiguration; 437 | buildSettings = { 438 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/RWPromise.app/RWPromise"; 439 | INFOPLIST_FILE = RWPromiseTests/Info.plist; 440 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 441 | PRODUCT_BUNDLE_IDENTIFIER = RW.RWPromiseTests; 442 | PRODUCT_NAME = "$(TARGET_NAME)"; 443 | TEST_HOST = "$(BUNDLE_LOADER)"; 444 | }; 445 | name = Release; 446 | }; 447 | 6DFDCF7DFF8027913F0D515F /* Release */ = { 448 | isa = XCBuildConfiguration; 449 | buildSettings = { 450 | ALWAYS_SEARCH_USER_PATHS = NO; 451 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 452 | CLANG_CXX_LIBRARY = "libc++"; 453 | CLANG_ENABLE_MODULES = YES; 454 | CLANG_ENABLE_OBJC_ARC = YES; 455 | CLANG_WARN_BOOL_CONVERSION = YES; 456 | CLANG_WARN_CONSTANT_CONVERSION = YES; 457 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 458 | CLANG_WARN_EMPTY_BODY = YES; 459 | CLANG_WARN_ENUM_CONVERSION = YES; 460 | CLANG_WARN_INT_CONVERSION = YES; 461 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 462 | CLANG_WARN_UNREACHABLE_CODE = YES; 463 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 464 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 465 | COPY_PHASE_STRIP = NO; 466 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 467 | ENABLE_NS_ASSERTIONS = NO; 468 | ENABLE_STRICT_OBJC_MSGSEND = YES; 469 | GCC_C_LANGUAGE_STANDARD = gnu99; 470 | GCC_NO_COMMON_BLOCKS = YES; 471 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 472 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 473 | GCC_WARN_UNDECLARED_SELECTOR = YES; 474 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 475 | GCC_WARN_UNUSED_FUNCTION = YES; 476 | GCC_WARN_UNUSED_VARIABLE = YES; 477 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 478 | MTL_ENABLE_DEBUG_INFO = NO; 479 | SDKROOT = iphoneos; 480 | VALIDATE_PRODUCT = YES; 481 | }; 482 | name = Release; 483 | }; 484 | /* End XCBuildConfiguration section */ 485 | 486 | /* Begin XCConfigurationList section */ 487 | 6DFDC249F9A3981346D3EEA5 /* Build configuration list for PBXNativeTarget "RWPromiseTests" */ = { 488 | isa = XCConfigurationList; 489 | buildConfigurations = ( 490 | 6DFDC4C72D3FB25E01E1C61E /* Debug */, 491 | 6DFDCC210C1BC56A119D1C8E /* Release */, 492 | ); 493 | defaultConfigurationIsVisible = 0; 494 | defaultConfigurationName = Release; 495 | }; 496 | 6DFDC34C08D1F4FEAE9C427E /* Build configuration list for PBXProject "RWPromise" */ = { 497 | isa = XCConfigurationList; 498 | buildConfigurations = ( 499 | 6DFDC38C29713EA388ED47F4 /* Debug */, 500 | 6DFDCF7DFF8027913F0D515F /* Release */, 501 | ); 502 | defaultConfigurationIsVisible = 0; 503 | defaultConfigurationName = Release; 504 | }; 505 | 6DFDC66D485385F715AA0E08 /* Build configuration list for PBXNativeTarget "RWPromise" */ = { 506 | isa = XCConfigurationList; 507 | buildConfigurations = ( 508 | 6DFDC5FDD4AFB7E4AF345AF9 /* Debug */, 509 | 6DFDC1844172E3CD12125B21 /* Release */, 510 | ); 511 | defaultConfigurationIsVisible = 0; 512 | defaultConfigurationName = Release; 513 | }; 514 | /* End XCConfigurationList section */ 515 | }; 516 | rootObject = 6DFDC58F70518388EBEAA73A /* Project object */; 517 | } 518 | --------------------------------------------------------------------------------