Skip to content

Commit

Permalink
add async support for promise resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Yeh committed Aug 4, 2016
1 parent 4ac18e4 commit 25e1fa0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
2 changes: 1 addition & 1 deletion AYPromise.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'AYPromise'
s.version = '1.1.1'
s.version = '1.1.2'
s.summary = 'Promise for objective-c.'

s.homepage = 'https://github.com/alan-yeh/AYPromise'
Expand Down
5 changes: 5 additions & 0 deletions AYPromise/Classes/AYPromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ typedef NS_ENUM(NSUInteger, AYPromiseState) {
- (AYPromise *(^)(NSTimeInterval delaySecond, id value))thenDelay;/**< 延迟执行 */
- (AYPromise *(^)(dispatch_queue_t queue, id value))thenOn;/**< 在指定线程执行 */
- (AYPromise *(^)(void (^resolver)(id result, AYResolve resolve)))thenPromise;/**< 需要回调的任务 */
- (AYPromise *(^)(void (^resolver)(id result, AYResolve resolve)))thenAsyncPromise;/**< 异步执行需要回调的任务 */
- (AYPromise *(^)(id value))catchAsync;/**< 异步处理错误 */
- (AYPromise *(^)(dispatch_queue_t queue, id value))catchOn;/**< 在指定线程处理错误 */
- (AYPromise *(^)(id value))always;/**< 无论错误还是正确都执行 */
Expand All @@ -120,6 +121,10 @@ FOUNDATION_EXPORT AYPromise *AYPromiseAsyncWith(_Nullable id value);
* 创建一个需要回调的Promise
*/
FOUNDATION_EXPORT AYPromise *AYPromiseWithResolve(void (^)(AYResolve resolve));
/**
* 创建一个异步执行,需要回调的Promise
*/
FOUNDATION_EXPORT AYPromise *AYPromiseAsyncWithResolve(void (^)(AYResolve resolve));
NS_ASSUME_NONNULL_END


30 changes: 28 additions & 2 deletions AYPromise/Classes/AYPromise.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ - (NSMutableArray *)handlers{
/**
* 创建一个未执行的Promise
*/
- (instancetype)initWithResolver:(void (^)(AYResolve))resolver{
- (instancetype)initWithResolver:(void (^)(AYResolve))resolver andExecuteQueue:(dispatch_queue_t)queue{
if (self = [super init]) {
_state = AYPromiseStatePending;

Expand Down Expand Up @@ -136,7 +136,7 @@ - (instancetype)initWithResolver:(void (^)(AYResolve))resolver{
}
};
//创建好之后,直接开始执行任务
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(queue, ^{
@try {
resolver(__resolve);
}
Expand All @@ -148,6 +148,10 @@ - (instancetype)initWithResolver:(void (^)(AYResolve))resolver{
return self;
}

- (instancetype)initWithResolver:(void (^)(AYResolve))resolver{
return [self initWithResolver:resolver andExecuteQueue:dispatch_get_main_queue()];
}

/**
* 创建一个已完成的Promise
* 如果Value是Promise对象,则直接返回
Expand Down Expand Up @@ -365,6 +369,25 @@ @implementation AYPromise (Extension)
};
}

- (AYPromise * (^)(void (^)(id, AYResolve)))thenAsyncPromise{
return ^(void (^resolver)(id, AYResolve)){
return __pipe(self, ^(id result, AYResolve resolve) {
if (!isError(result)) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@try {
resolver(result, resolve);
}
@catch (NSError *error) {
resolve(error);
}
});
}else{
resolve(result);
}
});
};
}

- (AYPromise *(^)(id))catchAsync{
return ^(id value){
NSAssert(isBlock(value) || isInvocation(value), @"[catchAsync] can only handle block/invocation.");
Expand Down Expand Up @@ -418,3 +441,6 @@ @implementation AYPromise (Extension)
return [[AYPromise alloc] initWithResolver:resolver];
}

AYPromise *AYPromiseAsyncWithResolve(void (^resolver)(AYResolve)){
return [[AYPromise alloc] initWithResolver:resolver andExecuteQueue:dispatch_get_global_queue(0, 0)];
}
16 changes: 8 additions & 8 deletions Example/AYPromise.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 6003F5BF195388D20070C39A /* Build configuration list for PBXNativeTarget "AYPromise_Example" */;
buildPhases = (
D0BF0870D18C56D4616FD914 /* 📦 Check Pods Manifest.lock */,
D0BF0870D18C56D4616FD914 /* [CP] Check Pods Manifest.lock */,
9D5360476AEE413E105BB188 /* [CP] Check Pods Manifest.lock */,
6003F586195388D20070C39A /* Sources */,
6003F587195388D20070C39A /* Frameworks */,
Expand All @@ -232,7 +232,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 6003F5C2195388D20070C39A /* Build configuration list for PBXNativeTarget "AYPromise_Tests" */;
buildPhases = (
A28A20F7BFBBFE2AFE679088 /* 📦 Check Pods Manifest.lock */,
A28A20F7BFBBFE2AFE679088 /* [CP] Check Pods Manifest.lock */,
EFA78DA3FA0AA846AE8FE514 /* [CP] Check Pods Manifest.lock */,
6003F5AA195388D20070C39A /* Sources */,
6003F5AB195388D20070C39A /* Frameworks */,
Expand Down Expand Up @@ -429,34 +429,34 @@
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
A28A20F7BFBBFE2AFE679088 /* 📦 Check Pods Manifest.lock */ = {
A28A20F7BFBBFE2AFE679088 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Check Pods Manifest.lock";
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
D0BF0870D18C56D4616FD914 /* 📦 Check Pods Manifest.lock */ = {
D0BF0870D18C56D4616FD914 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Check Pods Manifest.lock";
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
EC1F7DD50CF65810025A2CDC /* 📦 Copy Pods Resources */ = {
Expand Down
10 changes: 5 additions & 5 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- AYPromise (1.1.0):
- AYPromise (1.1.2):
- AYRuntime
- AYRuntime (1.0.1)
- AYRuntime (1.0.2)

DEPENDENCIES:
- AYPromise (from `../`)
Expand All @@ -11,9 +11,9 @@ EXTERNAL SOURCES:
:path: ../

SPEC CHECKSUMS:
AYPromise: 530ee61c6cb1219bd62823b1b9cdffa53b4e0a53
AYRuntime: c738c601ee1cc553601def6dda83e640ea0fd465
AYPromise: f0d8241aa96470a28bc5a8cde69702fad26cced7
AYRuntime: a1d344a10f06897a4c5b7c1573d4071e72d36089

PODFILE CHECKSUM: 6e0270353a9b3f48982dd61cbbf7e0e7648b456a

COCOAPODS: 1.0.0
COCOAPODS: 1.1.0.beta.1
25 changes: 25 additions & 0 deletions Example/Tests/ThreadTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,29 @@ - (void)test12{

[self waitForExpectationsWithTimeout:TIME_OUT handler:nil];
}

- (void)test13{
id ex1 = [self expectationWithDescription:@""];
AYPromiseAsyncWithResolve(^(AYResolve _Nonnull resolve) {
XCTAssertEqual([NSThread currentThread].isMainThread, NO);
resolve(nil);
}).always(^{
[ex1 fulfill];
});
[self waitForExpectationsWithTimeout:TIME_OUT handler:nil];
}

- (void)test14{
id ex1 = [self expectationWithDescription:@""];
AYPromiseWith(^{
XCTAssertEqual([NSThread currentThread].isMainThread, YES);
}).thenAsyncPromise(^(id arg, AYResolve resolve){
XCTAssertEqual([NSThread currentThread].isMainThread, NO);
resolve(nil);
}).always(^{
[ex1 fulfill];
});
[self waitForExpectationsWithTimeout:TIME_OUT handler:nil];
}

@end

0 comments on commit 25e1fa0

Please sign in to comment.