SlideShare a Scribd company logo
1 of 21
Download to read offline
IntroduzioneaReactiveX
Andrea Ceroni
andrea.ceroni@elfo.net
@andrekiba6
Sponsored by
28 aprile
2016
Relatore
Introduzione aReactiveX
Tipo disessione: Frontale
Durata sessione: 45min
Argomenti
ReactiveProgramming
observablestream
IObservable eIObserver
Pullmodelvs Push model
Coldvs Hotobservables
ReactiveX
classeObservable
Operatori
1/18
Code Q & AIntro
2/18
3/18
Cosa significa
Reactive Programming?
paradigma di programmazione dichiarativa
in cui l’applicazione reagisce adeventi
non necessariamente concorrente
4/18
What's the difference between anarray
and events?
Erik Meijer
[ 16, 2, 5, 6, 4, 11, 7 ] Where(x=> IsPrime(x)) [ 2, 5, 11, 7 ]
timex=6
y= 6
x=22
y= 116
x=7
y= 91
x=33
y= 4
Where(e=> e.X > 20)
timex=22
y= 116
x=33
y= 4
5/18
observable= stream di eventi
è possibilecreare facilmentedata stream di qualsiasi cosa, nonsoloeventi
glistream sonopoco costosiintermini di risorse epossono stare ovunque
6/18
Come recuperiamo i dati?
Single Multiple
Sync T GetData() IEnumerable<T> GetData()
Async Task<T> GetData() IObservable<T>GetData()
e sevogliamoprocessarein asincronouna collection
senza attendere di avere tuttiidati?
e se nonsappiamo a prioriquando dovremo farlo?
7/18IEnumerable<T>
Il metodo IEnumerable.GetEnumerator()ritornaun oggettodi tipo IEnumeratorcheci permettedi iterare su una
collectionehaquesti metodieproprietà:
• bool MoveNext():avanza all’elementosuccessivoeritornatrue o false (se esiste o no)
• T Current{ get; }: ritorna l’elementocorrente
• throwsException:la chiamataaCurrent può generare eccezionesel’elementononesiste
• void Dispose() : rilascia le risorse utilizzate dall’enumerator
pull model
8/18esempio
varenumerator = newList<int> { 1, 2, 3 }.GetEnumerator();
while(enumerator.MoveNext())
{
Console.WriteLine(enumerator.Current);
}
enumerator.Dispose();
9/18IObserver<T>
Ogni metododiIEnumeratorha il suo duale nell’interfaccia IObserver :
• void OnCompleted(): notifical’osservatore cheil provider hafinito di mandare dati
• void OnNext(T): rende disponibile un nuovo elemntoall’osservatore
• void OnError(Exception):notifical’osservatore chec’èstato un errore
pushmodel
x time
10/18esempio
public static IObservable<int>GetData()
{
return Observable.Create<int>(o => {
o.OnNext(1);
o.OnNext(2);
o.OnNext(3);
o.OnCompleted();
return Disposable.Empty;
});
}
11/18Confronto
IEnumerable IObservable
pull push
bool MoveNext() voidOnCompleted()
T Current { get; } void OnNext()
throws Exception void OnError(Exception)
12/18
IObservable<T> e IObserver<T>
interface IObserver<inT>
{
voidOnNext(T item);
voidOnCompleted();
voidOnError(Exception error);
}
interface IObservable<outT>
{
IDisposableSubscribe(IObserver<T> observer);
}
13/18
Cold
• iniziaademettere item soloquandovienesottoscritta
• gliitem emessinon sono condivisitragliosservatori
Hot
• emette item indipendentementedall’esistenzadiun osservatore
• gliitem sonocondivisitratutti gliosservatori
Cold vs Hot observables
14/18
ReactiveX
Rxis a library for programming with
asynchronousdata streams.
It is a combination of thebest ideas from
theObserver pattern,theIterator pattern,
and functionalprogramming
15/18
mette a disposizionelaclasseObservablechein unasingolaastrazionecipermette
digestire qualsiasistream
possiamotrattare stream dieventicomenormalicollection
componibile:le queryutilizzano diversioperatorie sipossonocomporre, ilflussoè chiaro
dichiarativa:specificacosafailcodice,non comelofa
trasformativa:lequery possonotrasforamre datidaun tipoadun altro
dalpuntodivistadell’observer l’implementazionenon importa à disaccoppiamento
Quali vantaggi?
16/18
Observable.Just(code)
17/18
Riassunto
che cosaè un’ observable
differenza tra push modelepull model
differenza tra coldehotobservables
wrappare un evento inuna observable
comeconcatenare operatori Rx per
modificare i dati
18/18questions
.ToObservable()
.Subscribe(q=> {
if(iKnowTheAnswer)
Answer(q);J
});
Grazie!

More Related Content

Viewers also liked

The five dysfunctions of a team
The five dysfunctions of a teamThe five dysfunctions of a team
The five dysfunctions of a teamKlab
 
A Quick Intro to ReactiveX
A Quick Intro to ReactiveXA Quick Intro to ReactiveX
A Quick Intro to ReactiveXTroy Miles
 
NetflixOSS season 2 episode 2 - Reactive / Async
NetflixOSS   season 2 episode 2 - Reactive / AsyncNetflixOSS   season 2 episode 2 - Reactive / Async
NetflixOSS season 2 episode 2 - Reactive / AsyncRuslan Meshenberg
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaReactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaKasun Indrasiri
 
What Makes Great Infographics
What Makes Great InfographicsWhat Makes Great Infographics
What Makes Great InfographicsSlideShare
 
Masters of SlideShare
Masters of SlideShareMasters of SlideShare
Masters of SlideShareKapost
 
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
STOP! VIEW THIS! 10-Step Checklist When Uploading to SlideshareSTOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
STOP! VIEW THIS! 10-Step Checklist When Uploading to SlideshareEmpowered Presentations
 
10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation Optimization10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation OptimizationOneupweb
 
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
How To Get More From SlideShare - Super-Simple Tips For Content MarketingHow To Get More From SlideShare - Super-Simple Tips For Content Marketing
How To Get More From SlideShare - Super-Simple Tips For Content MarketingContent Marketing Institute
 
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...SlideShare
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShareSlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShareSlideShare
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksSlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShareSlideShare
 

Viewers also liked (15)

The five dysfunctions of a team
The five dysfunctions of a teamThe five dysfunctions of a team
The five dysfunctions of a team
 
A Quick Intro to ReactiveX
A Quick Intro to ReactiveXA Quick Intro to ReactiveX
A Quick Intro to ReactiveX
 
NetflixOSS season 2 episode 2 - Reactive / Async
NetflixOSS   season 2 episode 2 - Reactive / AsyncNetflixOSS   season 2 episode 2 - Reactive / Async
NetflixOSS season 2 episode 2 - Reactive / Async
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaReactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-Java
 
What Makes Great Infographics
What Makes Great InfographicsWhat Makes Great Infographics
What Makes Great Infographics
 
Masters of SlideShare
Masters of SlideShareMasters of SlideShare
Masters of SlideShare
 
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
STOP! VIEW THIS! 10-Step Checklist When Uploading to SlideshareSTOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
 
You Suck At PowerPoint!
You Suck At PowerPoint!You Suck At PowerPoint!
You Suck At PowerPoint!
 
10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation Optimization10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation Optimization
 
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
How To Get More From SlideShare - Super-Simple Tips For Content MarketingHow To Get More From SlideShare - Super-Simple Tips For Content Marketing
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
 
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShare
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to Introduzione a ReactiveX

Reactive programming con RxJS
Reactive programming con RxJSReactive programming con RxJS
Reactive programming con RxJSNucleode Srl
 
RxJS - Intro to Reactive Programming
RxJS - Intro to Reactive ProgrammingRxJS - Intro to Reactive Programming
RxJS - Intro to Reactive ProgrammingMichele Stieven
 
Testing Rails: Model vs Integration
Testing Rails: Model vs IntegrationTesting Rails: Model vs Integration
Testing Rails: Model vs IntegrationDavid Librera
 
Migrazione dei meccanismi di workflow di un sistema informativo assicurativo ...
Migrazione dei meccanismi di workflow di un sistema informativo assicurativo ...Migrazione dei meccanismi di workflow di un sistema informativo assicurativo ...
Migrazione dei meccanismi di workflow di un sistema informativo assicurativo ...Donato Clun
 
Autenticazione Continua Durante la Navigazione Web Basata sulla Dinamica del ...
Autenticazione Continua Durante la Navigazione Web Basata sulla Dinamica del ...Autenticazione Continua Durante la Navigazione Web Basata sulla Dinamica del ...
Autenticazione Continua Durante la Navigazione Web Basata sulla Dinamica del ...danieledegan
 
Laboratorio Programmazione: Overflow e switch
Laboratorio Programmazione: Overflow e switchLaboratorio Programmazione: Overflow e switch
Laboratorio Programmazione: Overflow e switchMajong DevJfu
 

Similar to Introduzione a ReactiveX (9)

Reactive programming con RxJS
Reactive programming con RxJSReactive programming con RxJS
Reactive programming con RxJS
 
RxJS - Intro to Reactive Programming
RxJS - Intro to Reactive ProgrammingRxJS - Intro to Reactive Programming
RxJS - Intro to Reactive Programming
 
Testing Rails: Model vs Integration
Testing Rails: Model vs IntegrationTesting Rails: Model vs Integration
Testing Rails: Model vs Integration
 
Migrazione dei meccanismi di workflow di un sistema informativo assicurativo ...
Migrazione dei meccanismi di workflow di un sistema informativo assicurativo ...Migrazione dei meccanismi di workflow di un sistema informativo assicurativo ...
Migrazione dei meccanismi di workflow di un sistema informativo assicurativo ...
 
Autenticazione Continua Durante la Navigazione Web Basata sulla Dinamica del ...
Autenticazione Continua Durante la Navigazione Web Basata sulla Dinamica del ...Autenticazione Continua Durante la Navigazione Web Basata sulla Dinamica del ...
Autenticazione Continua Durante la Navigazione Web Basata sulla Dinamica del ...
 
Rx - ReactiveX
Rx - ReactiveXRx - ReactiveX
Rx - ReactiveX
 
Dal C a Java (2/3)
Dal C a Java (2/3)Dal C a Java (2/3)
Dal C a Java (2/3)
 
Laboratorio Programmazione: Overflow e switch
Laboratorio Programmazione: Overflow e switchLaboratorio Programmazione: Overflow e switch
Laboratorio Programmazione: Overflow e switch
 
TYPO3 CMS 7.3 - le novita
TYPO3 CMS 7.3 - le novitaTYPO3 CMS 7.3 - le novita
TYPO3 CMS 7.3 - le novita
 

Introduzione a ReactiveX