Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
RxJSではじめるリアクティブプログラミング
Search
Satoshi Watanabe
February 10, 2018
Programming
110
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
RxJSではじめるリアクティブプログラミング
Satoshi Watanabe
February 10, 2018
Transcript
30 slides
RxJSではじめる リアクティブプログラミング Satoshi Watanabe(@sassy_watson) 2018/2/10 プログラミング生放送勉強会
このプレゼンの目的 • 今日はRxJSとその背景となるリアクティブプログラミングのについて 簡単に話します。
アジェンダ • 自己紹介 • リアクティブプログラミングとは • RxJSとは
自己紹介 • 渡辺 悟史(わたなべ さとし) • twitter: @sassy_watson • 株式会社カケハシ所属
• フロントエンド開発 • モバイルアプリ開発(Android)
株式会社カケハシ • Musubiという薬局での服薬指導ツールを開発 • フロントエンドはAngular • バックエンドは Python3 • カケハシでは一緒に働ける仲間を募集しています!
リアクティブプログラミングと RxJS
リアクティブプログラミングとは • 「何か」に反応するプログラミング • 通知されてくるデータを受け取るたびに反応して処理をするプログラ ミング • 処理の流れを定義してあげて、データがきたらその定義された順番 に処理していくイメージ
Excelの例 先に式を定義する セルに値を入れると計算される (セルに入力された値に反応する)
「何か」に反応=ストリーム • ストリームは通知されるデータの流れ • リアクティブプログラミングはこのストリームを処理していく
Observerパターン
Observerパターン • オブザーバーと呼ばれるオブジェクトをサブジェクトを監視するため に登録する • サブジェクトがイベントを発行するたびにオブザーバーがそのイベン トの処理を行う • リアクティブプログラミングは基本このパターン
RxJSとは • JavaScriptでリアクティブプログラミングを行うためのライブラリ • RxはReactiveExtensionsの略 Observable Observer data
例 import { Observable } from 'rxjs'; let value =
10; Observable.fromEvent(document, 'click') .subscribe( () => { //Success Callback value++; document.getElementById('ret').innerHTML = value.toString(); }, (error) => { // Error Callback console.error(error); }, () => { console.log('complete'); } ) ObserverがSubscribe(購読)することにより データを受け取る データを受け取るコールバックは next/error/completeの3種類
2種類のObservable • Cold Observable • Subscribeするまでデータを流さない • Hot Observable •
Subscribeに関係なくデータを流す
Cold Observable • Subscribeするまでデータを流さない
const ob$ = Observable.interval(1000) .take(5); setTimeout(() => { ob$.subscribe( (x)
=> { console.log(x); }, (err) => {console.log(err)}, () => {console.log('complete')} ) }, 3000); 0 1 2 3 4 complete 結果
Hot Observable • Subscribeに関係なくデータを流す
const ob2$ = Observable.interval(1000) .take(5) .publish(); //publishでHotなObservableを作る ob2$.connect(); //connectでデータを流し始める setTimeout(()
=> { ob2$.subscribe( (x) => { console.log(x); }, (err) => {console.log(err)}, () => {console.log('complete')} ) }, 3000); 2 3 4 complete 結果
オペレータ • Observableが通知したデータをObserverに届くまでにデータを操作す るメソッド • Observableを生成する(メソッドチェーンが使える)
例 Observable.from([1, 2, 3, 4, 5, 6]) .filter(x => x
% 2 == 0) // 2の倍数でfilter .map(x => x * 100) //100かける .subscribe( (x) => { console.log(x); }, (error) => { // Error Callback console.error(error); }, () => { console.log('complate'); } ); > 200 > 400 > 600 > complate 結果
例 二つの入力を入れるとbuttonがenableに なる
const i1 = document.getElementById('input1'); const i2 = document.getElementById('input2'); const input1$
= Observable.fromEvent(i1, "input") .map((x) => { const target = x.target; return target.value !== ''; }); const input2$ = Observable.fromEvent(i2, "input") .map((x) => { const target = x.target; return target.value !== ''; }); Observable.combineLatest(input1$,input2$) .map(([a, b]) => a && b) .distinctUntilChanged() .subscribe((x) => { const button = document.getElementById('button'); if (x) { button.removeAttribute('disabled'); } else { button.setAttribute(‘disabled’, ‘’); } })
Subject • Observableであり、Observerでもあるクラス • subscribeでnext/error/completeのイベントハンドラを登録できる • next()でイベントを起こす・error()でエラーを起こす
const sub$ = new Subject(); sub$.subscribe( (x) => { console.log(x);
}, (err) => { console.error(err); }, () => { console.log('Complete'); } ) sub$.next(10); //10が表示 sub$.next(20); //20が表示 sub$.complete(); //complete
各種オペレータ(一部のみ)
map • それぞれのデータに演算して加工する map (x => 10 * x) 1
2 3 10 20 30
filter • trueが返ってきたものだけ値を使う filter (X => x > 10) 2
1¥ 30 0 22 5 60 1 30 22 60
zip • 二つのストリームの値を結合する zip 1 2 3 4 5 A
B C D 1A 2B 3C 4D
distinctUntilChanged • 値が変化するまで通知しない distinctUntilChanged 1 2 3 4 4 1
2 3 4 1 2 2
まとめ • JavaScriptでプログラミングをするときはRxJSを使ってみましょう! • Enjoy Reactive Programming!
More Decks by Satoshi Watanabe
See All by Satoshi Watanabe
AIエージェントでのコーディングでわかった理解の重要性
sassy
0
48
StartupAngular2資料
sassy
0
82
最近作ったClovaスキルの紹介
sassy
0
370
作ったスキルの紹介
sassy
1
470
Other Decks in Programming
See All in Programming
SODA - FACT BOOK(EN)
sodainc
1
310
RaspberryPi 5で軽くはじめるk3sとtailscaleを用いたホームサーバー構築 (1)
nasum
0
450
あなたはMCPの機能を、まだ知らない。
4geru
1
760
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
210
protojson における文字化けの扱い
kazuki1023
0
470
Kotlin JVM用に作られたライブラリのKMPを前提とした再設計
jsoizo
0
240
Growing PicoRuby's Ecosystem on ESP32
yuuu
2
150
LambdaなのにVM?Lambda MicroVMsを調べてみた / About Lambda Micro VMs
ak2ie
2
110
Scalaの型でマルチテナントの認可を守る
nextbeatdev
0
180
はじめてのPLATEAU
nokonoko1203
1
680
数行の変更でDockerビルドを最大91%高速化~GitLab CIの実行基盤を社内40リポジトリに展開するまで~
picopico
4
1.3k
SONY CISC-NEWS NWS-1750 + NWB-225 ハードウェアメンテとフレームバッファカラー対応 / KOF2026
tsutsui
0
150
Featured
See All Featured
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.3k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
650
Paper Plane
katiecoart
PRO
6
55k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
440
Building Adaptive Systems
keathley
44
3.3k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
330
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.7k
The Pragmatic Product Professional
lauravandoore
37
7.5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Done Done
chrislema
187
17k
Being A Developer After 40
akosma
92
590k
Color Theory Basics | Prateek | Gurzu
gurzu
1
530