Last active
December 28, 2015 18:09
-
-
Save jonsterling/7541042 to your computer and use it in GitHub Desktop.
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
// A nice little example of using transactional signal generators. | |
// Imagine a board of buttons with numbers on it, and a big red button with a picture of a bomb on it. | |
// When a button with a number is pressed, `increment` is invoked with that number. When the bomb | |
// button is pressed, then `reset` is invoked. | |
// | |
//This returns a signal of the running total made by incrementing by numbers and resetting by explosions. | |
RACDynamicSignalGenerator *increment = [[RACDynamicSignalGenerator alloc] initWithBlock:^RACSignal *(NSNumber *add) { | |
return [RACSignal return:^(NSNumber *count) { | |
return @(count.integerValue + add.integerValue); | |
}]; | |
}]; | |
RACDynamicSignalGenerator *reset = [[RACDynamicSignalGenerator alloc] initWithBlock:^RACSignal *(id _) { | |
return [RACSignal return:^(NSNumber *count) { | |
return @0; | |
}]; | |
}]; | |
RACSignal *transformers = [RACSignal merge:@[ increment.asTransactionSignalGenerator.transactions.flatten, | |
reset.asTransactionSignalGenerator.transactions.flatten ]]; | |
return [transformers scanWithStart:@0 reduce:^id(NSNumber *count, NSNumber *(^transformer)(NSNumber *)) { | |
return transformer(count); | |
}]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment