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
100
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
RxJSではじめるリアクティブプログラミング
Satoshi Watanabe
February 10, 2018
More Decks by Satoshi Watanabe
See All by Satoshi Watanabe
AIエージェントでのコーディングでわかった理解の重要性
sassy
0
42
StartupAngular2資料
sassy
0
78
最近作ったClovaスキルの紹介
sassy
0
360
作ったスキルの紹介
sassy
1
470
Other Decks in Programming
See All in Programming
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
970
mrbgem 三角測量 開発
ogom
0
230
JRuby: Past, Present, and Future
headius
0
230
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
150
Simple Storage Service(S3) is not simple
iwatsukayura
0
250
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
気持ちぃ〜角丸 Squircle
ryu1013
0
240
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
650
Java 27新機能 / Java 27 new features
kishida
2
220
カツオ、ご期待ください
suneo3476
0
160
golang-bob.pdf
iwatsukayura
0
120
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
150
Featured
See All Featured
Balancing Empowerment & Direction
lara
6
1.3k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
450
Chasing Engaging Ingredients in Design
codingconduct
0
350
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Designing for Timeless Needs
cassininazir
1
520
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
400
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
Color Theory Basics | Prateek | Gurzu
gurzu
1
480
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
370
Six Lessons from altMBA
skipperchong
29
4.5k
Transcript
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!