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
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6.5k
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
180
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
920
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.8k
A2UI という光を覗いてみる
satohjohn
1
150
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
120
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
170
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
120
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
270
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
300
Featured
See All Featured
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
330
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.6k
RailsConf 2023
tenderlove
30
1.5k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.3k
We Have a Design System, Now What?
morganepeng
55
8.2k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
The Cost Of JavaScript in 2023
addyosmani
55
10k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
370
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
430
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
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ʹτʔΫ͕͋Δ
͓ΘΓ