Slide 1

Slide 1 text

RxJS: Stream-basierte Denkweise am konkreten Beispiel Yannick Baron @yannick_baron https://www.thinktecture.com/yannick-baron

Slide 2

Slide 2 text

Intro: Why Streams? P

Slide 3

Slide 3 text

• various forms of asynchronicity in modern web applications • WebSockets • DOM Events • AJAX Dealing with Asynchronicity

Slide 4

Slide 4 text

• programming paradigm • reacting to asynchronous events 
 (e.g. user input, data updates, asynchronous responses) • data streams and propagation of change Reactive programming

Slide 5

Slide 5 text

• library implementing the Observer pattern • API to describe data streams • shines when handling multiple reactive events • lots of useful operators 
 retrying, error handling, delaying, timing, fi ltering... → for more than just HTTP requests Rx - Reactive Extensions

Slide 6

Slide 6 text

Today's problem

Slide 7

Slide 7 text

• 2 containers: main and side • when scrolling one, the other needs to stay in sync • real world problem I have encountered 
 Goals: • solve problem • transition to stream based solution Problem de fi nition: synchronize scrolling

Slide 8

Slide 8 text

• when one container scrolls, set the scroll position of the other Naive solution

Slide 9

Slide 9 text

• when one container scrolls, set the scroll position of the other Caveats: • setting the scroll position results in emitting a scroll event • results in cyclic updates • will converge, causes fl ickering Naive solution

Slide 10

Slide 10 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Advanced solution

Slide 11

Slide 11 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS P

Slide 12

Slide 12 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS

Slide 13

Slide 13 text

• Operator decision tree https://rxjs-dev. fi rebaseapp.com/operator-decision-tree Finding the right operator

Slide 14

Slide 14 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS https://rxmarbles.com/#race race: The observable to emit fi rst is used.

Slide 15

Slide 15 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS

Slide 16

Slide 16 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS tap: Transparently perform actions or side-effects, such as logging.

Slide 17

Slide 17 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS

Slide 18

Slide 18 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS https://rxmarbles.com/#debounceTime debounceTime: Discard emitted values that take less than the speci fi ed time between output.

Slide 19

Slide 19 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS

Slide 20

Slide 20 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS https://rxmarbles.com/#repeat repeat: Repeats an observable on completion.

Slide 21

Slide 21 text

• determine which container scrolled fi rst • ignore events from second container • update scroll position of the other container • wait for 100ms with no scroll events • reset Stream-based solution with RxJS https://rxmarbles.com/#repeat https://rxmarbles.com/#take repeat: Repeats an observable on completion. take: Emit provided number of values before completing.

Slide 22

Slide 22 text

race: The observable to emit fi rst is used. tap: Transparently perform actions or side- effects, such as logging. debounceTime: Discard emitted values that take less than the speci fi ed time between output. take: Emit provided number of values before completing. repeat: Repeats an observable on completion. Stream-based solution with RxJS

Slide 23

Slide 23 text

It's a wrap! https://www.thinktecture.com/yannick-baron • Ask me your RxJS Questions in an Expert 1on1! • Did you enjoy this Webinar? Learn more in my 3-part article series: 
 https://www.thinktecture.com/en/angular/rxjs-antipattern-1-nested-subs/