Skip to content

Instantly share code, notes, and snippets.

[[[NSUserDefaultsController sharedUserDefaultsController] rac_subscribableForKeyPath:@"values.yourDefaultsKey" onObject:self] subscribeNext:(id x) {
// do the things
}];
// Here, rowView is a weak pointer to the view's superview.
RAC(self.layer.contents) = [[[[RACAble(self.rowView.selected) distinctUntilChanged] injectObjectWeakly:self]
doNext:^(RACTuple *x) {
CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"contents"];
fade.duration = 0.15;
[[x.last layer] addAnimation:fade forKey:@"animateContents"];
}] select:^id(RACTuple *x) {
return ([x.first boolValue] ? [self highlightedGradient] : [self standardGradient]);
}];
RAC(self.layer.contents) = [[[[[[RACAble(self.rowView) select:^(YourRowView *rowView) {
return RACAble(rowView, selected);
}] switch] distinctUntilChanged] injectObjectWeakly:self]
doNext:^(RACTuple *x) {
CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"contents"];
fade.duration = 0.15;
[[x.last layer] addAnimation:fade forKey:@"animateContents"];
}] select:^id(RACTuple *x) {
return ([x.first boolValue] ? [self highlightedGradient] : [self standardGradient]);
}];
@joshaber
joshaber / gist:4230872
Created December 7, 2012 04:57
ReactiveCocoa stopwatch
self.startButton.rac_command = [RACCommand command];
self.stopButton.rac_command = [RACCommand command];
__unsafe_unretained id weakSelf = self;
id<RACSignal> tick = [[[[self.startButton.rac_command
map:^(id _) {
RTAFirstView *strongSelf = weakSelf;
// Map each start button click to a new signal that fires every second
// and stops when the stop button is clicked.
return [[RACSignal interval:1] takeUntil:strongSelf.stopButton.rac_command];
@joshaber
joshaber / gist:4238342
Created December 8, 2012 02:54
ReactiveCocoa stopwatch with reset
self.startButton.rac_command = [RACCommand command];
self.stopButton.rac_command = [RACCommand command];
self.resetButton.rac_command = [RACCommand command];
static const CGFloat interval = 0.01;
__unsafe_unretained id weakSelf = self;
// Signal id -> Signal Signal Number
// Map each click of the start button to a signal that fires at our interval
// and stops when the stop button's clicked.
id<RACSignal> start = [self.startButton.rac_command map:^(id _) {
@joshaber
joshaber / gist:4394046
Created December 28, 2012 02:49
NSEvent+RACSignals
@implementation NSEvent (RACSignals)
+ (id<RACSignal>)rac_signalForEventsMatchingMask:(NSEventMask)eventMask {
return [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
id monitor = [NSEvent addLocalMonitorForEventsMatchingMask:eventMask handler:^(NSEvent *event) {
[subscriber sendNext:event];
return event;
}];
return [RACDisposable disposableWithBlock:^{
[[[[[[[[[[self.searchTextField
rac_textSignal]
filter:^BOOL(NSString *value) {
return value.length > 2;
}]
doNext:^(NSString *x) {
NSLog(@"[%@] Text Changed: %@", [NSThread currentThread], x);
}]
throttle: .6]
doNext:^(id x) {
RAC(view, hidden) = [arraySignal map:^(NSArray *x) {
return x.count == 0;
}];
RAC(self.someView.hidden) = [RACAbleWithStart(self.someArray) map:^(NSArray *x) {
return x.count == 0;
}];
@joshaber
joshaber / gist:5226626
Last active December 15, 2015 07:49
ReactivePersistence?
// The basics
[[self.connection fetch:@"the-answer"] subscribeNext:^(NSNumber *x) {
NSLog(@"The answer is %@", x);
}];
[[self.connection writeObject:@42 forKey:@"the-answer"] subscribeCompleted:^{
NSLog(@"Wrote it.");
}];
// Signal for changes to the object at a key