Created
October 24, 2015 00:20
-
-
Save stanislaw/4e8afb1b7946f6463ef6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// COBasicSequence.m | |
// DevelopmentApp | |
// | |
// Created by Stanislaw Pankevich on 24/10/15. | |
// Copyright © 2015 Stanislaw Pankevich. All rights reserved. | |
// | |
#import "Sequence.h" | |
#import <CompositeOperations/COSimpleOperation.h> | |
// COS - composite operation step | |
#define COSInitial @"NSNull" | |
#define COSFinal nil | |
#define COS(name) NSStringFromClass([name class]) ?: COSInitial | |
typedef id (^Generator)(id); | |
@interface Operation1 : COSimpleOperation; @end | |
@interface Operation2 : COSimpleOperation; @end | |
@interface Operation3 : COSimpleOperation; @end | |
@implementation Operation1 | |
- (void)main { [self finishWithResult:@(1)]; } | |
@end | |
@implementation Operation2 | |
- (void)main { [self finishWithResult:@(2)]; } | |
@end | |
@implementation Operation3; | |
- (void)main { [self finishWithResult:@(3)]; } | |
@end | |
@interface Sequence () | |
@end | |
@implementation Sequence | |
- (NSDictionary *)map { | |
return @{ | |
COSInitial: ^id(id _){ | |
return [Operation1 new]; | |
}, | |
COS(Operation1): ^id(id _){ | |
return [Operation2 new]; | |
}, | |
COS(Operation2): ^id(id _){ | |
return [Operation3 new]; | |
}, | |
COS(Operation3): ^id(id _){ | |
return COSFinal; | |
}, | |
}; | |
} | |
- (NSOperation<COOperation> *)nextOperationAfterOperation:(NSOperation<COOperation> *)previousOperationOrNil { | |
NSString *step = COS([previousOperationOrNil class]); | |
Generator generator = self.map[step]; | |
NSAssert(generator, nil); | |
id nextOperation = generator(previousOperationOrNil); | |
return nextOperation; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment