SlideShare a Scribd company logo
RX – Reactive Extensions
Observer pattern
 Software design
 Subject maintains list of Dependents or Observers
 Observer registers for future notification
 Subject notifies observer of state change
 Decoupling components
 Event driven software
What is RX
 is a set of types representing asynchronous data streams
 a set of operators over asynchronous data streams
 a set of types to parameterize concurrencyRxJS
 RxJS = Observables + LINQ + Schedulers
 ReactiveX combines the Observer pattern with the Iterator
pattern and functional programming with collections to fill the need for an
ideal way of managing sequences of events.

What is Rx
 Observable: represents the idea of an invokable collection of future values or
events.
 Observer: is a collection of callbacks that knows how to listen to values delivered
by the Observable.
 Subscription: represents the execution of an Observable, is primarily useful for
cancelling the execution.
 Operators: are pure functions that enable a functional programming style of
dealing with collections with operations like map, filter, concat, flatMap, etc.
 Subject: is the equivalent to an EventEmitter, and the only way of multicasting a
value or event to multiple Observers.
 Schedulers: are centralized dispatchers to control concurrency, allowing us to
coordinate when computation happens on e.g. setTimeout or
requestAnimationFrame
Essentials
interface Observer {
next: (value: T) => void;
error: (err: any) => void;
complete: () => void;
}
interface Observable<T> {
subscribe(next: (value: T) => void, error: (err: any) => void, complete: () => void): Subscription;
}
Observables as generalizations of functions
function foo() {
console.log('Hello');
return 42;
}
var x = foo.call(); // same as foo()
console.log(x);
var y = foo.call(); // same as foo()
console.log(y);
Output:
"Hello"
42
"Hello"
42
var foo = Rx.Observable.create(function (observer) {
console.log('Hello');
observer.next(42);
});
foo.subscribe(function (x) {
console.log(x);
});
foo.subscribe(function (y) {
console.log(y);
});
Output:
"Hello"
42
"Hello"
42
 Observables are like functions with zero arguments, but generalize those to
allow multiple values
Observables as generalizations of functions
Creating Observables
var foo = Rx.Observable.create(function subscribe(observer) {
console.log('Hello');
observer.next(42);
observer.next(100); // "return" another value
observer.next(200); // "return" yet another
setTimeout(() => {
observer.next(300); // happens asynchronously
}, 1000);
});
console.log('before');
foo.subscribe(function (x) {
console.log(x);
});
console.log('after');
Output:
"before"
"Hello"
42
100
200
"after“
300
Subscribing to observable
 myObservable.subscribe((x) => console.log(x))
 Each subscribe triggers the subscribe function provided when creating the
Observable (Observable.create(function subscribe(observer) {...}))
 Providing callback on which data will be delivered
Observable execution
var foo = Rx.Observable.create(function subscribe(observer) {
// code here is run once for each observer when he subscribes
observer.next(42);
observer.next(100);
observer.next(200);
});
foo.subscribe(function (x) {
console.log(x);
});
• Multiple subscriptions, multiple executions.
• No execution context sharing
• Each execution can produce multiple values, synchronously or
asynchronously
Observable execution
Three types of return values from Observable execution:
 "Next" notification: sends a value such as a Number, a String, an Object, etc.
 "Error" notification: sends a JavaScript Error or exception.
 "Complete" notification: does not send a value.
 Infinite next notifications
 Once Error or Complete is delivered, nothing else is delivered afterwards
Observable execution
Observable execution
Subscription and disposing Observable
execution
 Execution can run infinitely. We
need to stop it
 Subscription is object representing
disposable resources
 Unsubscribe method stops
execution and dispose resource
let foo = Rx.Observable.create(function
subscribe(observer) {
let intervalId = setInterval(() => {
observer.next(100);
}, 1000);
return function unsubscribe() {
clearInterval(intervalId);
};
});
let subscription = foo.subscribe((x) => console.log(x));
subscription.unsubscribe();
Subject
 An observable that allows values to be sent to many observers.
 Unlike observable, no multiple observable execution occurs
 Maintains registry of observers
Subject
 Is observable:
var subject = new Rx.Subject();
subject.subscribe({
next: (v) => console.log('observerA: ' + v)
});
subject.subscribe({
next: (v) => console.log('observerB: ' + v)
});
 Is observer:
subject.next(100);
subject.next(200);
subject.complete();
BehaviorSubject
 Same as Subject
 Contains notion of “current value”
 All observers will first receive the current value immediately
 Useful for representing "values over time"
Rx operators
 Provide the usability behind the foundation that is the observable
 Provide means for composing complex asynchronous code
 Methods on the Observable type: .map(…).filter(…).merge(…)
 DO NOT modify the existing observable, but return a NEW Observable
Rx operators
 Operator categories:
 Creation
 Transformation
 Filtering
 Combination
 Multicasting
 Utility
 Conditional
 Mathematical and aggregation
Rx operators
Creation
 create
 empty
 from
 fromEvent
 fromPromise
 of
 throw
 timer
Transformation
 map
 mapTo
 mergeMap
 pluck
 switchMap
Filtering
 debounceTime
 distinctUntilCh
anged
 filter
 skip
 take
Combination
 combineLatest
 concat
 forkJoin
 merge
 switch
Creation
 do
 delay
 toPromise
References
 https://en.wikipedia.org/wiki/Observer_pattern
 https://yobriefca.se/presentations/tyrannosaurus-rx.pdf
 http://reactivex.io/rxjs/manual/overview.html
 https://www.slideshare.net/mattpodwysocki/cascadiajs-dont-cross-the-
streams
 Playground: https://rxviz.com/examples/random-error

More Related Content

What's hot (19)

PDF
Oop assignment 02
MamoonKhan39
 
PDF
Reactive computing
Stan Lea
 
PDF
Intro to ReactiveCocoa
kleneau
 
PDF
The Ring programming language version 1.5.1 book - Part 20 of 180
Mahmoud Samir Fayed
 
PDF
An Introduction to Reactive Cocoa
SmartLogic
 
DOCX
C# console programms
Yasir Khan
 
PDF
Java Practical File Diploma
mustkeem khan
 
DOCX
C# labprograms
Jafar Nesargi
 
PPTX
Rxjs ngvikings
Christoffer Noring
 
PDF
ReactiveCocoa Goodness - Part I of II
manuelmaly
 
PPTX
Lex programming
sureshmoharana2013
 
PDF
DSU C&C++ Practical File Diploma
mustkeem khan
 
PDF
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
Nexus FrontierTech
 
PPTX
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
Chester Chen
 
PDF
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
Sylvain Hallé
 
DOCX
Lab 1
rimshailyas1
 
PDF
Reactive cocoa
iacisclo
 
PDF
C#
actacademy
 
PDF
Introduction to JQ
Knoldus Inc.
 
Oop assignment 02
MamoonKhan39
 
Reactive computing
Stan Lea
 
Intro to ReactiveCocoa
kleneau
 
The Ring programming language version 1.5.1 book - Part 20 of 180
Mahmoud Samir Fayed
 
An Introduction to Reactive Cocoa
SmartLogic
 
C# console programms
Yasir Khan
 
Java Practical File Diploma
mustkeem khan
 
C# labprograms
Jafar Nesargi
 
Rxjs ngvikings
Christoffer Noring
 
ReactiveCocoa Goodness - Part I of II
manuelmaly
 
Lex programming
sureshmoharana2013
 
DSU C&C++ Practical File Diploma
mustkeem khan
 
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
Nexus FrontierTech
 
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
Chester Chen
 
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
Sylvain Hallé
 
Reactive cocoa
iacisclo
 
Introduction to JQ
Knoldus Inc.
 

Similar to Rx – reactive extensions (20)

PDF
Reactive x
Gabriel Araujo
 
PPTX
Reacting with ReactiveUI
kiahiska
 
PPTX
Rxjs marble-testing
Christoffer Noring
 
PPTX
Rxjs swetugg
Christoffer Noring
 
PPTX
Reactive Java (GeeCON 2014)
Tomasz Kowalczewski
 
PDF
Demystifying Reactive Programming
Tom Bulatewicz, PhD
 
PDF
响应式编程及框架
jeffz
 
PDF
RxJS - The Reactive extensions for JavaScript
Viliam Elischer
 
PPTX
Reactive Java (33rd Degree)
Tomasz Kowalczewski
 
PDF
GKAC 2015 Apr. - RxAndroid
GDG Korea
 
PPTX
Luis Atencio on RxJS
Luis Atencio
 
PPTX
Introduction to RxJS
Abul Hasan
 
PDF
Building Scalable Stateless Applications with RxJava
Rick Warren
 
PDF
Reactive programming on Android
Tomáš Kypta
 
PDF
Think Async: Asynchronous Patterns in NodeJS
Adam L Barrett
 
PDF
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
PDF
Coding in Style
scalaconfjp
 
PPTX
Understanding reactive programming with microsoft reactive extensions
Oleksandr Zhevzhyk
 
PPTX
Rx workshop
Ryan Riley
 
PDF
RxJava for Android - GDG DevFest Ukraine 2015
Constantine Mars
 
Reactive x
Gabriel Araujo
 
Reacting with ReactiveUI
kiahiska
 
Rxjs marble-testing
Christoffer Noring
 
Rxjs swetugg
Christoffer Noring
 
Reactive Java (GeeCON 2014)
Tomasz Kowalczewski
 
Demystifying Reactive Programming
Tom Bulatewicz, PhD
 
响应式编程及框架
jeffz
 
RxJS - The Reactive extensions for JavaScript
Viliam Elischer
 
Reactive Java (33rd Degree)
Tomasz Kowalczewski
 
GKAC 2015 Apr. - RxAndroid
GDG Korea
 
Luis Atencio on RxJS
Luis Atencio
 
Introduction to RxJS
Abul Hasan
 
Building Scalable Stateless Applications with RxJava
Rick Warren
 
Reactive programming on Android
Tomáš Kypta
 
Think Async: Asynchronous Patterns in NodeJS
Adam L Barrett
 
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Coding in Style
scalaconfjp
 
Understanding reactive programming with microsoft reactive extensions
Oleksandr Zhevzhyk
 
Rx workshop
Ryan Riley
 
RxJava for Android - GDG DevFest Ukraine 2015
Constantine Mars
 
Ad

Recently uploaded (20)

PDF
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
PPTX
Cutting Optimization Pro 5.18.2 Crack With Free Download
cracked shares
 
PDF
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PDF
Enhancing Security in VAST: Towards Static Vulnerability Scanning
ESUG
 
PDF
How Agentic AI Networks are Revolutionizing Collaborative AI Ecosystems in 2025
ronakdubey419
 
PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PPTX
Employee salary prediction using Machine learning Project template.ppt
bhanuk27082004
 
PDF
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
 
PDF
Supabase Meetup: Build in a weekend, scale to millions
Carlo Gilmar Padilla Santana
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
Cutting Optimization Pro 5.18.2 Crack With Free Download
cracked shares
 
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Enhancing Security in VAST: Towards Static Vulnerability Scanning
ESUG
 
How Agentic AI Networks are Revolutionizing Collaborative AI Ecosystems in 2025
ronakdubey419
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
Employee salary prediction using Machine learning Project template.ppt
bhanuk27082004
 
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
 
Supabase Meetup: Build in a weekend, scale to millions
Carlo Gilmar Padilla Santana
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Ad

Rx – reactive extensions

  • 1. RX – Reactive Extensions
  • 2. Observer pattern  Software design  Subject maintains list of Dependents or Observers  Observer registers for future notification  Subject notifies observer of state change  Decoupling components  Event driven software
  • 3. What is RX  is a set of types representing asynchronous data streams  a set of operators over asynchronous data streams  a set of types to parameterize concurrencyRxJS  RxJS = Observables + LINQ + Schedulers  ReactiveX combines the Observer pattern with the Iterator pattern and functional programming with collections to fill the need for an ideal way of managing sequences of events. 
  • 4. What is Rx  Observable: represents the idea of an invokable collection of future values or events.  Observer: is a collection of callbacks that knows how to listen to values delivered by the Observable.  Subscription: represents the execution of an Observable, is primarily useful for cancelling the execution.  Operators: are pure functions that enable a functional programming style of dealing with collections with operations like map, filter, concat, flatMap, etc.  Subject: is the equivalent to an EventEmitter, and the only way of multicasting a value or event to multiple Observers.  Schedulers: are centralized dispatchers to control concurrency, allowing us to coordinate when computation happens on e.g. setTimeout or requestAnimationFrame
  • 5. Essentials interface Observer { next: (value: T) => void; error: (err: any) => void; complete: () => void; } interface Observable<T> { subscribe(next: (value: T) => void, error: (err: any) => void, complete: () => void): Subscription; }
  • 6. Observables as generalizations of functions function foo() { console.log('Hello'); return 42; } var x = foo.call(); // same as foo() console.log(x); var y = foo.call(); // same as foo() console.log(y); Output: "Hello" 42 "Hello" 42
  • 7. var foo = Rx.Observable.create(function (observer) { console.log('Hello'); observer.next(42); }); foo.subscribe(function (x) { console.log(x); }); foo.subscribe(function (y) { console.log(y); }); Output: "Hello" 42 "Hello" 42  Observables are like functions with zero arguments, but generalize those to allow multiple values Observables as generalizations of functions
  • 8. Creating Observables var foo = Rx.Observable.create(function subscribe(observer) { console.log('Hello'); observer.next(42); observer.next(100); // "return" another value observer.next(200); // "return" yet another setTimeout(() => { observer.next(300); // happens asynchronously }, 1000); }); console.log('before'); foo.subscribe(function (x) { console.log(x); }); console.log('after'); Output: "before" "Hello" 42 100 200 "after“ 300
  • 9. Subscribing to observable  myObservable.subscribe((x) => console.log(x))  Each subscribe triggers the subscribe function provided when creating the Observable (Observable.create(function subscribe(observer) {...}))  Providing callback on which data will be delivered
  • 10. Observable execution var foo = Rx.Observable.create(function subscribe(observer) { // code here is run once for each observer when he subscribes observer.next(42); observer.next(100); observer.next(200); }); foo.subscribe(function (x) { console.log(x); }); • Multiple subscriptions, multiple executions. • No execution context sharing • Each execution can produce multiple values, synchronously or asynchronously
  • 11. Observable execution Three types of return values from Observable execution:  "Next" notification: sends a value such as a Number, a String, an Object, etc.  "Error" notification: sends a JavaScript Error or exception.  "Complete" notification: does not send a value.  Infinite next notifications  Once Error or Complete is delivered, nothing else is delivered afterwards
  • 14. Subscription and disposing Observable execution  Execution can run infinitely. We need to stop it  Subscription is object representing disposable resources  Unsubscribe method stops execution and dispose resource let foo = Rx.Observable.create(function subscribe(observer) { let intervalId = setInterval(() => { observer.next(100); }, 1000); return function unsubscribe() { clearInterval(intervalId); }; }); let subscription = foo.subscribe((x) => console.log(x)); subscription.unsubscribe();
  • 15. Subject  An observable that allows values to be sent to many observers.  Unlike observable, no multiple observable execution occurs  Maintains registry of observers
  • 16. Subject  Is observable: var subject = new Rx.Subject(); subject.subscribe({ next: (v) => console.log('observerA: ' + v) }); subject.subscribe({ next: (v) => console.log('observerB: ' + v) });  Is observer: subject.next(100); subject.next(200); subject.complete();
  • 17. BehaviorSubject  Same as Subject  Contains notion of “current value”  All observers will first receive the current value immediately  Useful for representing "values over time"
  • 18. Rx operators  Provide the usability behind the foundation that is the observable  Provide means for composing complex asynchronous code  Methods on the Observable type: .map(…).filter(…).merge(…)  DO NOT modify the existing observable, but return a NEW Observable
  • 19. Rx operators  Operator categories:  Creation  Transformation  Filtering  Combination  Multicasting  Utility  Conditional  Mathematical and aggregation
  • 20. Rx operators Creation  create  empty  from  fromEvent  fromPromise  of  throw  timer Transformation  map  mapTo  mergeMap  pluck  switchMap Filtering  debounceTime  distinctUntilCh anged  filter  skip  take Combination  combineLatest  concat  forkJoin  merge  switch Creation  do  delay  toPromise
  • 21. References  https://en.wikipedia.org/wiki/Observer_pattern  https://yobriefca.se/presentations/tyrannosaurus-rx.pdf  http://reactivex.io/rxjs/manual/overview.html  https://www.slideshare.net/mattpodwysocki/cascadiajs-dont-cross-the- streams  Playground: https://rxviz.com/examples/random-error