Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxJSで始めるリアクティブプログラミング

 RxJSで始めるリアクティブプログラミング

「秋のJavaScript祭 in mixi 2017」で発表したスライドです。

Kazuma Nishihata

November 18, 2017
Tweet

More Decks by Kazuma Nishihata

Other Decks in Programming

Transcript

  1. +BWB4DSJQU [1, 2, 3, 4, 5, 6, 7, 8] .map(val

    => val*2) .forEach(val => { console.log(val) });
  2. 3Y+4 Rx.Observable.from([1, 2, 3, 4, 5, 6, 7, 8]) .map(val

    => val*2) .subscribe(val => { console.log(val); });
  3. ඇಉظ const streamA = Rx.Observable.of('A').delay(1000); const streamB = Rx.Observable.of('B').delay(500); const

    streamC = Rx.Observable.of('C').delay(1500); Rx.Observable.forkJoin( streamA, streamB, streamC ).subscribe( data => console.log(data[0], data[1], data[2]) ); 3Y+4
  4. ඇಉظ const proA = new Promise( resolve => setTimeout(resolve, 1000,

    "A") ); const proB = new Promise( resolve => setTimeout(resolve, 500, "B") ); const proC = new Promise( resolve => setTimeout(resolve, 1500, "C") ); Promise.all([proA, proB, proC]) .then(data => console.log(data[0], data[1], data[2])); +BWB4DSJQU
  5. ඇಉظ const streamA = Rx.Observable.of('A').delay(1000); const streamB = Rx.Observable.of('B').delay(500); const

    combine = streamA.map(() => { console.log('Finish A'); return streamB }).concatAll(); combined.subscribe(() => console.log('Finish B')); 3Y+4
  6. ඇಉظ const proA = () => new Promise( resolve =>

    setTimeout(resolve, 1000, "A") ); const proB = () => new Promise( resolve => setTimeout(resolve, 500, "B") ); ( async () => { await proA(); console.log('Finish A'); await proB(); console.log('Finish B'); } )(); +BWB4DSJQU
  7. ϦΞΫςΟϒ const sub = new Rx.BehaviorSubject(0); sub.subscribe( res => {

    console.log(res); }); window.addEventListener('click', () => { sub.next(sub.value + 1); });
  8. ϦΞΫςΟϒ const sub = new Rx.BehaviorSubject(0); sub.subscribe( res => {

    console.log(res); }); window.addEventListener('click', () => { sub.next(sub.value + 1); }); ߪಡ ஋͕มΘͬͨΒ࣮ߦ ஋͕มΘͬͨ͜ͱΛ ڭ͑Δ
  9. ϦΞΫςΟϒ const sub = new Rx.BehaviorSubject(0); sub.subscribe( res => {

    console.log(1, res); }); sub.subscribe( res => { console.log(2, res); }); window.addEventListener('click', () => { sub.next(sub.value + 1); }); ॲཧA ॲཧB
  10. @Injectable() export class CountService { public count = new Rx.BehaviorSubject(0);

    increment() { this.count.next(this.count.value + 1); } } "OHVMBS αʔϏε
  11. export class CountComponent implements OnInit { count: number; constructor( private

    countService: CountService ) { } ngOnInit() { this.countService.count.subscribe( res => { this.count = res; }); } increment() { this.countService.increment(); } } "OHVMBS ίϯϙʔωϯτ
  12. export class CountComponent implements OnInit, OnDestroy { count: number; subscription:

    Subscription; constructor( private countService: CountService ) { } ngOnInit() { const sub = this.countService.count.subscribe( res => { this.count = res; }); this.subscription.add(sub); } ngOnDestroy() { this.subscription.unsubscribe(); } increment() { this.countService.increment(); } } "OHVMBS ίϯϙʔωϯτ
  13. export class CountComponent implements OnInit { count: Observable<number>; constructor( private

    countService: CountService ) { } ngOnInit() { this.count = this.countService.count; } increment() { this.countService.increment(); } } "OHVMBS ίϯϙʔωϯτ