Slide 1

Slide 1 text

RxJS: Reactive If-Else Handling

Slide 2

Slide 2 text

Agenda Reactive programming What is RxJS? The if-else problem Reactive if-else pattern References

Slide 3

Slide 3 text

Reactive programming A programming paradigm concerned with data streams and change propagation. With this paradigm it is possible to express and manipulate dynamic (e.g. event emitters) or static (e.g. arrays) data streams.

Slide 4

Slide 4 text

Reactive programming class LightSwitch { lightBulb; flipSwitchOn() { this.lightBulb.power(true); } }

Slide 5

Slide 5 text

Reactive programming class LightBulb { constructor(lightSwitch) { lightSwitch.subscribe(on => this.power(on)); } }

Slide 6

Slide 6 text

Reactive programming class LightBulb { constructor(lightSwitch) { lightSwitch.subscribe(on => this.power(on)); } }

Slide 7

Slide 7 text

Reactive programming Publisher / Subscriber design pattern reflects the concept. PUBLISHER SUBSCRIBER

Slide 8

Slide 8 text

What is RxJS? Reactive Extensions for JavaScript is a library for transforming, composing, and querying streams of data. See: https://rxjs-dev.firebaseapp.com/

Slide 9

Slide 9 text

What is RxJS?

Slide 10

Slide 10 text

What is RxJS? const { of } = Rx; const numbers = of(1, 2, 3); numbers.subscribe(num => console.log(num)) OBSERVABLE SUBSCRIBER 3 2 1

Slide 11

Slide 11 text

const calls = [ { who: 'boss' }, { who: 'spouse' }, { who: 'unknown' }, { who: 'mum' }, ... ]; const phoneCalls = zip( interval(1000), from(calls) ).pipe(map(([_, call]) => call)); CODE!

Slide 12

Slide 12 text

The if-else problem MAPPING OBSERVABLE SUBSCRIBER map(({ who }) => { if (who === ‘mum’) return ‘ ’; if (who === ‘boss’) return ‘ ’; ... else return ‘ ’; })

Slide 13

Slide 13 text

phoneCalls.pipe( map(({ who }) => { if (who === ‘mum’) return ‘ ’; if (who === ‘boss’) return ‘ ’; if (who === ‘spouse’) return ‘ ’; /** * * More response conditions here * */ else return ‘ ’; }) ); CODE!

Slide 14

Slide 14 text

phoneCalls.pipe( tap(({ who }) => { if (who === ‘mum’) doSomething(); ... else doSomethingElse(); }), map(({ who }) => { if (who === ‘mum’) return ‘ ’; ... else return ‘ ’; }) ); CODE!

Slide 15

Slide 15 text

phoneCalls.pipe( tap(({ who }) => { if (who === ‘mum’) doSomething(); ... else doSomethignElse(); }), map(({ who }) => { if (who === ‘mum’) return ‘ ’; ... else return ‘ ’; }) ); CODE!

Slide 16

Slide 16 text

Reactive if-else pattern MUM const mumResponder = phoneCalls.pipe( filter(({ who }) => who === ‘mum’), map(() => ‘ ’), tap(() => textBack(‘Yes mum...’)) );

Slide 17

Slide 17 text

Reactive if-else pattern MUM const mumResponder = phoneCalls.pipe( filter(({ who }) => who === ‘mum’), map(() => ‘ ’), tap(() => textBack(‘Yes mum...’)) ); BOSS const bossResponder = phoneCalls.pipe( filter(({ who }) => who === ‘boss’), map(() => ‘ ’) );

Slide 18

Slide 18 text

Reactive if-else pattern OBSERVABLE SUBSCRIBER SIDE EFFECTS MUM SPOUSE BOSS UNKNOWN

Slide 19

Slide 19 text

Reactive if-else pattern OBSERVABLE SUBSCRIBER const phoneResponder = merge( mumResponder, spouseResponder, bossResponder, unknownResponder );

Slide 20

Slide 20 text

References https://rxviz.com https://rxjs-dev.firebaseapp.com https://blog.rangle.io/rxjs-where-is-the-if-else-operator

Slide 21

Slide 21 text

Thank you. LinkedIn: /in/jakubbarczyk Twitter: @jakubbarczyk