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

Reduce stack frame from continuation stack trace if task is completed. #237

Merged
merged 1 commit into from
Mar 11, 2016
Merged
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
72 changes: 36 additions & 36 deletions Bolts/Common/BFTask.m
Original file line number Diff line number Diff line change
@@ -318,59 +318,59 @@ - (BFTask *)continueWithExecutor:(BFExecutor *)executor
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];

// Capture all of the state that needs to used when the continuation is complete.
void (^wrappedBlock)() = ^() {
[executor execute:^{
if (cancellationToken.cancellationRequested) {
[tcs cancel];
return;
}

id result = nil;
@try {
result = block(self);
} @catch (NSException *exception) {
tcs.exception = exception;
return;
}

if ([result isKindOfClass:[BFTask class]]) {
dispatch_block_t executionBlock = ^{
if (cancellationToken.cancellationRequested) {
[tcs cancel];
return;
}

id (^setupWithTask) (BFTask *) = ^id(BFTask *task) {
if (cancellationToken.cancellationRequested || task.cancelled) {
[tcs cancel];
} else if (task.exception) {
tcs.exception = task.exception;
} else if (task.error) {
tcs.error = task.error;
} else {
tcs.result = task.result;
}
return nil;
};
id result = nil;
@try {
result = block(self);
} @catch (NSException *exception) {
tcs.exception = exception;
return;
}

BFTask *resultTask = (BFTask *)result;
if ([result isKindOfClass:[BFTask class]]) {

if (resultTask.completed) {
setupWithTask(resultTask);
id (^setupWithTask) (BFTask *) = ^id(BFTask *task) {
if (cancellationToken.cancellationRequested || task.cancelled) {
[tcs cancel];
} else if (task.exception) {
tcs.exception = task.exception;
} else if (task.error) {
tcs.error = task.error;
} else {
[resultTask continueWithBlock:setupWithTask];
tcs.result = task.result;
}
return nil;
};

BFTask *resultTask = (BFTask *)result;

if (resultTask.completed) {
setupWithTask(resultTask);
} else {
tcs.result = result;
[resultTask continueWithBlock:setupWithTask];
}
}];

} else {
tcs.result = result;
}
};

BOOL completed;
@synchronized(self.lock) {
completed = self.completed;
if (!completed) {
[self.callbacks addObject:[wrappedBlock copy]];
[self.callbacks addObject:[^{
[executor execute:executionBlock];
} copy]];
}
}
if (completed) {
wrappedBlock();
[executor execute:executionBlock];
}

return tcs.task;