개요
- 어떤 맥락에서든 접근할 수 있는 상태를 의미한다.
- ReactorKit 에서는 따로 Global states 를 정의하지 않으므로, 정의는 우리가 알아서 할 수 있다.
BehaviorSubject, PublishSubject, Reactor 등으로 구현할 수 있다.
- global state 에서는 Action → Mutation → State 흐름이 없다.
- 따라서 global state 를
transform(mutation:) 을 이용하여 리액터의 mutation 으로 변경해주어야한다.
코드 예시
현재 인증된 사용자를 저장하는 global BehaviorSubject 가 있는 상황
- 만약 currentUser 가 변경되었을 때, Mutation.setUser(User?) 를 방출하고 싶다면, 아래 코드와 같이 구현하면 된다.
var currentUser: BehaviorSubject<User> // global state
func transform(mutation: Observable<Mutation>) -> Observable<Mutation> {
return Observable.merge(mutation, currentUser.map(Mutation.setUser))
}