Commit 91e65606 by Dima Bart

Refactor BUYCheckoutOperation.

parent e1811bb7
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
@property (strong, nonatomic, readonly) id<BUYPaymentToken> token; @property (strong, nonatomic, readonly) id<BUYPaymentToken> token;
@property (strong, nonatomic, readonly) BUYCheckoutOperationCompletion completion; @property (strong, nonatomic, readonly) BUYCheckoutOperationCompletion completion;
@property (strong, atomic) BUYRequestOperation *currentOperation; @property (strong, atomic) NSArray *operations;
@end @end
...@@ -66,12 +66,22 @@ ...@@ -66,12 +66,22 @@
- (void)finishWithCheckout:(BUYCheckout *)checkout - (void)finishWithCheckout:(BUYCheckout *)checkout
{ {
if (self.isCancelled) {
return;
}
[self finishExecution]; [self finishExecution];
self.completion(checkout, nil); self.completion(checkout, nil);
} }
- (void)finishWithError:(NSError *)error - (void)finishWithError:(NSError *)error
{ {
if (self.isCancelled) {
return;
}
[self cancelAllOperations];
[self finishExecution]; [self finishExecution];
self.completion(nil, error); self.completion(nil, error);
} }
...@@ -86,64 +96,78 @@ ...@@ -86,64 +96,78 @@
[super startExecution]; [super startExecution];
__weak typeof(self) weakSelf = self; BUYRequestOperation *beginOperation = [self createBeginOperation];
BUYRequestOperation *pollOperation = [self createPollOperation];
BUYRequestOperation *getOperation = [self createGetOperation];
BUYRequestOperation *beginOperation = [weakSelf.client beginCheckout:weakSelf.checkout paymentToken:weakSelf.token completion:^(BUYCheckout *checkout, NSError *error) { [pollOperation addDependency:beginOperation];
if (weakSelf.isCancelled) { [getOperation addDependency:pollOperation];
return;
}
if (!checkout) { self.operations = @[
[weakSelf finishWithError:error]; beginOperation,
return; pollOperation,
} getOperation,
];
BUYRequestOperation *pollOperation = [weakSelf.client getCompletionStatusOfCheckoutToken:weakSelf.checkout.token start:NO completion:^(BUYStatus status, NSError *error) { [self startAllOperations];
if (weakSelf.isCancelled) { }
return;
}
if (status != BUYStatusComplete) { - (void)cancelExecution
[weakSelf finishWithError:error]; {
return; [super cancelExecution];
} [self cancelAllOperations];
}
BUYRequestOperation *getOperation = [weakSelf.client getCheckout:weakSelf.checkout start:NO completion:^(BUYCheckout *checkout, NSError *error) { #pragma mark - Start / Stop -
if (weakSelf.isCancelled) {
return; - (void)startAllOperations
{
for (BUYRequestOperation *operation in self.operations) {
[self.client startOperation:operation];
} }
}
if (checkout) { - (void)cancelAllOperations
[weakSelf finishWithCheckout:checkout]; {
} else { for (BUYRequestOperation *operation in self.operations) {
[weakSelf finishWithError:error]; [operation cancel];
} }
}]; }
weakSelf.currentOperation = getOperation; #pragma mark - Operations -
[weakSelf.client startOperation:getOperation]; - (BUYRequestOperation *)createBeginOperation
{
return [self.client beginCheckout:self.checkout paymentToken:self.token completion:^(BUYCheckout *checkout, NSError *error) {
if (!checkout) {
[self finishWithError:error];
}
}];
}
- (BUYRequestOperation *)createPollOperation
{
BUYRequestOperation *operation =[self.client getCompletionStatusOfCheckoutToken:self.checkout.token start:NO completion:^(BUYStatus status, NSError *error) {
if (status != BUYStatusComplete) {
[self finishWithError:error];
}
}]; }];
pollOperation.pollingHandler = ^BOOL (NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { operation.pollingHandler = ^BOOL (NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
return response.statusCode == BUYStatusProcessing; return response.statusCode == BUYStatusProcessing;
}; };
weakSelf.currentOperation = pollOperation; return operation;
[weakSelf.client startOperation:pollOperation];
}];
// Update current operation
self.currentOperation = beginOperation;
[self.client startOperation:beginOperation];
} }
- (void)cancelExecution - (BUYRequestOperation *)createGetOperation
{ {
[super cancelExecution]; return [self.client getCheckout:self.checkout start:NO completion:^(BUYCheckout *checkout, NSError *error) {
[self.currentOperation cancel]; if (checkout) {
[self finishWithCheckout:checkout];
} else {
[self finishWithError:error];
}
}];
} }
@end @end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment