Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flux アーキテクチャの話

Avatar for popkirby popkirby
July 29, 2015
440

Flux アーキテクチャの話

W8LT #4の資料

Avatar for popkirby

popkirby

July 29, 2015
Tweet

Transcript

  1. Flux • Application Architecture for Building User Interfaces • https://github.com/facebook/flux

    • MV* ͱ͔Έ͍ͨͳGUI ϓϩάϥϛϯάͷ
 ύλʔϯ • Web ΞϓϦέʔγϣϯ ޲͚
  2. MVC ͷҋ • M, V, Cͷఆ͚͕ٛͩҰਓา͖ͯͦ͠ΕͧΕͷؒ ͷ௨৴ํ๏͕޿·͍ͬͯͳ͍ • View ͸

    Model ΛݟΔ͜ͱ͕Ͱ͖Δͷ͔ʁ • View ͸ Model Λߋ৽Ͱ͖Δͷ͔ʁ • → ܾ·ͬͨఆ͕ٛཉ͍͠
  3. MVC ͷҋ • MVC ͷ೿ੜܥ͕ଟ͗ͯࠞ͢ཚ͢Δ(Ҽԑ) • MVP: Presenterͱ͍͏ϒϥοΫࣾһΛಋೖ • MVVM:

    ViewModel(MVͷؒΛ͍͍ײ͡ʹͱΓ΋ͬ ͨΓɺViewͷঢ়ଶΛ΋ͬͨΓ)Λ௥Ճ • MVW: WhateverͰ͍͍͡ΌΜɺMVCʹ͍ͭͯߟ͑ ΔͷΛ΍ΊΖɺͦΜͳՋ͕͋Ε͹ίʔυΛॻ͚
  4. Flux ͱ͸ • Action -> Dispatcher -> Store -> View

    -> Action -> … ͷྲྀΕΛఆٛ
  5. Action • Ϣʔβͷೖྗ΍ ֎෦ͱ ͷ௨৴͔Β࡞ΒΕΔ • ʮͲͷΑ͏ͳछྨͷૢ ࡞/௨৴͕ߦΘΕ͔ͨʯ ͱ͍͏৘ใͱσʔλΛ ࣋ͭΦϒδΣΫτ

    • Dispatcherʹ౉͞ΕΔ // ActionCreator function increment(count) { AppDispatcher.dispatch({ type: INCREMENT_COUNTER, count }); }
  6. Dispatcher • ड͚औͬͨ Action ͷछྨʹରԠ͢Δ Store ͷίʔϧόο ΫΛݺͼग़͢ // Πϝʔδ

    class Dispatch { register(cb) { this.callbacks.push(cb); } dispatch(action) { for (let cb in this.callbacks) { cb(action); } } }
  7. Store • σʔλɾϏδωεϩδοΫΛ ࣋ͭ • Dispatcher ͔Βड͚औͬͨ௨ ஌Λར༻ͯ͠σʔλΛߋ৽ • ͜ΕҎ֎ʹσʔλΛߋ৽͢

    Δํ๏͸ͳ͍ • σʔλ͕ߋ৽͞ΕͨΒ View ʹ௨஌ // Πϝʔδ let counter = 0; const CounterStore = { getCounter() { return counter; } emitChange() { … } addChangeListener(cb) { … } }; AppDispatcher.register((action) => { switch(action.type) { case INCREMENT_COUNTER: counter += action.count; CounterStore.emitChange(); break; } });
  8. View • Store Λݟͯ UI Λͭ͘ Δ • Ϣʔβ͔ΒͷೖྗΛड ͚औͬͨΒ

    Action Λ ࡞ͬͯ Dispatcher ʹ ౤͛Δ class CounterApp extends Component { constructor() { CounterStore.addChangeListener(this.onChange); } onChange = () => { this.setState({ counter: CounterStore.getCounter() }); } handleIncrement(counter) { CounterActions.increment(counter); } render() { return ( <Counter counter={counter} increment={::this.handleIncrement} /> } }
  9. ͍ͭͰʹ • ࣮͸ Flux ͸࣮࣭తʹ͸ MVC ͱ·ͬͨ͘ಉ͡ • ͍ΘΏΔSmallTalk MVC

    • JavaScript ͕ Web ΞϓϦͷϩδοΫΛ୲౰Ͱ͖ΔΑ͏ʹ ͳͬͯ෮׆ • ͦΕͧΕͷཁૉؒͷؔ܎Λڧௐ͍ͯ͠Δ • ΦϒδΣΫτࢦ޲͸ΦϒδΣΫτ͚ͩ͡Όͳ͍࿦ͱࣅ͍ͯΔ