Created
February 21, 2020 17:59
-
-
Save chunkyguy/0f67f074c974aac7024d153011e5400e to your computer and use it in GitHub Desktop.
Rx with ObjectiveC using NSInvocation
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
#import "RxObservable.h" | |
@interface RxArraySource : RxObservable | |
+ (instancetype)createWithElements: (NSArray *)array; | |
- (void)subscribe:(NSInvocation *)invocation; | |
@end |
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
#import "RxArraySource.h" | |
@interface RxArraySource () | |
@property (nonatomic, copy) NSArray *elements; | |
@end | |
@implementation RxArraySource | |
+ (instancetype)createWithElements:(NSArray *)elements; | |
{ | |
RxArraySource *instance = [RxArraySource new]; | |
instance.elements = elements; | |
return instance; | |
} | |
- (void)subscribe:(NSInvocation *)invocation; | |
{ | |
for (id element in _elements) { | |
[invocation setArgument:&element atIndex:2]; | |
[invocation invoke]; | |
} | |
} | |
@end |
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
@interface RxDriver : NSObject | |
- (void)run; | |
@end |
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
#import "RxDriver.h" | |
#import "NSInvocation+RxConvenience.h" | |
#import "RxObservable.h" | |
@implementation RxDriver | |
- (void)print:(NSNumber *)num | |
{ | |
NSLog(@"%@", num); | |
} | |
- (NSNumber *)tenTimesOfNumber:(NSNumber *)num | |
{ | |
return [NSNumber numberWithInteger:[num integerValue] * 10]; | |
} | |
- (void)run | |
{ | |
NSInvocation *verbose = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(print:)]]; | |
[verbose setTarget:self]; | |
[verbose setSelector:@selector(print:)]; | |
RxObservable *stream = [RxObservable observableFromArray:@[@1, @2, @3]]; | |
// Simple subscribe | |
NSInvocation *printInvoc = [NSInvocation invocationWithTarget:self | |
selector:@selector(print:)]; | |
[stream subscribe:printInvoc]; | |
// Square | |
NSInvocation *mapInvoc = [NSInvocation invocationWithTarget:self | |
selector:@selector(tenTimesOfNumber:)]; | |
[[stream mapWithInvocation:mapInvoc] subscribe:printInvoc]; | |
} | |
@end |
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
#import "RxObservable.h" | |
@interface RxMapOperator : RxObservable | |
+ (instancetype) operatorFromStream:(RxObservable *)upstream | |
withInvocation:(NSInvocation *)invoc; | |
@end |
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
#import "RxMapOperator.h" | |
#import "NSInvocation+RxConvenience.h" | |
@interface RxMapOperator () | |
@property (nonatomic, strong) RxObservable *stream; | |
@property (nonatomic, strong) NSInvocation *invocUp; | |
@property (nonatomic, strong) NSInvocation *invocDown; | |
@end | |
@implementation RxMapOperator | |
+ (instancetype) operatorFromStream:(RxObservable *)stream | |
withInvocation:(NSInvocation *)invoc; | |
{ | |
RxMapOperator *instance = [RxMapOperator new]; | |
instance.stream = stream; | |
instance.invocUp = invoc; | |
return instance; | |
} | |
- (void)transform:(id)arg | |
{ | |
[self.invocUp setArgument:&arg atIndex:2]; | |
[self.invocUp invoke]; | |
id ret; | |
[self.invocUp getReturnValue:&ret]; | |
[self.invocDown setArgument:&ret atIndex:2]; | |
[self.invocDown invoke]; | |
} | |
- (void)subscribe:(NSInvocation *)invocation | |
{ | |
self.invocDown = invocation; | |
[self.stream subscribe:[NSInvocation invocationWithTarget:self selector:@selector(transform:)]]; | |
} | |
@end |
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
@interface RxObservable : NSObject | |
+ (instancetype)observableFromArray:(NSArray *)array; | |
- (instancetype)mapWithInvocation:(NSInvocation *)invo; | |
- (void)subscribe:(NSInvocation *)invocation; | |
@end |
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
#import "RxObservable.h" | |
#import "RxArraySource.h" | |
#import "RxMapOperator.h" | |
@implementation RxObservable | |
+ (instancetype)observableFromArray:(NSArray *)array; | |
{ | |
return [RxArraySource createWithElements:array]; | |
} | |
- (instancetype)mapWithInvocation:(NSInvocation *)invo | |
{ | |
return [RxMapOperator operatorFromStream:self withInvocation:invo]; | |
} | |
- (void)subscribe:(NSInvocation *)invocation; | |
{} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment