Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dot-syntax support. #8

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Bolts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
1FA01DD018A0651600D20BDC /* BFTask+DotSyntax.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA01DCF18A0651600D20BDC /* BFTask+DotSyntax.m */; };
1FA01DD118A0651600D20BDC /* BFTask+DotSyntax.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA01DCF18A0651600D20BDC /* BFTask+DotSyntax.m */; };
1FA01DD218A0651600D20BDC /* BFTask+DotSyntax.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA01DCF18A0651600D20BDC /* BFTask+DotSyntax.m */; };
1FA01DD318A0651600D20BDC /* BFTask+DotSyntax.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA01DCF18A0651600D20BDC /* BFTask+DotSyntax.m */; };
1FA01DD718A06A5400D20BDC /* DotSyntaxedTaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA01DD418A06A5400D20BDC /* DotSyntaxedTaskTests.m */; };
1FA01DD818A06A5400D20BDC /* DotSyntaxedTaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA01DD418A06A5400D20BDC /* DotSyntaxedTaskTests.m */; };
8E0B5CD817E139660066379B /* BFTask.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8E9C3D1E17DEA35700427E62 /* BFTask.h */; };
8E0B5CD917E1396C0066379B /* BFTaskCompletionSource.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8E9C3D2017DEA35700427E62 /* BFTaskCompletionSource.h */; };
8E17EC261805D0960049E862 /* Bolts.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EA6BF661805CED500337041 /* Bolts.m */; };
Expand Down Expand Up @@ -105,6 +111,9 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
1FA01DCE18A0651600D20BDC /* BFTask+DotSyntax.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "BFTask+DotSyntax.h"; sourceTree = "<group>"; };
1FA01DCF18A0651600D20BDC /* BFTask+DotSyntax.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "BFTask+DotSyntax.m"; sourceTree = "<group>"; };
1FA01DD418A06A5400D20BDC /* DotSyntaxedTaskTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DotSyntaxedTaskTests.m; sourceTree = "<group>"; };
8E0B5CDA17E13B4C0066379B /* BoltsVersion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BoltsVersion.h; sourceTree = "<group>"; };
8E8C8ED217F23C3B00E3F1C7 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
8E8C8ED917F23C3B00E3F1C7 /* BoltsTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "BoltsTests-Info.plist"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -173,6 +182,7 @@
children = (
8EA6BF681805CF5600337041 /* BoltsTests.m */,
8E9C3D1C17DE9F6500427E62 /* TaskTests.m */,
1FA01DD418A06A5400D20BDC /* DotSyntaxedTaskTests.m */,
8E8C8ED817F23C3B00E3F1C7 /* Supporting Files */,
);
path = BoltsTests;
Expand Down Expand Up @@ -229,6 +239,8 @@
8EB2CB411805DDDB00323385 /* BFExecutor.m */,
8E9C3D1E17DEA35700427E62 /* BFTask.h */,
8E9C3D1F17DEA35700427E62 /* BFTask.m */,
1FA01DCE18A0651600D20BDC /* BFTask+DotSyntax.h */,
1FA01DCF18A0651600D20BDC /* BFTask+DotSyntax.m */,
8E9C3D2017DEA35700427E62 /* BFTaskCompletionSource.h */,
8E9C3D2117DEA35700427E62 /* BFTaskCompletionSource.m */,
8E9C3CEF17DE9DE000427E62 /* Supporting Files */,
Expand Down Expand Up @@ -380,6 +392,8 @@
files = (
8EA6BF691805CF5600337041 /* BoltsTests.m in Sources */,
8E8C8EFB17F23E5F00E3F1C7 /* TaskTests.m in Sources */,
1FA01DD718A06A5400D20BDC /* DotSyntaxedTaskTests.m in Sources */,
1FA01DD218A0651600D20BDC /* BFTask+DotSyntax.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -389,6 +403,8 @@
files = (
8E17EC271805D0A40049E862 /* BoltsTests.m in Sources */,
8E8C8F2917F241FF00E3F1C7 /* TaskTests.m in Sources */,
1FA01DD818A06A5400D20BDC /* DotSyntaxedTaskTests.m in Sources */,
1FA01DD318A0651600D20BDC /* BFTask+DotSyntax.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -398,6 +414,7 @@
files = (
8EA6BF671805CED500337041 /* Bolts.m in Sources */,
8E9C3D2217DEA35700427E62 /* BFTask.m in Sources */,
1FA01DD018A0651600D20BDC /* BFTask+DotSyntax.m in Sources */,
8EB2CB421805DDDB00323385 /* BFExecutor.m in Sources */,
8E9C3D2417DEA35700427E62 /* BFTaskCompletionSource.m in Sources */,
);
Expand All @@ -409,6 +426,7 @@
files = (
8E42701E1805E59A000B84ED /* BFExecutor.m in Sources */,
8E17EC261805D0960049E862 /* Bolts.m in Sources */,
1FA01DD118A0651600D20BDC /* BFTask+DotSyntax.m in Sources */,
8EDDA62D17E17DDC00655F8A /* BFTask.m in Sources */,
8EDDA62E17E17DDC00655F8A /* BFTaskCompletionSource.m in Sources */,
);
Expand Down
26 changes: 26 additions & 0 deletions Bolts/BFTask+DotSyntax.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// BFTask+DotSyntax.h
// Bolts
//
// Created by Yasuhiro Inami on 2014/02/04.
// Copyright (c) 2014年 Parse Inc. All rights reserved.
//

#import "BFTask.h"

@interface BFTask (DotSyntax)

+ (BFTask * (^)(id result))taskWithResult;
+ (BFTask * (^)(NSError *error))taskWithError;
+ (BFTask * (^)(NSException *exception))taskWithException;
+ (BFTask * (^)(NSArray *result))taskForCompletionOfAllTasks;
+ (BFTask * (^)(int millis))taskWithDelay;

- (BFTask * (^)(BFContinuationBlock block))continueWithBlock;
- (BFTask * (^)(BFContinuationBlock block))continueWithSuccessBlock;

- (BFTask * (^)(BFExecutor *executor))continueWithExecutor;
- (BFTask * (^)(BFContinuationBlock block))withBlock;
- (BFTask * (^)(BFContinuationBlock block))withSuccessBlock;

@end
80 changes: 80 additions & 0 deletions Bolts/BFTask+DotSyntax.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// BFTask+DotSyntax.m
// Bolts
//
// Created by Yasuhiro Inami on 2014/02/04.
// Copyright (c) 2014年 Parse Inc. All rights reserved.
//

#import "BFTask+DotSyntax.h"

@implementation BFTask (DotSyntax)

+ (BFTask * (^)(id))taskWithResult
{
return ^BFTask*(id result) {
return [self taskWithResult:result];
};
}

+ (BFTask * (^)(NSError *))taskWithError
{
return ^BFTask*(NSError *error) {
return [self taskWithError:error];
};
}

+ (BFTask * (^)(NSException *))taskWithException
{
return ^BFTask*(NSException *exception) {
return [self taskWithException:exception];
};
}

+ (BFTask * (^)(NSArray *))taskForCompletionOfAllTasks
{
return ^BFTask*(NSArray *result) {
return [self taskForCompletionOfAllTasks:result];
};
}

+ (BFTask * (^)(int))taskWithDelay
{
return ^BFTask*(int millis) {
return [self taskWithDelay:millis];
};
}

- (BFTask * (^)(BFContinuationBlock block))continueWithBlock
{
return ^BFTask*(BFContinuationBlock block) {
return [self continueWithBlock:block];
};
}

- (BFTask * (^)(BFContinuationBlock block))continueWithSuccessBlock
{
return ^BFTask*(BFContinuationBlock block) {
return [self continueWithSuccessBlock:block];
};
}

- (BFTask * (^)(BFExecutor *executor))continueWithExecutor
{
return ^BFTask*(BFExecutor *executor) {
self.executor = executor;
return self;
};
}

- (BFTask * (^)(BFContinuationBlock block))withBlock
{
return self.continueWithBlock;
}

- (BFTask * (^)(BFContinuationBlock block))withSuccessBlock
{
return self.continueWithSuccessBlock;
}

@end
2 changes: 2 additions & 0 deletions Bolts/BFTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ typedef id(^BFContinuationBlock)(BFTask *task);
*/
+ (BFTask *)taskWithDelay:(int)millis;

@property (nonatomic) BFExecutor *executor;

// Properties that will be set on the task once it is completed.

/*!
Expand Down
4 changes: 3 additions & 1 deletion Bolts/BFTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ - (id)init {
self.lock = [[NSObject alloc] init];
self.condition = [[NSCondition alloc] init];
self.callbacks = [NSMutableArray array];
self.executor = [BFExecutor defaultExecutor];
}
return self;
}
Expand Down Expand Up @@ -264,11 +265,12 @@ - (void)runContinuations {

- (BFTask *)continueWithExecutor:(BFExecutor *)executor
withBlock:(BFContinuationBlock)block {
self.executor = executor;
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];

// Capture all of the state that needs to used when the continuation is complete.
void (^wrappedBlock)() = ^() {
[executor execute:^{
[self.executor execute:^{
id result = nil;
@try {
result = block(self);
Expand Down
1 change: 1 addition & 0 deletions Bolts/Bolts.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "BoltsVersion.h"
#import "BFExecutor.h"
#import "BFTask.h"
#import "BFTask+DotSyntax.h"
#import "BFTaskCompletionSource.h"

/*! @abstract 80175001: There were multiple errors. */
Expand Down
64 changes: 64 additions & 0 deletions BoltsTests/DotSyntaxedTaskTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#import <XCTest/XCTest.h>

#import "Bolts.h"

@interface DotSyntaxedTaskTests : XCTestCase
@end

@implementation DotSyntaxedTaskTests

- (void)setUp {
[super setUp];
}

- (void)tearDown {
[super tearDown];
}

- (void)testPassOnError {
NSError *originalError = [NSError errorWithDomain:@"Bolts" code:30 userInfo:nil];
[BFTask.taskWithError(originalError).continueWithSuccessBlock(^id(BFTask *task) {
XCTFail(@"This callback should be skipped.");
return nil;
}).continueWithSuccessBlock(^id(BFTask *task) {
XCTFail(@"This callback should be skipped.");
return nil;
}).continueWithBlock(^id(BFTask *task) {
XCTAssertNotNil(task.error);
XCTAssertEqual((NSInteger)30, task.error.code);
NSError *newError = [NSError errorWithDomain:@"Bolts" code:31 userInfo:nil];
return BFTask.taskWithError(newError);
}).continueWithSuccessBlock(^id(BFTask *task) {
XCTFail(@"This callback should be skipped.");
return nil;
}).continueWithBlock(^id(BFTask *task) {
XCTAssertNotNil(task.error);
XCTAssertEqual((NSInteger)31, task.error.code);
return BFTask.taskWithResult(@"okay");
}).continueWithSuccessBlock(^id(BFTask *task) {
XCTAssertEqualObjects(@"okay", task.result);
return nil;
}) waitUntilFinished];
}

- (void)testExecuteImmediately {
XCTAssertTrue([NSThread isMainThread]);
BFTask *task = [BFTask taskWithResult:nil];
task = task.continueWithExecutor([BFExecutor immediateExecutor]).withBlock(^id(BFTask *task) {
XCTAssertTrue([NSThread isMainThread]);
return nil;
});
XCTAssertTrue(task.isCompleted);
}

@end