Skip to content

Commit ffdb207

Browse files
taichinobenski
authored andcommitted
Add a factory method for convenience (benski#5)
* Add factory method * Chaining * Fix typo
1 parent 7aaa4f3 commit ffdb207

File tree

7 files changed

+121
-34
lines changed

7 files changed

+121
-34
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ DerivedData
1616
*.hmap
1717
*.ipa
1818
*.xcuserstate
19+
.DS_Store
20+
Podfile.lock
1921

2022
# CocoaPods
2123
#

BAPromise.xcodeproj/project.pbxproj

+48-30
Large diffs are not rendered by default.

BAPromiseTests/HelperTests.swift

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// HelperTests.swift
3+
// BAPromise
4+
//
5+
// Created by Taichi Matsumoto on 12/28/16.
6+
// Copyright © 2016 Ben Allison. All rights reserved.
7+
//
8+
9+
import XCTest
10+
import BAPromise
11+
12+
class HelperTests: XCTestCase {
13+
14+
override func setUp() {
15+
super.setUp()
16+
}
17+
18+
override func tearDown() {
19+
super.tearDown()
20+
}
21+
22+
func testFulfillWithFactoryMethod() {
23+
let expectation = XCTestExpectation()
24+
25+
BAPromiseClient<NSString>.promise({fulfill, reject in
26+
fulfill("Success" as NSString)
27+
}).then { value in
28+
expectation.fulfill()
29+
}.rejected { error in
30+
XCTFail("the promise should never be rejected")
31+
}
32+
}
33+
34+
func testRejectWithFactoryMethod() {
35+
let expectation = XCTestExpectation()
36+
37+
BAPromiseClient<NSString>.promise({fulfill, reject in
38+
reject(NSError(domain: "promise test", code: 0, userInfo: nil))
39+
}).then { value in
40+
XCTFail("the promise should never be fulfilled")
41+
}.rejected { error in
42+
expectation.fulfill()
43+
}
44+
}
45+
}

Classes/BAPromise.h

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ typedef dispatch_block_t BAPromiseFinallyBlock;
8787
+(instancetype)fulfilledPromise:(T)obj;
8888
+(instancetype)rejectedPromise:(NSError *)error;
8989

90+
// Unfortunate signature thanks to objc fukcing block syntax.
91+
// This method takes one block "resolver" as a parameter, which takes two blocks "fulfill" and "reject".
92+
+(nonnull instancetype)promiseWithResolver:(void (^ __nonnull)(void (^ __nonnull fulfill)(__nonnull T), void (^ __nonnull reject)(NSError * __nonnull)))resolver NS_SWIFT_NAME(promise(_:));
93+
9094
/* helper methods to streamline syntax for nil objects*/
9195
-(void)fulfill;
9296
-(void)reject;

Classes/BAPromise.m

+12
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,18 @@ +(BAPromiseClient *)rejectedPromise:(NSError *)error
520520
return promise;
521521
}
522522

523+
+(instancetype)promiseWithResolver:(void (^)(void (^fulfill)(id), void (^reject)(NSError *)))resolver
524+
{
525+
BAPromiseClient *promise = [[BAPromiseClient alloc] init];
526+
resolver(^(id obj) {
527+
[promise fulfillWithObject:obj];
528+
},
529+
^(NSError *error) {
530+
[promise rejectWithError:error];
531+
});
532+
return promise;
533+
}
534+
523535
-(void)fulfillWithObject:(id)obj
524536
{
525537
if ([obj isKindOfClass:[BAPromise class]]) {

OS XTests-Bridging-Header.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//
2+
// Use this file to import your target's public headers that you would like to expose to Swift.
3+
//
4+

Podfile

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
xcodeproj 'BAPromise'
2+
use_frameworks!
23

34
target 'iOS' do
4-
platform :ios, '8.0'
5-
target 'iOSTests' do
6-
pod "OCMock"
7-
end
5+
platform :ios, '8.0'
6+
pod "BAPromise" , :path => "."
7+
target 'iOSTests' do
8+
pod "OCMock"
9+
end
810
end
911

1012
target 'OS X' do

0 commit comments

Comments
 (0)