SlideShare a Scribd company logo
RxJS - What is it?
 
Adam GoΕ‚Δ…b
Fullstack JS Developer @ Brainhub
Reactive Programming
Reactive programming is an asynchronous programming
paradigm concerned with data streams and the
propagation of change.
 
Think of RxJS as Lodash for events.
 
Developed by Microsoft, in collaboration with a community
of open source developers.
RxJS is a library for composing asynchronous and event-
based programs by using observable sequences. It
provides one core type, the Observable, satellite types
(Observer, Schedulers, Subjects) and operators
inspired by Array#extras (map, filter, reduce,
every, etc) to allow handling asynchronous events as
collections.
Introduction to RxJS
Observer
class Observable {
notify() {
for observer in this.observers {
observer.notifyAboutNewState();
}
}
}
Real world example!
Vanilla JS
const button = document.querySelector('button');
button.addEventListener('click', () => console.log('Clicked!'))
RxJS
const button = document.querySelector('button');
Rx.Observable.fromEvent(button, 'click')
.subscribe(() => console.log('Clicked!'));
Purity!
Vanilla JS
let count = 0;
const button = document.querySelector('button');
button.addEventListener('click', () =>
console.log(`Clicked ${++count} times`)
);
RxJS
const button = document.querySelector('button');
Rx.Observable.fromEvent(button, 'click')
.scan(count => count + 1, 0)
.subscribe(count => console.log(`Clicked ${count} times`));
Introduction to RxJS
Observable
Lazy Push collections of multiple values. They ll the
missing spot in the following table:
  Single Multiple
Pull Function Iterator
Push Promise Observable
Observable
"just before subscribe"
"got value 1"
"got value 2"
"got value 3"
"just after subscribe"
"got value 4"
"done"
❯
var observable = Rx.Observable.create(
function (observer) {
observer.next(1);
observer.next(2);
observer.next(3);
setTimeout(() => {
observer.next(4);
observer.complete();
}, 1000);
}
);
console.log('just before subscribe');
observable.subscribe({
next: x => console.log('got value ' + x),
error: err => console.error(
'something wrong occurred: ' + err
),
complete: () => console.log('done'),
});
console.log('just after subscribe');
Run ClearConsoleJavaScript β–Ύ
HTML CSS JavaScript Console Output HelpJS Bin Save
Executing Observables
The code inside Observable.create(function
subscribe(observer) {...}) represents an
"Observable execution", a lazy computation that only
happens for each Observer that subscribes. The execution
produces multiple values over time, either synchronously or
asynchronously.
 
 
 
Types of values.
There are three types of values an Observable Execution
can deliver:
"Next" noti cation: sends a value such as a Number, a
String, an Object, etc.
"Error" noti cation: sends a JavaScript Error or exception.
"Complete" noti cation: does not send a value.
Executing Observables
"got value 1"
"got value 2"
"got value 3"
"done"
❯
const observable = Rx.Observable.create(
function subscribe(observer) {
try {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
observer.next(4);
} catch (err) {
observer.error(err);
}
}
);
observable.subscribe({
next: x => console.log('got value ' + x),
error: err => console.error(
'something wrong occurred: ' + err
),
complete: () => console.log('done'),
});
Run ClearConsoleJavaScript β–Ύ
HTML CSS JavaScript Console Output HelpJS Bin Save
Subject
A Subject is like an Observable, but can multicast to
many Observers.
Subjects are like EventEmitters they maintain a
registry of many listeners
Subject
"observerA: 1"
"observerB: 1"
"observerA: 2"
"observerB: 2"
❯
const subject = new Rx.Subject();
subject.subscribe({
next: (v) => console.log('observerA: ' + v)
});
subject.subscribe({
next: (v) => console.log('observerB: ' + v)
});
subject.next(1);
subject.next(2);
Run ClearConsoleJavaScript β–Ύ
HTML CSS JavaScript Console Output HelpJS Bin Save
Behavior Subject
One of the variants of Subjects is the
BehaviorSubject, which has a notion of "the current
value". It stores the latest value emitted to its consumers,
and whenever a new Observer subscribes, it will
immediately receive the "current value" from the
BehaviorSubject.
Bahavior Subject
"observerA: 0"
"observerA: 1"
"observerA: 2"
"observerB: 2"
"observerA: 3"
"observerB: 3"
❯
const subject = new Rx.BehaviorSubject(0);
// 0 is the initial value
subject.subscribe({
next: (v) => console.log('observerA: ' + v)
});
subject.next(1);
subject.next(2);
subject.subscribe({
next: (v) => console.log('observerB: ' + v)
});
subject.next(3);
Run ClearConsoleJavaScript β–Ύ
HTML CSS JavaScript Console Output HelpJS Bin Save
Replay Subject
A ReplaySubject is similar to a BehaviorSubject in
that it can send old values to new subscribers, but it can
also record a part of the Observable execution.
Replay Subject
"observerA: 1"
"observerA: 2"
"observerA: 3"
"observerA: 4"
"observerB: 2"
"observerB: 3"
"observerB: 4"
"observerA: 5"
"observerB: 5"
❯
const subject = new Rx.ReplaySubject(3);
// buffer 3 values for new subscribers
subject.subscribe({
next: (v) => console.log('observerA: ' + v)
});
subject.next(1);
subject.next(2);
subject.next(3);
subject.next(4);
subject.subscribe({
next: (v) => console.log('observerB: ' + v)
});
subject.next(5);
Run ClearConsoleJavaScript β–Ύ
HTML CSS JavaScript Console Output HelpJS Bin Save
Introduction to RxJS
What are operators?
Operators are methods on the Observable type, such
as .map(...), .filter(...), .merge(...), etc.
When called, they do not change the existing Observable
instance. Instead, they return a new Observable, whose
subscription logic is based on the rst Observable.
Operators
10
20
30
40
❯
function multiplyByTen(input) {
const output = Rx.Observable.create(
function subscribe(observer) {
input.subscribe({
next: (v) => observer.next(10 * v),
error: (err) => observer.error(err),
complete: () => observer.complete()
});
}
);
return output;
}
const input = Rx.Observable.from([1, 2, 3, 4]);
const output = multiplyByTen(input);
output.subscribe(x => console.log(x));
Run ClearConsoleJavaScript β–Ύ
HTML CSS JavaScript Console Output HelpJS Bin Save
Types of Operators
Creation
Transformation
Filtering
Combination
Multicasting
Error Handling
Utility
Operators example
"typed char 1 of
type numeric"
"typed char a of
type alpha"
"typed char @ of
type special"
"typed char Enter
of type special"
❯
const input = document.querySelector('input');
const typedChars = Rx.Observable.fromEvent(input, 'keypress');
const alphaChars = typedChars
.filter(event => /^[A-z]$/.test(event.key))
.map(event => ({ type: 'alpha', value: event.key }));
const numericChars = typedChars
.filter(event => /^d$/.test(event.key))
.map(event => ({ type: 'numeric', value: event.key }));
const specialChars = typedChars
.filter(event => /W|w{2,}/.test(event.key))
.map(event => ({ type: 'special', value: event.key }));
const charsToLog = alphaChars
.merge(numericChars)
.merge(specialChars);
charsToLog.subscribe(key =>
console.log(`typed char ${key.value} of type ${key.type}`)
);
ClearConsoleJavaScript β–Ύ 1a@
Run with JS
Auto-run JS
HTML CSS JavaScript Console Output HelpJS Bin Save
Useful Resources
http://reactivex.io/rxjs/
http://rxmarbles.com/
http://jaredforsyth.com/rxvision/
Questions?

More Related Content

What's hot

RxJS - The Basics & The Future
RxJS - The Basics & The FutureRxJS - The Basics & The Future
RxJS - The Basics & The Future
Tracy Lee
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
Garrett Welson
 
Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular
Jalpesh Vadgama
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
L&T Technology Services Limited
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
HΓΉng Nguyα»…n Huy
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
Saai Vignesh P
 
Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
Christoffer Noring
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?
FITC
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
Thomas Hunter II
 
Introducing type script
Introducing type scriptIntroducing type script
Introducing type script
Remo Jansen
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
Haim Michael
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
Eman Mohamed
 
Angular data binding
Angular data binding Angular data binding
Angular data binding
Sultan Ahmed
 
The New JavaScript: ES6
The New JavaScript: ES6The New JavaScript: ES6
The New JavaScript: ES6
Rob Eisenberg
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFuture
JosΓ© Paumard
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
Forziatech
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
AMD Developer Central
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 

What's hot (20)

RxJS - The Basics & The Future
RxJS - The Basics & The FutureRxJS - The Basics & The Future
RxJS - The Basics & The Future
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
Introducing type script
Introducing type scriptIntroducing type script
Introducing type script
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
 
Angular data binding
Angular data binding Angular data binding
Angular data binding
 
The New JavaScript: ES6
The New JavaScript: ES6The New JavaScript: ES6
The New JavaScript: ES6
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFuture
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
 

Similar to Introduction to RxJS

Luis Atencio on RxJS
Luis Atencio on RxJSLuis Atencio on RxJS
Luis Atencio on RxJS
Luis Atencio
 
From zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java scriptFrom zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java script
Maurice De Beijer [MVP]
 
From zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScriptFrom zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScript
Maurice De Beijer [MVP]
 
Rxjs marble-testing
Rxjs marble-testingRxjs marble-testing
Rxjs marble-testing
Christoffer Noring
 
rx.js make async programming simpler
rx.js make async programming simplerrx.js make async programming simpler
rx.js make async programming simpler
Alexander Mostovenko
 
WebCamp:Front-end Developers Day. АлСксандр ΠœΠΎΡΡ‚ΠΎΠ²Π΅Π½ΠΊΠΎ "Rx.js - Π΄Π΅Π»Π°Π΅ΠΌ асинхр...
WebCamp:Front-end Developers Day. АлСксандр ΠœΠΎΡΡ‚ΠΎΠ²Π΅Π½ΠΊΠΎ "Rx.js - Π΄Π΅Π»Π°Π΅ΠΌ асинхр...WebCamp:Front-end Developers Day. АлСксандр ΠœΠΎΡΡ‚ΠΎΠ²Π΅Π½ΠΊΠΎ "Rx.js - Π΄Π΅Π»Π°Π΅ΠΌ асинхр...
WebCamp:Front-end Developers Day. АлСксандр ΠœΠΎΡΡ‚ΠΎΠ²Π΅Π½ΠΊΠΎ "Rx.js - Π΄Π΅Π»Π°Π΅ΠΌ асинхр...
GeeksLab Odessa
 
Rx – reactive extensions
Rx – reactive extensionsRx – reactive extensions
Rx – reactive extensions
Voislav Mishevski
 
Reactive programming and RxJS
Reactive programming and RxJSReactive programming and RxJS
Reactive programming and RxJS
Ravi Mone
 
Rxjs kyivjs 2015
Rxjs kyivjs 2015Rxjs kyivjs 2015
Rxjs kyivjs 2015
Alexander Mostovenko
 
Rx java in action
Rx java in actionRx java in action
Rx java in action
Pratama Nur Wijaya
 
RxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScriptRxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScript
Viliam Elischer
 
Ajax and JavaScript Bootcamp
Ajax and JavaScript BootcampAjax and JavaScript Bootcamp
Ajax and JavaScript Bootcamp
AndreCharland
 
Reactive, component α„€α…³α„…α…΅α„€α…© angular2
Reactive, component α„€α…³α„…α…΅α„€α…©  angular2Reactive, component α„€α…³α„…α…΅α„€α…©  angular2
Reactive, component α„€α…³α„…α…΅α„€α…© angular2
Jeado Ko
 
Intro to Rx Java
Intro to Rx JavaIntro to Rx Java
Intro to Rx Java
Syed Awais Mazhar Bukhari
 
Rxjs swetugg
Rxjs swetuggRxjs swetugg
Rxjs swetugg
Christoffer Noring
 
Reactive x
Reactive xReactive x
Reactive x
Gabriel Araujo
 
Rx workshop
Rx workshopRx workshop
Rx workshop
Ryan Riley
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
Rick Warren
 
GKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroidGKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroid
GDG Korea
 
Streams
StreamsStreams

Similar to Introduction to RxJS (20)

Luis Atencio on RxJS
Luis Atencio on RxJSLuis Atencio on RxJS
Luis Atencio on RxJS
 
From zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java scriptFrom zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java script
 
From zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScriptFrom zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScript
 
Rxjs marble-testing
Rxjs marble-testingRxjs marble-testing
Rxjs marble-testing
 
rx.js make async programming simpler
rx.js make async programming simplerrx.js make async programming simpler
rx.js make async programming simpler
 
WebCamp:Front-end Developers Day. АлСксандр ΠœΠΎΡΡ‚ΠΎΠ²Π΅Π½ΠΊΠΎ "Rx.js - Π΄Π΅Π»Π°Π΅ΠΌ асинхр...
WebCamp:Front-end Developers Day. АлСксандр ΠœΠΎΡΡ‚ΠΎΠ²Π΅Π½ΠΊΠΎ "Rx.js - Π΄Π΅Π»Π°Π΅ΠΌ асинхр...WebCamp:Front-end Developers Day. АлСксандр ΠœΠΎΡΡ‚ΠΎΠ²Π΅Π½ΠΊΠΎ "Rx.js - Π΄Π΅Π»Π°Π΅ΠΌ асинхр...
WebCamp:Front-end Developers Day. АлСксандр ΠœΠΎΡΡ‚ΠΎΠ²Π΅Π½ΠΊΠΎ "Rx.js - Π΄Π΅Π»Π°Π΅ΠΌ асинхр...
 
Rx – reactive extensions
Rx – reactive extensionsRx – reactive extensions
Rx – reactive extensions
 
Reactive programming and RxJS
Reactive programming and RxJSReactive programming and RxJS
Reactive programming and RxJS
 
Rxjs kyivjs 2015
Rxjs kyivjs 2015Rxjs kyivjs 2015
Rxjs kyivjs 2015
 
Rx java in action
Rx java in actionRx java in action
Rx java in action
 
RxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScriptRxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScript
 
Ajax and JavaScript Bootcamp
Ajax and JavaScript BootcampAjax and JavaScript Bootcamp
Ajax and JavaScript Bootcamp
 
Reactive, component α„€α…³α„…α…΅α„€α…© angular2
Reactive, component α„€α…³α„…α…΅α„€α…©  angular2Reactive, component α„€α…³α„…α…΅α„€α…©  angular2
Reactive, component α„€α…³α„…α…΅α„€α…© angular2
 
Intro to Rx Java
Intro to Rx JavaIntro to Rx Java
Intro to Rx Java
 
Rxjs swetugg
Rxjs swetuggRxjs swetugg
Rxjs swetugg
 
Reactive x
Reactive xReactive x
Reactive x
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 
GKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroidGKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroid
 
Streams
StreamsStreams
Streams
 

More from Brainhub

Czy jest alternatywa dla chmury?
Czy jest alternatywa dla chmury?Czy jest alternatywa dla chmury?
Czy jest alternatywa dla chmury?
Brainhub
 
Jak zostać Dev w DevOps? O zwiększaniu niezależności zespołów developerskich ...
Jak zostać Dev w DevOps? O zwiększaniu niezależności zespołów developerskich ...Jak zostać Dev w DevOps? O zwiększaniu niezależności zespołów developerskich ...
Jak zostać Dev w DevOps? O zwiększaniu niezależności zespołów developerskich ...
Brainhub
 
AWS – jak rozpoczΔ…Δ‡ przygodΔ™ z chmurΔ…?
AWS – jak rozpoczΔ…Δ‡ przygodΔ™ z chmurΔ…?AWS – jak rozpoczΔ…Δ‡ przygodΔ™ z chmurΔ…?
AWS – jak rozpoczΔ…Δ‡ przygodΔ™ z chmurΔ…?
Brainhub
 
Konfiguracja GitLab CI/CD pipelines od podstaw
Konfiguracja GitLab CI/CD pipelines od podstawKonfiguracja GitLab CI/CD pipelines od podstaw
Konfiguracja GitLab CI/CD pipelines od podstaw
Brainhub
 
tRPC - czy to koniec GraphQL?
tRPC - czy to koniec GraphQL?tRPC - czy to koniec GraphQL?
tRPC - czy to koniec GraphQL?
Brainhub
 
Solid.js - nastΔ™pca Reacta?
Solid.js - nastΔ™pca Reacta?Solid.js - nastΔ™pca Reacta?
Solid.js - nastΔ™pca Reacta?
Brainhub
 
Struktury algebraiczne w JavaScripcie
Struktury algebraiczne w JavaScripcieStruktury algebraiczne w JavaScripcie
Struktury algebraiczne w JavaScripcie
Brainhub
 
WebAssembly - czy dzisiaj mi siΔ™ to przyda do pracy?
WebAssembly - czy dzisiaj mi siΔ™ to przyda do pracy?WebAssembly - czy dzisiaj mi siΔ™ to przyda do pracy?
WebAssembly - czy dzisiaj mi siΔ™ to przyda do pracy?
Brainhub
 
Ewoluowanie neuronowych mózgów w JavaScript, wielowątkowo!
Ewoluowanie neuronowych mózgów w JavaScript, wielowątkowo!Ewoluowanie neuronowych mózgów w JavaScript, wielowątkowo!
Ewoluowanie neuronowych mózgów w JavaScript, wielowątkowo!
Brainhub
 
Go home TypeScript, you're drunk!
Go home TypeScript, you're drunk!Go home TypeScript, you're drunk!
Go home TypeScript, you're drunk!
Brainhub
 
How I taught the messenger to tell lame jokes
How I taught the messenger to tell lame jokesHow I taught the messenger to tell lame jokes
How I taught the messenger to tell lame jokes
Brainhub
 
The hunt of the unicorn, to capture productivity
The hunt of the unicorn, to capture productivityThe hunt of the unicorn, to capture productivity
The hunt of the unicorn, to capture productivity
Brainhub
 
TDD in the wild
TDD in the wildTDD in the wild
TDD in the wild
Brainhub
 
WebAssembly - kolejny buzzword, czy (r)ewolucja?
WebAssembly - kolejny buzzword, czy (r)ewolucja?WebAssembly - kolejny buzzword, czy (r)ewolucja?
WebAssembly - kolejny buzzword, czy (r)ewolucja?
Brainhub
 
React performance
React performanceReact performance
React performance
Brainhub
 
React Native in a nutshell
React Native in a nutshellReact Native in a nutshell
React Native in a nutshell
Brainhub
 
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
Brainhub
 
Technologia, a Startup - Brainhub
Technologia, a Startup - BrainhubTechnologia, a Startup - Brainhub
Technologia, a Startup - Brainhub
Brainhub
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
Brainhub
 
How should you React to Redux
How should you React to ReduxHow should you React to Redux
How should you React to Redux
Brainhub
 

More from Brainhub (20)

Czy jest alternatywa dla chmury?
Czy jest alternatywa dla chmury?Czy jest alternatywa dla chmury?
Czy jest alternatywa dla chmury?
 
Jak zostać Dev w DevOps? O zwiększaniu niezależności zespołów developerskich ...
Jak zostać Dev w DevOps? O zwiększaniu niezależności zespołów developerskich ...Jak zostać Dev w DevOps? O zwiększaniu niezależności zespołów developerskich ...
Jak zostać Dev w DevOps? O zwiększaniu niezależności zespołów developerskich ...
 
AWS – jak rozpoczΔ…Δ‡ przygodΔ™ z chmurΔ…?
AWS – jak rozpoczΔ…Δ‡ przygodΔ™ z chmurΔ…?AWS – jak rozpoczΔ…Δ‡ przygodΔ™ z chmurΔ…?
AWS – jak rozpoczΔ…Δ‡ przygodΔ™ z chmurΔ…?
 
Konfiguracja GitLab CI/CD pipelines od podstaw
Konfiguracja GitLab CI/CD pipelines od podstawKonfiguracja GitLab CI/CD pipelines od podstaw
Konfiguracja GitLab CI/CD pipelines od podstaw
 
tRPC - czy to koniec GraphQL?
tRPC - czy to koniec GraphQL?tRPC - czy to koniec GraphQL?
tRPC - czy to koniec GraphQL?
 
Solid.js - nastΔ™pca Reacta?
Solid.js - nastΔ™pca Reacta?Solid.js - nastΔ™pca Reacta?
Solid.js - nastΔ™pca Reacta?
 
Struktury algebraiczne w JavaScripcie
Struktury algebraiczne w JavaScripcieStruktury algebraiczne w JavaScripcie
Struktury algebraiczne w JavaScripcie
 
WebAssembly - czy dzisiaj mi siΔ™ to przyda do pracy?
WebAssembly - czy dzisiaj mi siΔ™ to przyda do pracy?WebAssembly - czy dzisiaj mi siΔ™ to przyda do pracy?
WebAssembly - czy dzisiaj mi siΔ™ to przyda do pracy?
 
Ewoluowanie neuronowych mózgów w JavaScript, wielowątkowo!
Ewoluowanie neuronowych mózgów w JavaScript, wielowątkowo!Ewoluowanie neuronowych mózgów w JavaScript, wielowątkowo!
Ewoluowanie neuronowych mózgów w JavaScript, wielowątkowo!
 
Go home TypeScript, you're drunk!
Go home TypeScript, you're drunk!Go home TypeScript, you're drunk!
Go home TypeScript, you're drunk!
 
How I taught the messenger to tell lame jokes
How I taught the messenger to tell lame jokesHow I taught the messenger to tell lame jokes
How I taught the messenger to tell lame jokes
 
The hunt of the unicorn, to capture productivity
The hunt of the unicorn, to capture productivityThe hunt of the unicorn, to capture productivity
The hunt of the unicorn, to capture productivity
 
TDD in the wild
TDD in the wildTDD in the wild
TDD in the wild
 
WebAssembly - kolejny buzzword, czy (r)ewolucja?
WebAssembly - kolejny buzzword, czy (r)ewolucja?WebAssembly - kolejny buzzword, czy (r)ewolucja?
WebAssembly - kolejny buzzword, czy (r)ewolucja?
 
React performance
React performanceReact performance
React performance
 
React Native in a nutshell
React Native in a nutshellReact Native in a nutshell
React Native in a nutshell
 
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
Ant Colony Optimization (Heuristic algorithms & Swarm intelligence)
 
Technologia, a Startup - Brainhub
Technologia, a Startup - BrainhubTechnologia, a Startup - Brainhub
Technologia, a Startup - Brainhub
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
How should you React to Redux
How should you React to ReduxHow should you React to Redux
How should you React to Redux
 

Recently uploaded

Abortion pills in Fujairah *((+971588192166*)☎️)Β₯) **Effective Abortion Pills...
Abortion pills in Fujairah *((+971588192166*)☎️)Β₯) **Effective Abortion Pills...Abortion pills in Fujairah *((+971588192166*)☎️)Β₯) **Effective Abortion Pills...
Abortion pills in Fujairah *((+971588192166*)☎️)Β₯) **Effective Abortion Pills...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Software development... for all? (keynote at ICSOFT'2024)
Software development... for all? (keynote at ICSOFT'2024)Software development... for all? (keynote at ICSOFT'2024)
Software development... for all? (keynote at ICSOFT'2024)
miso_uam
 
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
arvindkumarji156
 
mobile-app-development-company-in-noida.pdf
mobile-app-development-company-in-noida.pdfmobile-app-development-company-in-noida.pdf
mobile-app-development-company-in-noida.pdf
Mobile App Development Company in Noida - Drona Infotech
 
Kolkata @β„‚all @Girls ꧁❀ 000000000 ❀꧂@β„‚all @Girls Service Vip Top Model Safe
Kolkata @β„‚all @Girls ꧁❀ 000000000 ❀꧂@β„‚all @Girls Service Vip Top Model SafeKolkata @β„‚all @Girls ꧁❀ 000000000 ❀꧂@β„‚all @Girls Service Vip Top Model Safe
Kolkata @β„‚all @Girls ꧁❀ 000000000 ❀꧂@β„‚all @Girls Service Vip Top Model Safe
Misti Soneji
 
ANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdfANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdf
sachin chaurasia
 
@Call @Girls in Tiruppur πŸ€·β€β™‚οΈ XXXXXXXX πŸ€·β€β™‚οΈ Tanisha Sharma Best High Class ...
 @Call @Girls in Tiruppur πŸ€·β€β™‚οΈ  XXXXXXXX πŸ€·β€β™‚οΈ Tanisha Sharma Best High Class ... @Call @Girls in Tiruppur πŸ€·β€β™‚οΈ  XXXXXXXX πŸ€·β€β™‚οΈ Tanisha Sharma Best High Class ...
@Call @Girls in Tiruppur πŸ€·β€β™‚οΈ XXXXXXXX πŸ€·β€β™‚οΈ Tanisha Sharma Best High Class ...
Mona Rathore
 
AI Chatbot Development – A Comprehensive Guideβ€― .pdf
AI Chatbot Development – A Comprehensive Guideβ€― .pdfAI Chatbot Development – A Comprehensive Guideβ€― .pdf
AI Chatbot Development – A Comprehensive Guideβ€― .pdf
ayushiqss
 
Top 10 Tips To Get Google AdSense For Your Website
Top 10 Tips To Get Google AdSense For Your WebsiteTop 10 Tips To Get Google AdSense For Your Website
Top 10 Tips To Get Google AdSense For Your Website
e-Definers Technology
 
Addressing the Top 9 User Pain Points with Visual Design Elements.pptx
Addressing the Top 9 User Pain Points with Visual Design Elements.pptxAddressing the Top 9 User Pain Points with Visual Design Elements.pptx
Addressing the Top 9 User Pain Points with Visual Design Elements.pptx
Sparity1
 
Intro to Amazon Web Services (AWS) and Gen AI
Intro to Amazon Web Services (AWS) and Gen AIIntro to Amazon Web Services (AWS) and Gen AI
Intro to Amazon Web Services (AWS) and Gen AI
Ortus Solutions, Corp
 
WEBINAR SLIDES: CCX for Cloud Service Providers
WEBINAR SLIDES: CCX for Cloud Service ProvidersWEBINAR SLIDES: CCX for Cloud Service Providers
WEBINAR SLIDES: CCX for Cloud Service Providers
Severalnines
 
COMPSAC 2024 D&I Panel: Charting a Course for Equity: Strategies for Overcomi...
COMPSAC 2024 D&I Panel: Charting a Course for Equity: Strategies for Overcomi...COMPSAC 2024 D&I Panel: Charting a Course for Equity: Strategies for Overcomi...
COMPSAC 2024 D&I Panel: Charting a Course for Equity: Strategies for Overcomi...
Hironori Washizaki
 
bangalore @Call @Girls πŸ±β€πŸ‰ XXXXXXXXXX πŸ±β€πŸ‰ WhatsApp Number for Real Meet
bangalore @Call @Girls πŸ±β€πŸ‰  XXXXXXXXXX πŸ±β€πŸ‰ WhatsApp Number for Real Meetbangalore @Call @Girls πŸ±β€πŸ‰  XXXXXXXXXX πŸ±β€πŸ‰ WhatsApp Number for Real Meet
bangalore @Call @Girls πŸ±β€πŸ‰ XXXXXXXXXX πŸ±β€πŸ‰ WhatsApp Number for Real Meet
rajesvigrag
 
Panvel @Call @Girls Whatsapp 9833363713 With High Profile Offer
Panvel @Call @Girls Whatsapp 9833363713 With High Profile OfferPanvel @Call @Girls Whatsapp 9833363713 With High Profile Offer
Panvel @Call @Girls Whatsapp 9833363713 With High Profile Offer
$A19
 
dachnug51 - HCL Domino Roadmap .pdf
dachnug51 - HCL Domino Roadmap      .pdfdachnug51 - HCL Domino Roadmap      .pdf
dachnug51 - HCL Domino Roadmap .pdf
DNUG e.V.
 
Migrate your Infrastructure to the AWS Cloud
Migrate your Infrastructure to the AWS CloudMigrate your Infrastructure to the AWS Cloud
Migrate your Infrastructure to the AWS Cloud
Ortus Solutions, Corp
 
@Call @Girls in Ahmedabad πŸ±β€πŸ‰ XXXXXXXXXX πŸ±β€πŸ‰ Best High Class Ahmedabad Ava...
 @Call @Girls in Ahmedabad πŸ±β€πŸ‰  XXXXXXXXXX πŸ±β€πŸ‰  Best High Class Ahmedabad Ava... @Call @Girls in Ahmedabad πŸ±β€πŸ‰  XXXXXXXXXX πŸ±β€πŸ‰  Best High Class Ahmedabad Ava...
@Call @Girls in Ahmedabad πŸ±β€πŸ‰ XXXXXXXXXX πŸ±β€πŸ‰ Best High Class Ahmedabad Ava...
DiyaSharma6551
 
Major Outages in Major Enterprises Payara Conference
Major Outages in Major Enterprises Payara ConferenceMajor Outages in Major Enterprises Payara Conference
Major Outages in Major Enterprises Payara Conference
Tier1 app
 
Ported to Cloud with Wing_ Blue ZnZone app from _Hexagonal Architecture Expla...
Ported to Cloud with Wing_ Blue ZnZone app from _Hexagonal Architecture Expla...Ported to Cloud with Wing_ Blue ZnZone app from _Hexagonal Architecture Expla...
Ported to Cloud with Wing_ Blue ZnZone app from _Hexagonal Architecture Expla...
Asher Sterkin
 

Recently uploaded (20)

Abortion pills in Fujairah *((+971588192166*)☎️)Β₯) **Effective Abortion Pills...
Abortion pills in Fujairah *((+971588192166*)☎️)Β₯) **Effective Abortion Pills...Abortion pills in Fujairah *((+971588192166*)☎️)Β₯) **Effective Abortion Pills...
Abortion pills in Fujairah *((+971588192166*)☎️)Β₯) **Effective Abortion Pills...
 
Software development... for all? (keynote at ICSOFT'2024)
Software development... for all? (keynote at ICSOFT'2024)Software development... for all? (keynote at ICSOFT'2024)
Software development... for all? (keynote at ICSOFT'2024)
 
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
Bhiwandi @Call @Girls Whatsapp 000000000 With Best And No 1
 
mobile-app-development-company-in-noida.pdf
mobile-app-development-company-in-noida.pdfmobile-app-development-company-in-noida.pdf
mobile-app-development-company-in-noida.pdf
 
Kolkata @β„‚all @Girls ꧁❀ 000000000 ❀꧂@β„‚all @Girls Service Vip Top Model Safe
Kolkata @β„‚all @Girls ꧁❀ 000000000 ❀꧂@β„‚all @Girls Service Vip Top Model SafeKolkata @β„‚all @Girls ꧁❀ 000000000 ❀꧂@β„‚all @Girls Service Vip Top Model Safe
Kolkata @β„‚all @Girls ꧁❀ 000000000 ❀꧂@β„‚all @Girls Service Vip Top Model Safe
 
ANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdfANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdf
 
@Call @Girls in Tiruppur πŸ€·β€β™‚οΈ XXXXXXXX πŸ€·β€β™‚οΈ Tanisha Sharma Best High Class ...
 @Call @Girls in Tiruppur πŸ€·β€β™‚οΈ  XXXXXXXX πŸ€·β€β™‚οΈ Tanisha Sharma Best High Class ... @Call @Girls in Tiruppur πŸ€·β€β™‚οΈ  XXXXXXXX πŸ€·β€β™‚οΈ Tanisha Sharma Best High Class ...
@Call @Girls in Tiruppur πŸ€·β€β™‚οΈ XXXXXXXX πŸ€·β€β™‚οΈ Tanisha Sharma Best High Class ...
 
AI Chatbot Development – A Comprehensive Guideβ€― .pdf
AI Chatbot Development – A Comprehensive Guideβ€― .pdfAI Chatbot Development – A Comprehensive Guideβ€― .pdf
AI Chatbot Development – A Comprehensive Guideβ€― .pdf
 
Top 10 Tips To Get Google AdSense For Your Website
Top 10 Tips To Get Google AdSense For Your WebsiteTop 10 Tips To Get Google AdSense For Your Website
Top 10 Tips To Get Google AdSense For Your Website
 
Addressing the Top 9 User Pain Points with Visual Design Elements.pptx
Addressing the Top 9 User Pain Points with Visual Design Elements.pptxAddressing the Top 9 User Pain Points with Visual Design Elements.pptx
Addressing the Top 9 User Pain Points with Visual Design Elements.pptx
 
Intro to Amazon Web Services (AWS) and Gen AI
Intro to Amazon Web Services (AWS) and Gen AIIntro to Amazon Web Services (AWS) and Gen AI
Intro to Amazon Web Services (AWS) and Gen AI
 
WEBINAR SLIDES: CCX for Cloud Service Providers
WEBINAR SLIDES: CCX for Cloud Service ProvidersWEBINAR SLIDES: CCX for Cloud Service Providers
WEBINAR SLIDES: CCX for Cloud Service Providers
 
COMPSAC 2024 D&I Panel: Charting a Course for Equity: Strategies for Overcomi...
COMPSAC 2024 D&I Panel: Charting a Course for Equity: Strategies for Overcomi...COMPSAC 2024 D&I Panel: Charting a Course for Equity: Strategies for Overcomi...
COMPSAC 2024 D&I Panel: Charting a Course for Equity: Strategies for Overcomi...
 
bangalore @Call @Girls πŸ±β€πŸ‰ XXXXXXXXXX πŸ±β€πŸ‰ WhatsApp Number for Real Meet
bangalore @Call @Girls πŸ±β€πŸ‰  XXXXXXXXXX πŸ±β€πŸ‰ WhatsApp Number for Real Meetbangalore @Call @Girls πŸ±β€πŸ‰  XXXXXXXXXX πŸ±β€πŸ‰ WhatsApp Number for Real Meet
bangalore @Call @Girls πŸ±β€πŸ‰ XXXXXXXXXX πŸ±β€πŸ‰ WhatsApp Number for Real Meet
 
Panvel @Call @Girls Whatsapp 9833363713 With High Profile Offer
Panvel @Call @Girls Whatsapp 9833363713 With High Profile OfferPanvel @Call @Girls Whatsapp 9833363713 With High Profile Offer
Panvel @Call @Girls Whatsapp 9833363713 With High Profile Offer
 
dachnug51 - HCL Domino Roadmap .pdf
dachnug51 - HCL Domino Roadmap      .pdfdachnug51 - HCL Domino Roadmap      .pdf
dachnug51 - HCL Domino Roadmap .pdf
 
Migrate your Infrastructure to the AWS Cloud
Migrate your Infrastructure to the AWS CloudMigrate your Infrastructure to the AWS Cloud
Migrate your Infrastructure to the AWS Cloud
 
@Call @Girls in Ahmedabad πŸ±β€πŸ‰ XXXXXXXXXX πŸ±β€πŸ‰ Best High Class Ahmedabad Ava...
 @Call @Girls in Ahmedabad πŸ±β€πŸ‰  XXXXXXXXXX πŸ±β€πŸ‰  Best High Class Ahmedabad Ava... @Call @Girls in Ahmedabad πŸ±β€πŸ‰  XXXXXXXXXX πŸ±β€πŸ‰  Best High Class Ahmedabad Ava...
@Call @Girls in Ahmedabad πŸ±β€πŸ‰ XXXXXXXXXX πŸ±β€πŸ‰ Best High Class Ahmedabad Ava...
 
Major Outages in Major Enterprises Payara Conference
Major Outages in Major Enterprises Payara ConferenceMajor Outages in Major Enterprises Payara Conference
Major Outages in Major Enterprises Payara Conference
 
Ported to Cloud with Wing_ Blue ZnZone app from _Hexagonal Architecture Expla...
Ported to Cloud with Wing_ Blue ZnZone app from _Hexagonal Architecture Expla...Ported to Cloud with Wing_ Blue ZnZone app from _Hexagonal Architecture Expla...
Ported to Cloud with Wing_ Blue ZnZone app from _Hexagonal Architecture Expla...
 

Introduction to RxJS

  • 1. RxJS - What is it?
  • 2.   Adam GoΕ‚Δ…b Fullstack JS Developer @ Brainhub
  • 3. Reactive Programming Reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change.
  • 4.   Think of RxJS as Lodash for events.  
  • 5. Developed by Microsoft, in collaboration with a community of open source developers. RxJS is a library for composing asynchronous and event- based programs by using observable sequences. It provides one core type, the Observable, satellite types (Observer, Schedulers, Subjects) and operators inspired by Array#extras (map, filter, reduce, every, etc) to allow handling asynchronous events as collections.
  • 7. Observer class Observable { notify() { for observer in this.observers { observer.notifyAboutNewState(); } } }
  • 8. Real world example! Vanilla JS const button = document.querySelector('button'); button.addEventListener('click', () => console.log('Clicked!')) RxJS const button = document.querySelector('button'); Rx.Observable.fromEvent(button, 'click') .subscribe(() => console.log('Clicked!'));
  • 9. Purity! Vanilla JS let count = 0; const button = document.querySelector('button'); button.addEventListener('click', () => console.log(`Clicked ${++count} times`) ); RxJS const button = document.querySelector('button'); Rx.Observable.fromEvent(button, 'click') .scan(count => count + 1, 0) .subscribe(count => console.log(`Clicked ${count} times`));
  • 11. Observable Lazy Push collections of multiple values. They ll the missing spot in the following table:   Single Multiple Pull Function Iterator Push Promise Observable
  • 12. Observable "just before subscribe" "got value 1" "got value 2" "got value 3" "just after subscribe" "got value 4" "done" ❯ var observable = Rx.Observable.create( function (observer) { observer.next(1); observer.next(2); observer.next(3); setTimeout(() => { observer.next(4); observer.complete(); }, 1000); } ); console.log('just before subscribe'); observable.subscribe({ next: x => console.log('got value ' + x), error: err => console.error( 'something wrong occurred: ' + err ), complete: () => console.log('done'), }); console.log('just after subscribe'); Run ClearConsoleJavaScript β–Ύ HTML CSS JavaScript Console Output HelpJS Bin Save
  • 13. Executing Observables The code inside Observable.create(function subscribe(observer) {...}) represents an "Observable execution", a lazy computation that only happens for each Observer that subscribes. The execution produces multiple values over time, either synchronously or asynchronously.      
  • 14. Types of values. There are three types of values an Observable Execution can deliver: "Next" noti cation: sends a value such as a Number, a String, an Object, etc. "Error" noti cation: sends a JavaScript Error or exception. "Complete" noti cation: does not send a value.
  • 15. Executing Observables "got value 1" "got value 2" "got value 3" "done" ❯ const observable = Rx.Observable.create( function subscribe(observer) { try { observer.next(1); observer.next(2); observer.next(3); observer.complete(); observer.next(4); } catch (err) { observer.error(err); } } ); observable.subscribe({ next: x => console.log('got value ' + x), error: err => console.error( 'something wrong occurred: ' + err ), complete: () => console.log('done'), }); Run ClearConsoleJavaScript β–Ύ HTML CSS JavaScript Console Output HelpJS Bin Save
  • 16. Subject A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters they maintain a registry of many listeners
  • 17. Subject "observerA: 1" "observerB: 1" "observerA: 2" "observerB: 2" ❯ const subject = new Rx.Subject(); subject.subscribe({ next: (v) => console.log('observerA: ' + v) }); subject.subscribe({ next: (v) => console.log('observerB: ' + v) }); subject.next(1); subject.next(2); Run ClearConsoleJavaScript β–Ύ HTML CSS JavaScript Console Output HelpJS Bin Save
  • 18. Behavior Subject One of the variants of Subjects is the BehaviorSubject, which has a notion of "the current value". It stores the latest value emitted to its consumers, and whenever a new Observer subscribes, it will immediately receive the "current value" from the BehaviorSubject.
  • 19. Bahavior Subject "observerA: 0" "observerA: 1" "observerA: 2" "observerB: 2" "observerA: 3" "observerB: 3" ❯ const subject = new Rx.BehaviorSubject(0); // 0 is the initial value subject.subscribe({ next: (v) => console.log('observerA: ' + v) }); subject.next(1); subject.next(2); subject.subscribe({ next: (v) => console.log('observerB: ' + v) }); subject.next(3); Run ClearConsoleJavaScript β–Ύ HTML CSS JavaScript Console Output HelpJS Bin Save
  • 20. Replay Subject A ReplaySubject is similar to a BehaviorSubject in that it can send old values to new subscribers, but it can also record a part of the Observable execution.
  • 21. Replay Subject "observerA: 1" "observerA: 2" "observerA: 3" "observerA: 4" "observerB: 2" "observerB: 3" "observerB: 4" "observerA: 5" "observerB: 5" ❯ const subject = new Rx.ReplaySubject(3); // buffer 3 values for new subscribers subject.subscribe({ next: (v) => console.log('observerA: ' + v) }); subject.next(1); subject.next(2); subject.next(3); subject.next(4); subject.subscribe({ next: (v) => console.log('observerB: ' + v) }); subject.next(5); Run ClearConsoleJavaScript β–Ύ HTML CSS JavaScript Console Output HelpJS Bin Save
  • 23. What are operators? Operators are methods on the Observable type, such as .map(...), .filter(...), .merge(...), etc. When called, they do not change the existing Observable instance. Instead, they return a new Observable, whose subscription logic is based on the rst Observable.
  • 24. Operators 10 20 30 40 ❯ function multiplyByTen(input) { const output = Rx.Observable.create( function subscribe(observer) { input.subscribe({ next: (v) => observer.next(10 * v), error: (err) => observer.error(err), complete: () => observer.complete() }); } ); return output; } const input = Rx.Observable.from([1, 2, 3, 4]); const output = multiplyByTen(input); output.subscribe(x => console.log(x)); Run ClearConsoleJavaScript β–Ύ HTML CSS JavaScript Console Output HelpJS Bin Save
  • 26. Operators example "typed char 1 of type numeric" "typed char a of type alpha" "typed char @ of type special" "typed char Enter of type special" ❯ const input = document.querySelector('input'); const typedChars = Rx.Observable.fromEvent(input, 'keypress'); const alphaChars = typedChars .filter(event => /^[A-z]$/.test(event.key)) .map(event => ({ type: 'alpha', value: event.key })); const numericChars = typedChars .filter(event => /^d$/.test(event.key)) .map(event => ({ type: 'numeric', value: event.key })); const specialChars = typedChars .filter(event => /W|w{2,}/.test(event.key)) .map(event => ({ type: 'special', value: event.key })); const charsToLog = alphaChars .merge(numericChars) .merge(specialChars); charsToLog.subscribe(key => console.log(`typed char ${key.value} of type ${key.type}`) ); ClearConsoleJavaScript β–Ύ 1a@ Run with JS Auto-run JS HTML CSS JavaScript Console Output HelpJS Bin Save