signal ing out of callback hell
TRANSCRIPT
![Page 1: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/1.jpg)
SIGNAL-ING OUT OFCALLBACK HELL
whoami / Omer Iqbal @olenhad
![Page 2: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/2.jpg)
Functional Reactive Programming
Compositional Event Systems? ^
![Page 3: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/3.jpg)
WHY?The world is asynchronous
Programs need to interact with the world
![Page 4: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/4.jpg)
But we have Listeners/Observer/Delegate/CallbackPatterns! Life's good! Go away!
![Page 5: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/5.jpg)
// Yeah, the empire still uses Objective C
@protocol DeathStarDelegate {- (void)didBlowUp;}
// In DeathStar@property (nonatomic, weak) id <DeathStarDelegate> delegate
// When Blowing up[self.delegate didBlowUp];
// In DarthVader who implements DeathStarDelegate- (void)didBlowUp { NSLog(@"Need to hire better stormtroopers");}
![Page 6: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/6.jpg)
PROBLEMS?Unpredictable OrderMissing EventsCleaning up listenersAccidentalRecursionMESSY STATE EwwMulthreading OMG
![Page 7: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/7.jpg)
Chaining dependent operations
![Page 8: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/8.jpg)
"our intellectual powers are rather geared to master staticrelations and that our powers to visualize processes
evolving in time are relatively poorly developed"
Dijkstra on GOTO
![Page 9: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/9.jpg)
ENTER FRP/COMPOSITIONAL EVENTSYSTEMS
![Page 10: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/10.jpg)
Imperative programming describes computations as a seriesof actions modifying program state
var numbers = [1,2,3,4,5];var even = [];
numbers.forEach(function(n) { if (n % 2 == 0) { even.push(n); }});
console.log(even);
![Page 11: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/11.jpg)
In functional programming we describe what we want ratherthan how we want it done
var numbers = [1,2,3,4,5];var even = numbers.filter(function(n){ return n % 2 == 0;});
![Page 12: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/12.jpg)
In FP we model computations as transformations of values
DeclarativePureThreadsafeComposable
![Page 13: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/13.jpg)
FRP extends this principle to asynchronous flows
![Page 14: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/14.jpg)
SIGNALSRepresenting a stream of values (over time)
e.g Async operations like API calls/UI Input/Timer RETURNSignals
Sends three kinds of events
NextErrorCompleted
![Page 15: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/15.jpg)
Signals can be subscribed to
[lannisterSignal subscribeNext:̂(NSString *name){ NSLog(@"%@ is a true Lannister", name);}];
[lannisterSignal sendNext:@"Cersei"];[lannisterSignal sendNext:@"Jamie"];
[lannisterSignal sendCompleted];
[lannisterSignal sendNext:@"Tyrion"]; // Nothing logged here. Sorry Tyrion
![Page 16: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/16.jpg)
Creating Signals
- (RACSignal *)signInSignal{ return [RACSignal createSignal:̂RACDisposable *(id<RACSubscriber> subscriber) {
[self.signInService signInWithUsername:self.txtUsername.text password:self.txtPassword.text complete:̂(BOOL status, NSError *error) { if (error) { [subscriber sendError:error]; } else { [subscriber sendNext:@(status)]; [subscriber sendCompleted]; } }];
return nil;
![Page 17: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/17.jpg)
I know what you're thinking
![Page 18: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/18.jpg)
COMPOSITION!Signals like values can be transformed via higher order
functions!
![Page 19: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/19.jpg)
Signal A -> Signal B
map
[lannisterSignal map:̂NSNumber *(NSString *name){ return @(name.length);}];
![Page 20: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/20.jpg)
Signal A -> predicate? -> Signal A
filter
[lannisterSignal filter:̂BOOL (NSString *name){ return ![name isEqualToString:@"Tyrion"];}];
![Page 21: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/21.jpg)
Merge
RACSignal *contenders =[RACSignal merge:@[lannisters, baratheons, starks, tyrells]];
![Page 22: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/22.jpg)
Signal of Signals
[khaleesiSignal map:̂RACSignal *(NSString *name){ return [[Khaleesi sharedInstance] fetchDragons];}];
![Page 23: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/23.jpg)
flatMap!
flatten: Signal (Signal A) -> Signal A
map: Signal A -> Signal B
psst... Also called bind. Shoutout to all the Haskell folks
![Page 24: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/24.jpg)
Chaining dependent async operations
[[[[client logIn]then:̂{ return [client loadCachedMessages];}]flattenMap:̂(NSArray *messages) { return [client fetchMessagesAfterMessage:messages.lastObject];}]subscribeError:̂(NSError *error) { [self presentError:error];} completed:̂{ NSLog(@"Fetched all messages.");}];
![Page 25: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/25.jpg)
PIPELINES!
![Page 26: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/26.jpg)
Declarative! => Clear Ordering
Composable!
No explicit state machine to manage!
Unified interface for Async events
![Page 27: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/27.jpg)
![Page 28: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/28.jpg)
BUT!
Not a silver bullet
Hot Signals vs Cold Signals
![Page 29: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/29.jpg)
GOOD NEWS!
RAC3 uses Signal and SignalProducer types to distinguish
![Page 30: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/30.jpg)
SIMPLE, NOT EASY THANKS RICH HICKEY!
Simple vs Complex
Easy vs Hard
![Page 31: Signal ing out of callback hell](https://reader034.vdocuments.mx/reader034/viewer/2022042611/58729f1c1a28ab07208b55cf/html5/thumbnails/31.jpg)
questions? answers : end;