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
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
4.1k
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
540
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
630
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.9k
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
450
その節約、円になってますか?
isamumumu
1
700
What's New in Android 2026
veronikapj
0
250
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
300
AI時代、エンジニアはどう育つのか -未経験エンジニアの成長を間近で見て考えたこと-
thasu0123
0
220
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
350
メールのエイリアス機能を履き違えない
isshinfunada
0
230
5分で問診!Composer セキュリティ健康診断
codmoninc
0
910
Featured
See All Featured
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
360
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Everyday Curiosity
cassininazir
0
270
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
350
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
570
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
460
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
490
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
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ʹτʔΫ͕͋Δ
͓ΘΓ