From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very...

32
From zero to hero with the Reactive Extensions for JavaScript Maurice de Beijer @mauricedb

Transcript of From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very...

Page 1: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

From zero to hero with the Reactive Extensions for JavaScriptMaurice de Beijer

@mauricedb

Page 2: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Who am I?

Maurice de Beijer

The Problem Solver

Microsoft Azure MVP

Freelance developer/instructor

Twitter: @mauricedb and @React_Tutorial

Web: http://www.TheProblemSolver.nl

E-mail: [email protected]

2

Page 4: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Topics

What is RxJS?

Why use it?

How to create observables.

Using operators with observables.

Page 9: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Why?

Reactive programming. Programming with asynchronous data streams.

Most actions are not standalone occurrences. Example: A mouse click triggers an Ajax request which triggers a UI

update.

RxJS composes these streams in a functional style.

Page 10: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Filtering data

Page 11: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

With array functions

Page 12: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

With RxJS

Page 13: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Fetching data

Page 14: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

With promises

Page 15: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

With RxJS

Page 16: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Asynchronous data

Page 17: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

With imperative code

Page 18: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

With RxJS

Page 19: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

The RxJSObservable

An Observable is the object that emits a stream of event. The observer is the code that subscribes to the event stream.

Page 20: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

A simple clock

Page 21: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Unsubscribing

Page 22: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Creating observables

Page 23: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

RxJS operators

Operators are used to operate on the event stream between the source and the subscriber.

There are many operators for all sorts of purposes: Transforming

Filtering

Combining

Error handling

Aggregate

Page 24: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

RxMarblesInteractive diagrams of Rx Observables

Page 25: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Events

Page 26: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Ajax

Page 27: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Retry failed requests

Page 28: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Retry with backing off

Page 29: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Combining streams

Streams can be combined in may ways: Switching

Combine

Merging

Zip

Page 30: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Merge Example

Page 31: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Conclusion

Reactive programming is very powerful.

Compose multiple asynchronous data streams.

Transform streams using operators as needed.

Retry failures.

Cancel subscriptions as needed.

Page 32: From zero to hero with the Reactive extensions for JavaScript · Reactive programming is very powerful. Compose multiple asynchronous data streams. Transform streams using operators

Thank you

Maurice de Beijer - @mauricedb