Last active
March 2, 2017 20:39
-
-
Save soxjke/4269e56942a0561f9adc60f9c7f3b41b to your computer and use it in GitHub Desktop.
ReactiveHell
This file contains hidden or 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
| Vehicle: | |
| { | |
| "vehicle":{ | |
| "id":1, | |
| "name":"AlfaRomeo", | |
| "chassi":[12, 13, 27, 24], | |
| "wings":[3, 250, 1], | |
| "engine":[29], | |
| "headlights":[201], | |
| "profile":901 | |
| } | |
| } | |
| Profiles: | |
| { | |
| "profile":{ | |
| "id":901, | |
| "chassi": { | |
| // bonus - no id :) | |
| "price":1, | |
| "param1":"value1" | |
| }, | |
| "wings": { | |
| // bonus - no id :) | |
| "price":100, | |
| "param2":"value2" | |
| }, | |
| "engine": { | |
| // bonus - no id :) | |
| "price":3, | |
| "param3":"value3" | |
| }, | |
| "headlights": { | |
| // bonus - no id :) | |
| "price":5, | |
| "param4":"value4" | |
| } | |
| } | |
| } |
This file contains hidden or 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 *)fetchModules:(NSDictionary<NSString *, NSString *> *)moduleIDsToProfileIDsDictionary | |
| forVehicle:(NSString *)vehicle | |
| moduleType:(VehicleModule)moduleType { | |
| @weakify(self); | |
| return [[[RACSignal merge:[moduleIDsToProfileIDsDictionary map:^RACSignal *(NSString *moduleID, NSString *profileID) { | |
| @strongify(self); | |
| return [[self fetchProfileForID:profileID] combineLatestWith:[RACSignal return:moduleID]]; | |
| }]] collect] map:^NSArray<ModuleBase *> *(NSArray<RACTuple *> *profiles) { | |
| return [[[profiles map:^NSArray<ModuleBase*> *(RACTuple *profileTuple) { | |
| RACTupleUnpack(NSDictionary *profileDict, NSString *moduleID) = profileTuple; | |
| return [profileDict map:^ModuleBase *(NSString *key, VehicleProfile *profile) { | |
| ModuleBase *module = nil; | |
| switch (moduleType) { | |
| case VehicleModuleHeadlights: | |
| module = profile.headlights; | |
| break; | |
| case VehicleModuleEngine: | |
| module = profile.engine; | |
| break; | |
| case VehicleModuleWings: | |
| module = profile.wings; | |
| break; | |
| case VehicleModuleChassi: | |
| module = profile.chassi; | |
| break; | |
| case VehicleModuleUndefinded: | |
| NSAssert(NO, @"undefined module type"); | |
| } | |
| module.moduleID = moduleID; | |
| return module; | |
| }]; | |
| }] flatten] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES]]]; | |
| }]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment