SlideShare a Scribd company logo
RXJS EVOLVED
PAUL TAYLOR
@trxcllnt
UI PLATFORM TEAM
@
OBSERVABLE
EVENTEMITTER.....
OBSERVABLE ≠ EVENTDISPATCHER
EVENTDELEGATE...
“The idea of future values
— maybe.”
–ME
SINGLE MULTIPLE
SYNCHRONOUS Function Enumerable
ASYNCHRONOUS Promise Observable
THE OTHER “IDEAS” OF VALUES
SINGLE MULTIPLE
PULL Function Enumerable
PUSH Promise Observable
THE OTHER “IDEAS” OF VALUES
FUNCTIONS
// The “idea” of a random number
var getRandomNumber = function() {
return Math.random();
}
// A random number
var rand = getRandomNumber.call();
(LAZINESS)
ANATOMY OF OBSERVABLE
CREATION
SUBSCRIPTION DISPOSAL
var randomNumbers = Observable.create((s) => {
var i = setTimeout(() => {
s.next(Math.random());
s.complete();
}, 1000);
return () => clearTimeout(i);
});
var sub = randomNumbers.subscribe({
next(x) { console.log(x); },
error(e) { console.error(e); },
complete() { console.log(“done”); }
});
var randomNumbers = Observable.create((s) => {
var i = setTimeout(() => {
s.next(Math.random());
s.complete();
}, 1000);
return () => clearTimeout(i);
});
var randomNumbers = Observable.create((s) => {
var i = setTimeout(() => {
s.next(Math.random());
s.complete();
}, 1000);
ANATOMY OF OBSERVABLE
★ CREATION
★ SUBSCRIPTION
★ DISPOSAL
var randomNumbers = Observable.create(
WHAT HAPPENS WHEN…
var randomNumbers = Observable.create((s) => {
var i = setTimeout(() => {
s.next(Math.random());
s.complete();
}, 1000);
return () => clearTimeout(i);
});
randomNumbers.subscribe(x => console.log(‘1: ’ + x));
randomNumbers.subscribe(x => console.log(‘2: ’ + x));
>
> 1: 0.1231982301923192831231
> 2: 0.8178491823912837129834
>
THIS HAPPENS
EVENTEMITTER.....
OBSERVABLE ≠ EVENTDISPATCHER
EVENTDELEGATE...
OBSERVABLE = FUNCTION...........
“Observable is a function that,
when invoked, returns 0-∞ values
between now and the end of time.”
–ME
OPERATORS
OPERATORS
METHODS THAT PERFORM
CALCULATIONS ON THE VALUES
MAP, FILTER, SCAN, REDUCE, FLATMAP, ZIP,
COMBINELATEST, TAKE, SKIP, TIMEINTERVAL,
DELAY, DEBOUNCE, SAMPLE, THROTTLE, ETC.
“lodash for events”
–NOT ME
WRITING AN OPERATOR (OLD)
class Observable {
constructor(subscribe) { this.subscribe = subscribe; }
map(selector) {
var source = this;
return new Observable((destination) => {
return source.subscribe({
next(x) { destination.next(selector(x)) },
error(e) { destination.error(e); },
complete() { destination.complete(); }
});
});
}
}
USING OPERATORS
var grades = { “a”: 100, “b+”: 89, “c-“: 70 };
Observable.of(“a”, “b+”, “c-”)
.map((grade) => grades[grade])
.filter((score) => score > grades[“b+”])
.count()
.subscribe((scoreCount) => {
console.log(scoreCount + “ students received A’s”);
});
SCHEDULERS
SCHEDULERS
CENTRALIZED DISPATCHERS TO
CONTROL CONCURRENCY
IMMEDIATE

TIMEOUT

REQUESTANIMATIONFRAME
USING SCHEDULERS
Observable.range = function(start, length, scheduler) {
return new Observable((subscriber) => {
return scheduler.schedule(({ index, count }) => {
if (subscriber.isUnsubscribed) { return; }
else if (index >= end) {
subscriber.complete();
} else {
subscriber.next(count);
this.schedule({ index: index + 1, count: count + 1 });
}
}, { index: 0, count: start });
});
}
USING SCHEDULERS
Observable.fromEvent(“mouseMove”, document.body)
.throttle(1, Scheduler.requestAnimationFrame)
.map(({ pageX, pageY }) => (<div
className=“red-circle”
style={{ top: pageX, left: pageY }} />
))
.subscribe((mouseDiv) => {
React.render(mouseDiv, “#app-container”);
});
RXJS NEXT (v5.0.0-alpha)
github.com/ReactiveX/RxJS
CONTRIBUTORS
BEN LESH ANDRÈ STALTZ OJ KWON
github.com/ReactiveX/RxJS/graphs/contributors
PRIMARY GOALS
★ MODULARITY
★ PERFORMANCE
★ DEBUGGING
★ EXTENSIBILITY
★ SIMPLER UNIT TESTS
import Observable from ‘@reactivex/rxjs/Observable’;
import ArrayObservable from
‘@reactivex/rxjs/observables/ArrayObservable’;
import reduce from ‘@reactivex/rxjs/operators/reduce’;
Observable.of = ArrayObservable.of;
Observable.prototype.reduce = reduce;
Observable.of(1, 2, 3, 4, 5)
.reduce((acc, i) => acc * i, 1)
.subscribe((result) => console.log(result));
MODULARITY
SPEED
4.3X* FASTER

*AVERAGE

(UP TO 11X)
50-90% FEWER
ALLOCATIONS
MEMORY
10-90% SHORTER
CALL STACKS
DEBUGGING
DEBUGGING
FLATMAP EXAMPLE RXJS 4
DEBUGGING
FLATMAP EXAMPLE RXJS 5
DEBUGGING
SWITCHMAP EXAMPLE RXJS 4
RxJS Evolved
RxJS Evolved
RxJS Evolved
DEBUGGING
SWITCHMAP EXAMPLE RXJS 5
PERFORMANCE + DEBUGGING
★ SCHEDULERS OVERHAUL
★ CLASS-BASED OPERATORS (“LIFT”)
★ UNIFIED OBSERVER + SUBSCRIPTION
★ FLATTENED DISPOSABLE TREE
★ REMOVE TRY-CATCH FROM INTERNALS
FLATMAP VS. LIFT
flatMap<T, R>(selector: (value: T) => Observable<R>): Observable<R>;
lift<T, R>(operator: (subscriber: Observer<T>) => Observer<R>): Observable<R>;
FLATMAP
Observable.prototype.map = function map(project) {
var source = this;
return new Observable(function (observer) {
return source.subscribe(
(x) => observer.next(project(x)),
(e) => observer.error(e),
( ) => observer.complete()
);
});
}
★ CLOSURE SCOPE
★ INFLEXIBLE TYPE
★ CLOSURES SHOULD
BE ON A PROTOTYPE
Observable.prototype.map = function(project) => {
return this.lift(new MapOperator(project));
}
class MapOperator implements Operator {
constructor(project) { this.project = project; }
call(observer) {
return new MapSubscriber(observer, this.project);
}
}
class MapSubscriber extends Subscriber {
constructor(destination, project) {
super(destination);
this.project = project;
}
next(x) { this.destination.next(this.project(x)); }
}
★ NO CLOSURES
★ DELEGATES NEW
OBSERVABLE TO LIFT
★ USES SUBSCRIBER
PROTOTYPE
LIFT
EXTENSIBILITY
★ ALLOW SUBCLASSING OBSERVABLE
★ MAINTAIN SUBJECT BI-DIRECTIONALITY
★ FUTURE BACK-PRESSURE SUPPORT?
SUBCLASS OBSERVABLE
class MouseObservable extends Observable {
constructor(source, operator) {
this.source = source;
this.operator = operator || ((x) => x);
}
lift(operator) { return new MouseObservable(this, operator); }
trackVelocity() {
return this.lift((destination) => {
return new VelocityScanningSubscriber(destination);
});
}
concatFrictionEvents(coefficientOfKineticFriction) { ... }
}
SUBCLASS OBSERVABLE
Observable.fromEvent(“mousedown”, document.body)
.flatMap((downEvent) => {
return Observable.fromEvent(“mousemove”, window)
.let(_ => new MouseObservable(this))
.trackVelocity()
.takeUntil(Observable.fromEvent(“mouseup”, window))
.concatFrictionEvents(0.5)
})
.throttle(1, Schedulers.requestAnimationFrame)
.subscribe(({ x, y }) => {
item.style.top = y;
item.style.left = x;
});
MAINTAIN TWO-WAY SUBJECTS
var naviSocket = new SocketSubject(“ws://127.0.0.1/navi”)
.map(x => JSON.parse(x.data))
.throttle(100);
naviSocket.subscribe((msg) => {
if (msg == “hey, listen!”) {
naviSocket.next(“go away navi!”);
}
});
SIMPLER UNIT TESTS
MARBLE DIAGRAMS AS UNIT TESTS
SIMPLER UNIT TESTS
MARBLE DIAGRAMS AS UNIT TESTS
it('should filter out even values', function() {
var source = hot('--0--1--2--3--4--|');
var expected = '-----1-----3-----|';
expectObservable(source.filter(x => x % 2 == 1)).toBe(expected);
});
RXJS NEXT (v5.0.0-alpha)
github.com/ReactiveX/RxJS

More Related Content

PDF
RxJS - The Basics & The Future
PDF
Introduction to RxJS
PPTX
Introduction to RxJS
PDF
Angular Observables & RxJS Introduction
PPTX
PDF
Angular & RXJS: examples and use cases
PDF
Angular and The Case for RxJS
PDF
RxJS Operators - Real World Use Cases (FULL VERSION)
RxJS - The Basics & The Future
Introduction to RxJS
Introduction to RxJS
Angular Observables & RxJS Introduction
Angular & RXJS: examples and use cases
Angular and The Case for RxJS
RxJS Operators - Real World Use Cases (FULL VERSION)

What's hot (20)

PDF
React Context API
PDF
RxJS & Angular Reactive Forms @ Codemotion 2019
PPTX
Top 10 RxJs Operators in Angular
PDF
An introduction to React.js
PDF
Nestjs MasterClass Slides
PPTX
Redux workshop
PDF
Towards Functional Programming through Hexagonal Architecture
PDF
Introducing Drools
PPTX
PDF
Redux Sagas - React Alicante
PDF
PDF
Domain Modeling with FP (DDD Europe 2020)
PPTX
React state
PDF
ES6 presentation
PDF
Building blocks of Angular
PDF
react redux.pdf
PDF
Angular 2 observables
PDF
Context API in React
PPTX
React hooks
PDF
Angular - Chapter 7 - HTTP Services
React Context API
RxJS & Angular Reactive Forms @ Codemotion 2019
Top 10 RxJs Operators in Angular
An introduction to React.js
Nestjs MasterClass Slides
Redux workshop
Towards Functional Programming through Hexagonal Architecture
Introducing Drools
Redux Sagas - React Alicante
Domain Modeling with FP (DDD Europe 2020)
React state
ES6 presentation
Building blocks of Angular
react redux.pdf
Angular 2 observables
Context API in React
React hooks
Angular - Chapter 7 - HTTP Services
Ad

Viewers also liked (7)

PDF
'The History of Metrics According to me' by Stephen Day
PPTX
PDF
RxJS: A Beginner & Expert's Perspective - ng-conf 2017
PDF
The Power of RxJS in Nativescript + Angular
PDF
如何「畫圖」寫測試 - RxJS Marble Test
PDF
You will learn RxJS in 2017
PPTX
RxJS and Reactive Programming - Modern Web UI - May 2015
'The History of Metrics According to me' by Stephen Day
RxJS: A Beginner & Expert's Perspective - ng-conf 2017
The Power of RxJS in Nativescript + Angular
如何「畫圖」寫測試 - RxJS Marble Test
You will learn RxJS in 2017
RxJS and Reactive Programming - Modern Web UI - May 2015
Ad

Similar to RxJS Evolved (20)

PDF
TI1220 Lecture 6: First-class Functions
PPTX
Rxjs swetugg
PDF
Creating an Uber Clone - Part XVIII - Transcript.pdf
PDF
Reactive programming with RxJS - ByteConf 2018
PDF
TypeScript Introduction
PDF
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
PDF
No More Promises! Let's RxJS!
PDF
Javascript
PDF
Bindings: the zen of montage
PPTX
Rxjs ngvikings
PDF
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
PPTX
Luis Atencio on RxJS
PPTX
Angular2 rxjs
PPTX
Rxjs marble-testing
PDF
Reactive x
PDF
Monadologie
PDF
C Language Lecture 17
PDF
Marble Testing RxJS streams
PDF
rx.js make async programming simpler
PDF
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
TI1220 Lecture 6: First-class Functions
Rxjs swetugg
Creating an Uber Clone - Part XVIII - Transcript.pdf
Reactive programming with RxJS - ByteConf 2018
TypeScript Introduction
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
No More Promises! Let's RxJS!
Javascript
Bindings: the zen of montage
Rxjs ngvikings
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
Luis Atencio on RxJS
Angular2 rxjs
Rxjs marble-testing
Reactive x
Monadologie
C Language Lecture 17
Marble Testing RxJS streams
rx.js make async programming simpler
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
PPTX
L1 - Introduction to python Backend.pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Transform Your Business with a Software ERP System
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
ai tools demonstartion for schools and inter college
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
L1 - Introduction to python Backend.pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
AIRLINE PRICE API | FLIGHT API COST |
Materi-Enum-and-Record-Data-Type (1).pptx
A REACT POMODORO TIMER WEB APPLICATION.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
How Creative Agencies Leverage Project Management Software.pdf
Softaken Excel to vCard Converter Software.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
Online Work Permit System for Fast Permit Processing
Transform Your Business with a Software ERP System
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
ISO 45001 Occupational Health and Safety Management System
ai tools demonstartion for schools and inter college
The Role of Automation and AI in EHS Management for Data Centers.pdf

RxJS Evolved