Skip to content

Commit

Permalink
Hide RACCommand's subscriber behavior in a private RACSubject
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Mar 13, 2013
1 parent d102887 commit ce7fd2f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
14 changes: 12 additions & 2 deletions ReactiveCocoaFramework/ReactiveCocoa/RACCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//

#import <Foundation/Foundation.h>
#import <ReactiveCocoa/RACSubject.h>
#import <ReactiveCocoa/RACSignal.h>

// A command is a signal triggered in response to some action, typically
// UI-related.
//
// Each `next` sent by a RACCommand corresponds to a value passed to -execute:.
@interface RACCommand : RACSubject
@interface RACCommand : RACSignal

// Whether or not this command can currently execute.
//
Expand Down Expand Up @@ -95,3 +95,13 @@
- (BOOL)execute:(id)value;

@end

@interface RACCommand (Deprecated)

- (void)sendNext:(id)value __attribute__((deprecated("Commands should not be manually controlled")));
- (void)sendError:(NSError *)error __attribute__((deprecated("Commands should not be manually controlled")));
- (void)sendCompleted __attribute__((deprecated("Commands should not be manually controlled")));
- (void)didSubscribeWithDisposable:(RACDisposable *)disposable __attribute__((deprecated("Commands should not be manually controlled")));
+ (instancetype)subject __attribute__((deprecated("Use +command instead")));

@end
54 changes: 52 additions & 2 deletions ReactiveCocoaFramework/ReactiveCocoa/RACCommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "EXTScope.h"
#import "RACScheduler.h"
#import "RACSignal+Operations.h"
#import "RACSubject.h"
#import "RACSubscriptingAssignmentTrampoline.h"
#import <libkern/OSAtomic.h>

Expand All @@ -25,6 +26,11 @@ @interface RACCommand () {

@property (atomic, readwrite) BOOL canExecute;

// A signal of the values passed to -execute:.
//
// Subscriptions to the receiver will actually be redirected to this subject.
@property (nonatomic, strong, readonly) RACSubject *values;

// Improves the performance of KVO on the receiver.
//
// See the documentation for <NSKeyValueObserving> for more information.
Expand Down Expand Up @@ -63,6 +69,14 @@ - (void)decrementItemsInFlight {
[self didChangeValueForKey:@keypath(self.executing)];
}

- (NSString *)name {
return self.values.name;
}

- (void)setName:(NSString *)name {
self.values.name = name;
}

#pragma mark Lifecycle

+ (instancetype)command {
Expand All @@ -81,6 +95,7 @@ - (id)initWithCanExecuteSignal:(RACSignal *)canExecuteSignal {
self = [super init];
if (self == nil) return nil;

_values = [RACSubject subject];
_errors = [RACSubject subject];

RAC(self.canExecute) = [RACSignal
Expand All @@ -103,7 +118,7 @@ - (RACSignal *)addSignalBlock:(RACSignal * (^)(id value))signalBlock {

@weakify(self);

return [[[[self
return [[[[self.values
doNext:^(id _) {
@strongify(self);
[self incrementItemsInFlight];
Expand Down Expand Up @@ -138,12 +153,18 @@ - (BOOL)execute:(id)value {
[self incrementItemsInFlight];
}

[self sendNext:value];
[self.values sendNext:value];
[self decrementItemsInFlight];

return YES;
}

#pragma mark RACSignal

- (RACDisposable *)subscribe:(id<RACSubscriber>)subscriber {
return [self.values subscribe:subscriber];
}

#pragma mark NSKeyValueObserving

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
Expand All @@ -154,3 +175,32 @@ + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
}

@end

@implementation RACCommand (Deprecated)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"

- (void)sendNext:(id)value {
[self.values sendNext:value];
}

- (void)sendError:(NSError *)error {
[self.values sendError:error];
}

- (void)sendCompleted {
[self.values sendCompleted];
}

- (void)didSubscribeWithDisposable:(RACDisposable *)disposable {
[self.values didSubscribeWithDisposable:disposable];
}

+ (instancetype)subject {
return [self command];
}

#pragma clang diagnostic pop

@end
1 change: 1 addition & 0 deletions ReactiveCocoaFramework/ReactiveCocoaTests/RACCommandSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "RACScheduler.h"
#import "RACSequence.h"
#import "RACSignal+Operations.h"
#import "RACSubject.h"
#import "RACUnit.h"

SpecBegin(RACCommand)
Expand Down

0 comments on commit ce7fd2f

Please sign in to comment.