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
継続モナドとリアクティブプログラミング
yukikurage
3
610
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1.1k
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
650
技術記事、 専門家としてのプログラマ、 言語化
mizchi
14
7.5k
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
500
Performance Engineering for Everyone
elenatanasoiu
0
270
フィードバックで育てるAI開発
kotaminato
1
120
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.8k
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
140
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
110
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
210
Featured
See All Featured
Music & Morning Musume
bryan
47
7.3k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
4 Signs Your Business is Dying
shpigford
187
22k
BBQ
matthewcrist
89
10k
The Spectacular Lies of Maps
axbom
PRO
1
860
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.5k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
180
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Paper Plane
katiecoart
PRO
2
52k
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ʹτʔΫ͕͋Δ
͓ΘΓ