Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RxJSで状態を管理する / state management by RxJS
Search
adwd
May 23, 2018
Programming
1.3k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
RxJSで状態を管理する / state management by RxJS
adwd
May 23, 2018
More Decks by adwd
See All by adwd
GraphQLのあまり知られていない魅力 (スキーマの表現力編) / domain modeling with GraphQL Schema
adwd
5
6.3k
Savkin先生から学んだぼくの考えた最強のAngularアプリアーキテクチャ / The best Angular application architecture
adwd
1
1.2k
create-react-app-introduction
adwd
7
2k
React/Redux Introduction
adwd
17
5.3k
Other Decks in Programming
See All in Programming
分散システム、なんですぐ死んでしまうん?耐障害性を高めたいあなたのためのレジリエンスパターン入門
mshibuya
7
6.3k
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
130
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
210
Haskell/Servantを通してWebミドルウェアを捉え直す
pizzacat83
1
580
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
110
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
390
act2-costs.pdf
sumedhbala
0
110
フィードバックで育てるAI開発
kotaminato
1
120
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
140
継続モナドとリアクティブプログラミング
yukikurage
3
610
1B+ /day規模のログを管理する技術
broadleaf
0
140
Featured
See All Featured
Color Theory Basics | Prateek | Gurzu
gurzu
0
390
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
350
First, design no harm
axbom
PRO
2
1.2k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
400
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
30 Presentation Tips
portentint
PRO
1
350
A Tale of Four Properties
chriscoyier
163
24k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
A Soul's Torment
seathinner
6
3.1k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Transcript
RxJS Ͱঢ়ଶΛཧ͢Δ Meguro.es #15 @ Drecom
ࣗݾհ • ాխത • @adwd118 (twitter) • @adwd (github) •
~2018/1 Bizreach • 2018/2~ Mercari/Merpay
RxJS
RxJS • ΠϕϯτɾඇಉظΛѻ͏ϥΠϒϥϦ • C#(ຊՈ), Java, Swift, JSͳͲͳͲͷ࣮͕͋Δ • ๛ͳAPIͰඇಉظΛ͔͋ͭ͑Δ
• AngularRxJSΛ͍ͬͯΔ
RxJSͷAPI • ͨ͘͞Μ͋ΔͷͰࠓճ͏͚ͭͩ • Observable • Subject • BehaviorSubject •
operators • map/filter/switchMap/debounceTime …
Observable • ඇಉظʹσʔλ͕ྲྀΕͯ͘ΔΦϒδΣΫτ • addEventListenerΈ͍ͨͳͷ • ͕ԿճདྷΔPromiseΈ͍ͨͳͷ
const obs1 = Rx.Observable.interval(1000); obs1.subscribe(count => console.log(‘count: ‘ + count));
// count: 1 // count: 2 // count: 3 const obs2 = Rx.Observable.fromEvent(document.body, ‘click') obs2.pipe( map(ev => ({ x: ev.clientX, y: ev.clientY })), filter(pos => pos.x > 400), ).subscribe(ev => console.log(ev)) // { x: 1000, y: 1000 } // { x: 1000, y: 200 } ग़ྗ͞Εͳ͍ Observable
Subject • Λྲྀ͢ϝιουੜ͑ͯΔObservable • Subject#next
Subject const myEvent = new Subject(); myEvent.subscribe(ev => console.log(‘event: ‘
+ ev)); myEvent.next(‘hi’) // event: hi myEvent.next(‘yo’) // event: yo
BehaviorSubject • ݱࡏͷΛอ͍࣋ͯ͠ΔSubject • Subjectͱͷࠩҟ • BehaviorSubject#getValue() • BehaviorSubject.subscribeͨ͠ॠؒʹݱࡏ ͷ͕ྲྀΕͯ͘Δ
BehaviorSubject // ॳظΛίϯετϥΫλͷҾʹऔΔɹ const myTodos = new BehaviorSubject([]); myTodos.subscribe(todos =>
console.log(‘todos: ‘ + todos.length)); // todos: 0 // subscribeͨ͠ॠ͕ؒྲྀΕͯ͘Δ myTodos.next([ { id: 1, text: ‘do something’ } ]); // todos: 1
BehaviorSubject = Flux • BehaviorSubject͕΄΅Flux(Redux)Λͬͯ͘ΕΔ • BS#next => action •
BS#subscribe => connect(react-redux) • combineLatest => combineReducers • (BehaviorSubject͡Όͳͯ͘Observableʹͳ Δ)
create-subscription • https://github.com/facebook/react/tree/ master/packages/create-subscription • reactνʔϜެࣜͷύοέʔδ • BehaviorSubjectΛσʔλιʔεͱ͢Δαϯϓ ϧ͕υΩϡϝϯτʹॻ͍ͯ͋Δ •
Observableͷػӡʁ
create-subscription example import { createSubscription } from ‘create-subscription’; const BehaviorSubscription
= createSubscription({ getCurrentValue: behaviorSubject => behaviorSubject.getValue(), subscribe: (behaviorSubject, callback) => { const subscription = behaviorSubject.subscribe(callback); return () => subscription.unsubscribe(); } }); <BehaviorSubscription source={myTodos}> {todos => <TodoList todos={todos} /> </BehaviorSubscription>
examples
Todomvc const todos = new BehaviorSubject([]); const addTodo = text
=> { const current = todos.getValue(); todos.next([…current, { text, id: newId() }]); }; const filter = new BehaviorSubject(‘all’); // all, active, completed const filteredTodos = combineLatest(todos, filter) .pipe(map(v => switch(v.filter) { case ‘all’: return todos; case ‘active: return todos.filter(t => !t.completed); … })) <Todos todos={filteredTodos} onFilterChange={filter.next} addTodo={addTodo} />
search w/ throttling const searchText = new BehaviorSubject(‘’); const searchResults
= searchText.pipe( debounceTime(1000), switchMap(text => fetch(`/search?q=${text}`)) ) <SearchTextInput onChange={text => searchText.next(text)} /> <SearchResults results={searchResults} />
αϯϓϧίʔυ • https://github.com/adwd/react-rxjs-todomvc • reduxjs/reduxͷtodomvc exampleΛϕʔε ʹͯ͠ஔ͖࣮͑ͨ
RxJSʹͯ͠Կ͕͍͍ͷ͔ • ๛ͳΦϖϨʔλͰReduxͷselectorɺmiddlewareΛ ͔ͭͬͯͬͯͨdebounceͱ͔͕؆୯ʹͰ͖Δ • ReduxͱൺΔͱ • actionɺswitchจͱ͔ॻ͔ͳ͍͍ͯ͘ • MobXͱൺΔͱ
• ϚδοΫײ͕ͳ͍ʢRxJSʹϚδοΫײด͡ΒΕΔʣ
RxJSͷΑ͘ͳ͍ͱ͜ • ֶशίετ • APIׂ͕ͱେ͖͘มΘΔʁ(v5 -> v6)
BehaviorSubjectͰFluxͰ͖ͨ • ͋ͱHoCͳΓͰBSɺؔͷࢀরΛίϯ ϙʔωϯτʹ༩͑ͯΕ͍͍ • ୯७ʹexport constͰఆٛͯͦ͠ΕΛίϯ ϙʔωϯτ͔Βࢀর͢ΔͷͰ͖Δ
ࣄͷϓϩδΣΫτʹೖத • Google Mapͷ্Ͱ͍Ζ͍Ζ͢ΔΞϓϦ • ϚοϓͷυϥοάʹԠͯ͡ൣғͷϞϊΛݕࡧ͢ΔAPIΛୟ ͘ • ݱࡏΛnavigator.geolocation.watchPositionͰlistenͯ͠ ϓϩοτ͢Δ
• ·ͩ·ͩվળͨ͘͞Μ͋Δ • ͬͯΔ࠷தʹผϓϩδΣΫτʹҟಈ
·ͱΊ • ࣄͰRedux -> RxJSʹ͍ͯͨ͠ • ࣗͱͯ͘͢͠͝ྑ͘ͳͬͨ • Ҿ͖ܧ͍ͩΤϯδχΞ͕RxJSΒͳ͍߹… •
GoogleͷAdWordsνʔϜ͕Flutter/AngularDartͰࣅͨΑ ͏ͳ͜ͱ(BLoC)ͬͯͨ • DartConf, Google I/O 2018ʹτʔΫ͕͋Δ
͓ΘΓ