Last active
August 29, 2015 14:01
-
-
Save jspahrsummers/f6424839cdeb505e31ee to your computer and use it in GitHub Desktop.
Brainstorming better subscribers for ReactiveCocoa 3.0. See https://github.com/ReactiveCocoa/ReactiveCocoa/pull/1308.
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
RACSignal *signal = [RACSignal create:^(id<RACSubscriber> subscriber) { | |
for (NSUInteger i = 0; !subscriber.disposable.disposed; i++) { | |
[subscriber sendNext:@(i)]; | |
} | |
}]; |
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
// Instead of the current -subscribe… methods, this makes it clearer that “connection” | |
// can have side effects, and that the connection is _not_ the same as the signal | |
// itself (e.g., signals never start or stop, only connections) | |
RACDisposable *disposable = [signal connect:[RACSubscriber subscriberWithNextHandler:^(id x) { | |
// … | |
} errorHandler:^(NSError *error) { | |
// … | |
} completedHandler:^{ | |
// … | |
}]]; | |
[disposable dispose]; |
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
// Ensures that events are always dequeued on the main thread, possibly after a hop. | |
RACSubscriber *multicastingSubscriber = [[RACSubscriber alloc] initWithScheduler:RACScheduler.mainThreadScheduler]; | |
[multicastingSubscriber addNextHandler:^(id x) { | |
// Thing A | |
}]; | |
[multicastingSubscriber addNextHandler:^(id x) { | |
// Thing B | |
}]; | |
[signal connect:multicastingSubscriber]; | |
// Forward events to another subscriber, without connecting to the original | |
// source again. | |
[multicastingSubscriber.events connect:anotherSubscriber]; |
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
RACSubscriber *multiplexingSubscriber = [RACSubscriber subscriberWithNextHandler:^(id x) { | |
// … | |
} errorHandler:nil completedHandler:nil]; | |
[signalA connect:multiplexingSubscriber]; | |
[signalB connect:multiplexingSubscriber]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment