-
-
Save jacksonh/7292c21eac40b79cc76d 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
// | |
// This is a signal that gets a users password. Imagine that it did something clever, like | |
// look in the keystore and prompt the user if the password isn't in the store | |
// | |
RACSignal *fetchPassword = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { | |
NSString *password = @"the password"; // imagine this required some async work to fetch | |
[subscriber sendNext:password]; | |
[subscriber sendCompleted]; | |
return [RACDisposable disposableWithBlock:^{ | |
}]; | |
}]; | |
// | |
// Once we have fetched the password, we want to fetch a | |
// list of items from a webservice that requires that password | |
// | |
[[fetchPassword flattenMap:^RACStream *(NSString *password) { | |
return [AsyncClient fetchStuffListWithPassword:password]; | |
}] flattenMap:^RACStream *(id stuffItem) { | |
// | |
// Now for each item that is in the list, we need to do | |
// another API call, this one requires the original password | |
// Ideally, I could pass that password in the flattenMap call | |
// | |
return [AsyncClient fetchStuffItem:stuffItem withPassword:password]; | |
}]; |
Slight modification on your version. Basically I sub'd repeat for never (never is a class method in my version of RAC), and changed the unpack line a bit:
[[fetchPassword flattenMap:^id (NSString *password) {
return [[[RACSignal return:password] repeat]
zipWith:[AsyncClient fetchStuffListWithPassword:password]]
}] flattenMap:^RACSignal *(RACTuple *passwordAndStuffItem) {
RACTupleUnpack(NSString *password, id stuffItem) = passwordAndStuffItem;
return [AsyncClient fetchStuffItem:stuffItem withPassword:password];
}];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hm, that's actually not that simple! How about something like this (totally untested):