Skip to content

Instantly share code, notes, and snippets.

@jspahrsummers
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jspahrsummers/f6424839cdeb505e31ee to your computer and use it in GitHub Desktop.
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.
RACSignal *signal = [RACSignal create:^(id<RACSubscriber> subscriber) {
for (NSUInteger i = 0; !subscriber.disposable.disposed; i++) {
[subscriber sendNext:@(i)];
}
}];
// 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];
// 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];
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