Skip to main content
다같이!
FluxUtils
한바퀴
React를 사용해
진행했던 두 프로젝트
AJAX : SuperAgent
Pub/Sub : EventEmitter3
View: React
Router: page.js
React를 사용해
진행했던 두 프로젝트
AJAX : SuperAgent
Pub/Sub : EventEmitter3
View: React
Router: page.js
라이브러리만 조합해 협업할 때
발생하는 다양한 문제들
적당한 수준의 구조와
개발자 간 공통적으로 이해할 수
있는 규칙이나 철학 필요
React와 궁합이 좋은
Flux 계열을 한번 살펴보자!
“ ”
FluxUtils를 사용한 이유
약 3개월 전 JSer.info를 통해
플럭스유틸즈에 관해 알게 됐다.
그리고 다음과 같은 고민을
하게 됐다.
FluxUtils
ReduxFluxUtils
ReduxFluxUtils
클래스로 디자인 돼 있다. 함수로 디자인 돼 있다.
메서드로 reduce를 제공한다.
ReduxFluxUtils
ReduxFluxUtils
todo/complete 액션에 맞춰
상태를 변경하고 반환한다.
DELETE_TODO 액션에 따라
상태를 변경하고 반환한다.
ReduxFluxUtils
ReduxFluxUtils
“
”
두 라이브러리는 닮은 부분이 있다.
두 개발자는 웹 상에서 열정적으로 의견을 주고
받으며 서로에게 영향을 주고 있다.
https://goo.gl/3PDL8X https://goo.gl/iww0V9&
FluxUtils
페이스북의 웹 개발자 빌 피셔(Bill Fisher)가
개발해 제공하는 기본 유틸리티 클래스 집합
이벤트 중앙 허브 격인 「Dispatcher」만제공하는
처음과 달리 다양한 Store와 Container를
실체화해 제공
FluxStore FluxReduceStore FluxMapStore FluxContainer
Store의 기반클래스, EventEmitter를 의존하고
Dispatcher를 구독하여 Flux 아키텍처 흐름의 기본을 실현함“
”
FluxStore
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L15-L18
#L72-L82
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L15-L18
#L72-L82
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L15-L18
#L72-L82
FluxStore는 EventEmitter를
의존한다.
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L15-L18
#L72-L82
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L15-L18
#L72-L82
생성자를 통해 Dispatcher의
인스턴스를 주입받는다.
Dispatcher와 EventEmitter의
인스턴스를 프로퍼티로 설정한다.
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L15-L18
#L72-L82
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L15-L18
#L72-L82
Dispatcher의 register 메서드에
익명함수를전달해해당Dispatcher의
액션을 구독한다.
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L149-L155
#L28-L54
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L149-L155
#L28-L54
__onDispatch 메서드는 추상메서드다.
EventEmitter의 emit 메서드를 통해
자신의 상태 변화를 발행한다.
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L149-L155
#L28-L54
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L130-L137
#L28-L54
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
#L130-L137
#L28-L54값이 변경되면 __emitChange 메서드를
호출해 __changed 프로퍼티를 true로
설정한다.
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
DispatcherFluxStore
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
DispatcherregisterFluxStore
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
addListener
FluxContainer
DispatcherregisterFluxStore
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
addListener
Component
FluxContainer
DispatcherregisterFluxStore
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
addListener
Component
FluxContainer
DispatcherregisterFluxStore
render
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
FluxContainer
FluxStore
Component
Dispatcher
FluxStore
Action
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
FluxContainer
FluxStore
Component
Dispatcher
FluxStore
AjaxAction
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
FluxContainer
FluxStore
Component
Dispatcher
FluxStore
AjaxAction
dispatch
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
FluxContainer
FluxStore
Component
payload
FluxStore
Ajax
Dispatcher
Action
dispatch
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
FluxContainer
FluxStore
Component
Dispatcherpayload
emit
FluxStore
AjaxAction
dispatch
FluxStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxStore.js)
FluxContainer
rerender
FluxStore
Component
Dispatcherpayload
emit
AjaxAction
dispatch
Store를 상속한 클래스
Redux와 비슷한 reduce 메서드 지원“
”
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L22-L45
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L22-L45
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L22-L45
생성자에서 getInitialState 메서드를
호출한다.
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L22-L45
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L22-L45
getInitialState는 추상 메서드로 구체
클래스에서 정의해야한다.
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
Dispatcher의 액션을 전달 받는 메서드가
재정의 돼 있다.
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
reduce 메서드를 호출한다.
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
reduce 메소드 역시 추상 메서드로
구체 클래스에서 정의해야한다.
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L66-L71
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L66-L71
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L66-L71
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L66-L71
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L66-L71
다음과 같은 형태로 정의될 수 있다.
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
기존의 상태 값과 reduce의 결과 값
이 다른지 비교한다.
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
다르다면 값을 변경한다.
__emitChange 메서드를 호출해
__changed 프로퍼티를 true로 변경한다.
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
FluxReduceStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxReduceStore.js)
#L47-L94
최종적으로 emit 하여 상태 변화를 구독자
에게 알린다.
FluxReduceStore
“
”
FluxMapStore
ReduceStore를 상속한 클래스 Store의
상태를 기본적으로 Immutable.Map 으로 선언하고
그와 관련한 몇 가지 메서드를 추가한 클래스
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L40-L44
#L59-L72
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L40-L44
#L59-L72
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L40-L44
#L59-L72
getInitialState 메서드가 정의돼 있고
상태로 Immutable.Map을 반환한다.
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L40-L44
#L59-L72
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L40-L44
#L59-L72
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L40-L44
#L59-L72Immutable.Map에서 값을 가져오기
위한 일부 메서드가 정의돼 있다.
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L26-L37
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L26-L37
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L26-L37
상태는 Immutable.Map 이므로 관련
API를 사용해 값을 변경한다.
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L26-L37
FluxMapStore
(https://github.com/facebook/flux/blob/master/src/stores/FluxMapStore.js)
#L26-L37
컴포넌트에서는 다음과 같이 사용할
수 있다.
FluxMapStore
“
”
FluxContainer
자체적으로 관리하는 State와 Props 없이
Store와 Component의 중간 매개체 역할을 하는
단순한 컨테이너 클래스
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
Container에서 관심있어 하는 Store를
반환한다.
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
componentWillReceiveProps 라이프
사이클 메서드에서 호출된다. Store의
값을 이용해 상태를 변경한다.
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
설정된 상태를 하위 컴포넌트에
전달한다.
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
#L60-L75
#L92-L109
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
#L60-L75
#L92-L109
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
#L60-L75
#L92-L109
componentDidMount 라이플 사이클
메서드에서 getStores 메서드를 통해
관계를 맺는 Store 리스트를 얻는다.
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
#L60-L75
#L92-L109
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
#L60-L75
#L92-L109
Store 리스트를 순회하며 addListener
메서드를 호출해 상태 변화를 구독한다.
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
#L60-L75
#L92-L109
FluxContainer
(https://github.com/facebook/flux/blob/master/src/stores/FluxContainer.js)
#L60-L75
#L92-L109
componentWillReceiveProps 라이프 사이클 메서
드에서 calculateState 메서드를 호출하며 이 메서
드에서 반환되는 값을 setState 한다.
FluxContainer
FluxUtils 기반으로 개발한
사내 회의실 예약 웹 애플리케이션
만들면서 느낀 몇 가지 규칙을 정리
규칙 1.
Container에서 자체적으로 상태를
다루거나 이벤트를 처리하지 않는다.
Container는 Store를 구독하고
Store의 상태 값을 하위 컴포넌트에
전달하는 역할로만 적합하다.
일반적인 방법으로 컴포넌트 독자적인
상태를 선언했다.
?