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 Task Cancellation #89

Merged
merged 1 commit into from
May 20, 2015
Merged
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
42 changes: 42 additions & 0 deletions Bolts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
1EC3017118CDAA8400D06D07 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EC3017018CDAA8400D06D07 /* AppDelegate.m */; };
1EC3017318CDAA8400D06D07 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1EC3017218CDAA8400D06D07 /* Images.xcassets */; };
1EC3019118CDABCE00D06D07 /* AppLinkTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EC3019018CDABCE00D06D07 /* AppLinkTests.m */; };
7C60AEBF1ACF08F300747DD7 /* BFCancellationToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C60AEBD1ACF08F300747DD7 /* BFCancellationToken.h */; settings = {ATTRIBUTES = (Public, ); }; };
7C60AEC01ACF08F300747DD7 /* BFCancellationToken.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C60AEBE1ACF08F300747DD7 /* BFCancellationToken.m */; };
7C60AEC31ACF093D00747DD7 /* BFCancellationTokenSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C60AEC11ACF093D00747DD7 /* BFCancellationTokenSource.h */; settings = {ATTRIBUTES = (Public, ); }; };
7C60AEC41ACF093D00747DD7 /* BFCancellationTokenSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C60AEC21ACF093D00747DD7 /* BFCancellationTokenSource.m */; };
7C60AEC61ACF19F900747DD7 /* CancellationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C60AEC51ACF19F900747DD7 /* CancellationTests.m */; };
7C60AEC71ACF19FD00747DD7 /* CancellationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C60AEC51ACF19F900747DD7 /* CancellationTests.m */; };
7C60AEC81ACF1A0100747DD7 /* BFCancellationToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C60AEBD1ACF08F300747DD7 /* BFCancellationToken.h */; settings = {ATTRIBUTES = (Public, ); }; };
7C60AEC91ACF1A0900747DD7 /* BFCancellationToken.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C60AEBE1ACF08F300747DD7 /* BFCancellationToken.m */; };
7C60AECA1ACF1A0B00747DD7 /* BFCancellationTokenSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C60AEC11ACF093D00747DD7 /* BFCancellationTokenSource.h */; settings = {ATTRIBUTES = (Public, ); }; };
7C60AECB1ACF1A0D00747DD7 /* BFCancellationTokenSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C60AEC21ACF093D00747DD7 /* BFCancellationTokenSource.m */; };
7CA39C921ADE715400DD78CC /* BFCancellationTokenRegistration.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CA39C901ADE715400DD78CC /* BFCancellationTokenRegistration.h */; settings = {ATTRIBUTES = (Public, ); }; };
7CA39C931ADE715400DD78CC /* BFCancellationTokenRegistration.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CA39C901ADE715400DD78CC /* BFCancellationTokenRegistration.h */; settings = {ATTRIBUTES = (Public, ); }; };
7CA39C941ADE715400DD78CC /* BFCancellationTokenRegistration.m in Sources */ = {isa = PBXBuildFile; fileRef = 7CA39C911ADE715400DD78CC /* BFCancellationTokenRegistration.m */; };
7CA39C951ADE715400DD78CC /* BFCancellationTokenRegistration.m in Sources */ = {isa = PBXBuildFile; fileRef = 7CA39C911ADE715400DD78CC /* BFCancellationTokenRegistration.m */; };
8103FA6819900A84000BAE3F /* BFExecutor.m in Sources */ = {isa = PBXBuildFile; fileRef = 8103FA4F19900A84000BAE3F /* BFExecutor.m */; };
8103FA6919900A84000BAE3F /* BFExecutor.m in Sources */ = {isa = PBXBuildFile; fileRef = 8103FA4F19900A84000BAE3F /* BFExecutor.m */; };
8103FA6A19900A84000BAE3F /* BFTask.m in Sources */ = {isa = PBXBuildFile; fileRef = 8103FA5119900A84000BAE3F /* BFTask.m */; };
Expand Down Expand Up @@ -101,6 +115,13 @@
1EC3017018CDAA8400D06D07 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
1EC3017218CDAA8400D06D07 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
1EC3019018CDABCE00D06D07 /* AppLinkTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppLinkTests.m; sourceTree = "<group>"; };
7C60AEBD1ACF08F300747DD7 /* BFCancellationToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFCancellationToken.h; sourceTree = "<group>"; };
7C60AEBE1ACF08F300747DD7 /* BFCancellationToken.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFCancellationToken.m; sourceTree = "<group>"; };
7C60AEC11ACF093D00747DD7 /* BFCancellationTokenSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFCancellationTokenSource.h; sourceTree = "<group>"; };
7C60AEC21ACF093D00747DD7 /* BFCancellationTokenSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFCancellationTokenSource.m; sourceTree = "<group>"; };
7C60AEC51ACF19F900747DD7 /* CancellationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CancellationTests.m; sourceTree = "<group>"; };
7CA39C901ADE715400DD78CC /* BFCancellationTokenRegistration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFCancellationTokenRegistration.h; sourceTree = "<group>"; };
7CA39C911ADE715400DD78CC /* BFCancellationTokenRegistration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFCancellationTokenRegistration.m; sourceTree = "<group>"; };
8103FA4E19900A84000BAE3F /* BFExecutor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFExecutor.h; sourceTree = "<group>"; };
8103FA4F19900A84000BAE3F /* BFExecutor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFExecutor.m; sourceTree = "<group>"; };
8103FA5019900A84000BAE3F /* BFTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFTask.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -236,6 +257,12 @@
8103FA5319900A84000BAE3F /* BFTaskCompletionSource.m */,
8103FA4E19900A84000BAE3F /* BFExecutor.h */,
8103FA4F19900A84000BAE3F /* BFExecutor.m */,
7C60AEBD1ACF08F300747DD7 /* BFCancellationToken.h */,
7C60AEBE1ACF08F300747DD7 /* BFCancellationToken.m */,
7CA39C901ADE715400DD78CC /* BFCancellationTokenRegistration.h */,
7CA39C911ADE715400DD78CC /* BFCancellationTokenRegistration.m */,
7C60AEC11ACF093D00747DD7 /* BFCancellationTokenSource.h */,
7C60AEC21ACF093D00747DD7 /* BFCancellationTokenSource.m */,
);
path = Common;
sourceTree = "<group>";
Expand Down Expand Up @@ -274,6 +301,7 @@
1EC3019018CDABCE00D06D07 /* AppLinkTests.m */,
8EA6BF681805CF5600337041 /* BoltsTests.m */,
8E9C3D1C17DE9F6500427E62 /* TaskTests.m */,
7C60AEC51ACF19F900747DD7 /* CancellationTests.m */,
85D5138918E4E45800D19D87 /* AppLinkReturnToRefererViewTests.m */,
8E8C8ED817F23C3B00E3F1C7 /* Supporting Files */,
);
Expand Down Expand Up @@ -364,24 +392,30 @@
81D0EE9019AFAA5F0000AE75 /* BFAppLinkReturnToRefererView.h in Headers */,
81D0EE8719AFAA220000AE75 /* BFExecutor.h in Headers */,
81D0EE8919AFAA2B0000AE75 /* BFTaskCompletionSource.h in Headers */,
7C60AEC31ACF093D00747DD7 /* BFCancellationTokenSource.h in Headers */,
81D0EE9119AFAA6F0000AE75 /* BFAppLinkTarget.h in Headers */,
81D0EE8019AFA9E20000AE75 /* BoltsVersion.h in Headers */,
81D0EE8C19AFAA5F0000AE75 /* BFAppLink.h in Headers */,
81D0EE9219AFAA6F0000AE75 /* BFMeasurementEvent.h in Headers */,
81D0EE9319AFAA6F0000AE75 /* BFURL.h in Headers */,
7CA39C921ADE715400DD78CC /* BFCancellationTokenRegistration.h in Headers */,
81D0EE8419AFAA100000AE75 /* Bolts.h in Headers */,
7C60AEBF1ACF08F300747DD7 /* BFCancellationToken.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
81D0EE8119AFA9EE0000AE75 /* Headers */ = {
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
7CA39C931ADE715400DD78CC /* BFCancellationTokenRegistration.h in Headers */,
81D0EE8519AFAA190000AE75 /* BFTask.h in Headers */,
7C60AECA1ACF1A0B00747DD7 /* BFCancellationTokenSource.h in Headers */,
81D0EE8819AFAA240000AE75 /* BFExecutor.h in Headers */,
81D0EE8A19AFAA2C0000AE75 /* BFTaskCompletionSource.h in Headers */,
81D0EE8219AFAA060000AE75 /* BoltsVersion.h in Headers */,
81D0EE8319AFAA0E0000AE75 /* Bolts.h in Headers */,
7C60AEC81ACF1A0100747DD7 /* BFCancellationToken.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -556,6 +590,7 @@
buildActionMask = 2147483647;
files = (
1EC3019118CDABCE00D06D07 /* AppLinkTests.m in Sources */,
7C60AEC61ACF19F900747DD7 /* CancellationTests.m in Sources */,
85D5138A18E4E45800D19D87 /* AppLinkReturnToRefererViewTests.m in Sources */,
8EA6BF691805CF5600337041 /* BoltsTests.m in Sources */,
8E8C8EFB17F23E5F00E3F1C7 /* TaskTests.m in Sources */,
Expand All @@ -568,6 +603,7 @@
files = (
8E17EC271805D0A40049E862 /* BoltsTests.m in Sources */,
8E8C8F2917F241FF00E3F1C7 /* TaskTests.m in Sources */,
7C60AEC71ACF19FD00747DD7 /* CancellationTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -576,7 +612,9 @@
buildActionMask = 2147483647;
files = (
8103FA7819900A84000BAE3F /* BFAppLinkTarget.m in Sources */,
7CA39C941ADE715400DD78CC /* BFCancellationTokenRegistration.m in Sources */,
8103FA6C19900A84000BAE3F /* BFTaskCompletionSource.m in Sources */,
7C60AEC01ACF08F300747DD7 /* BFCancellationToken.m in Sources */,
8103FA7619900A84000BAE3F /* BFAppLinkReturnToRefererView.m in Sources */,
8103FA7419900A84000BAE3F /* BFAppLinkReturnToRefererController.m in Sources */,
8103FA7C19900A84000BAE3F /* BFWebViewAppLinkResolver.m in Sources */,
Expand All @@ -585,6 +623,7 @@
8103FA7019900A84000BAE3F /* BFAppLink.m in Sources */,
8103FA6819900A84000BAE3F /* BFExecutor.m in Sources */,
8103FA7A19900A84000BAE3F /* BFURL.m in Sources */,
7C60AEC41ACF093D00747DD7 /* BFCancellationTokenSource.m in Sources */,
8103FA7219900A84000BAE3F /* BFAppLinkNavigation.m in Sources */,
B242FABB19A567660097ECAE /* BFMeasurementEvent.m in Sources */,
);
Expand All @@ -597,7 +636,10 @@
8103FA6D19900A84000BAE3F /* BFTaskCompletionSource.m in Sources */,
8103FA6B19900A84000BAE3F /* BFTask.m in Sources */,
8103FA6F19900A84000BAE3F /* Bolts.m in Sources */,
7CA39C951ADE715400DD78CC /* BFCancellationTokenRegistration.m in Sources */,
7C60AECB1ACF1A0D00747DD7 /* BFCancellationTokenSource.m in Sources */,
8103FA6919900A84000BAE3F /* BFExecutor.m in Sources */,
7C60AEC91ACF1A0900747DD7 /* BFCancellationToken.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
38 changes: 38 additions & 0 deletions Bolts/Common/BFCancellationToken.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 <Foundation/Foundation.h>

#import <Bolts/BFCancellationTokenRegistration.h>

/*!
A block that will be called when a token is cancelled.
*/
typedef void(^BFCancellationBlock)();

/*!
The consumer view of a CancellationToken. Propagates notification that
operations should be canceled.
A BFCancellationToken has methods to inspect whether the token has been cancelled.
*/
@interface BFCancellationToken : NSObject

/*!
Whether cancellation has been requested for this token source.
*/
@property (nonatomic, assign, readonly, getter = isCancellationRequested) BOOL cancellationRequested;

/*!
Register a block to be notified when the token is cancelled.
If the token is already cancelled the delegate will be notified immediately.
*/
- (BFCancellationTokenRegistration *)registerCancellationObserverWithBlock:(BFCancellationBlock)block;

@end
135 changes: 135 additions & 0 deletions Bolts/Common/BFCancellationToken.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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 "BFCancellationToken.h"
#import "BFCancellationTokenRegistration.h"

@interface BFCancellationToken ()

@property (atomic, assign, getter = isCancellationRequested) BOOL cancellationRequested;
@property (nonatomic, strong) NSMutableArray *registrations;
@property (nonatomic, strong) NSObject *lock;
@property (nonatomic) BOOL disposed;

@end

@interface BFCancellationTokenRegistration (BFCancellationToken)

+ (instancetype)registrationWithToken:(BFCancellationToken *)token delegate:(BFCancellationBlock)delegate;

- (void)notifyDelegate;

@end

@implementation BFCancellationToken

#pragma mark - Initializer

- (instancetype)init {
if (self = [super init]) {
_registrations = [NSMutableArray array];
_lock = [NSObject new];
}
return self;
}

#pragma mark - Custom Setters/Getters

- (BOOL)isCancellationRequested {
@synchronized(self.lock) {
[self throwIfDisposed];
return _cancellationRequested;
}
}

- (void)cancel {
NSArray *registrations;
@synchronized(self.lock) {
[self throwIfDisposed];
if (_cancellationRequested) {
return;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(cancelPrivate) object:nil];
_cancellationRequested = YES;
registrations = [self.registrations copy];
}

[self notifyCancellation:registrations];
}

- (void)notifyCancellation:(NSArray *)registrations {
for (BFCancellationTokenRegistration *registration in registrations) {
[registration notifyDelegate];
}
}

- (BFCancellationTokenRegistration *)registerCancellationObserverWithBlock:(BFCancellationBlock)block {
@synchronized(self.lock) {
BFCancellationTokenRegistration *registration = [BFCancellationTokenRegistration registrationWithToken:self delegate:[block copy]];
[self.registrations addObject:registration];

return registration;
}
}

- (void)unregisterRegistration:(BFCancellationTokenRegistration *)registration {
@synchronized(self.lock) {
[self throwIfDisposed];
[self.registrations removeObject:registration];
}
}

// Delay on a non-public method to prevent interference with a user calling performSelector or
// cancelPreviousPerformRequestsWithTarget on the public method
- (void)cancelPrivate {
[self cancel];
}

- (void)cancelAfterDelay:(int)millis {
[self throwIfDisposed];
NSAssert(millis >= -1, @"Delay must be >= -1");

if (millis == 0) {
[self cancel];
return;
}

@synchronized(self.lock) {
[self throwIfDisposed];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(cancelPrivate) object:nil];
if (self.cancellationRequested) {
return;
}

if (millis != -1) {
double delay = (double) millis / 1000;
[self performSelector:@selector(cancelPrivate) withObject:nil afterDelay:delay];
}
}
}

- (void)dispose {
@synchronized(self.lock) {
if (self.disposed) {
return;
}
self.disposed = YES;
for (BFCancellationTokenRegistration *registration in self.registrations) {
[registration dispose];
}
[self.registrations removeAllObjects];
}
}

- (void)throwIfDisposed {
NSAssert(!self.disposed, @"Object already disposed");
}

@end
25 changes: 25 additions & 0 deletions Bolts/Common/BFCancellationTokenRegistration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 <Foundation/Foundation.h>

/*!
Represents the registration of a cancellation observer with a cancellation token.
Can be used to unregister the observer at a later time.
*/
@interface BFCancellationTokenRegistration : NSObject

/*!
Removes the cancellation observer registered with the token and releases all resources associated
with this registration.
*/
- (void)dispose;

@end
Loading