Created
June 19, 2020 09:24
-
-
Save atimca/3bf316b23528e18ba47a0e67fe0f9b34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func scope<LocalState, LocalEvent: ComponentableEvent>( | |
state toLocalState: @escaping (State) -> LocalState, | |
event toGlobalEvent: @escaping (LocalEvent) -> Event, | |
feedBacks: [SideEffect<LocalState, LocalEvent>] | |
) -> Store<LocalState, LocalEvent> where LocalEvent.State == LocalState { | |
let localStore = Store<LocalState, LocalEvent>( | |
initial: toLocalState(state), | |
feedBacks: feedBacks, | |
reducer: { localState, localEvent in | |
var state = self.state | |
self.reducer(&state, toGlobalEvent(localEvent)) | |
localState = toLocalState(state) | |
if localEvent != LocalEvent.updateFromOutside(localState) { | |
self.accept(toGlobalEvent(localEvent)) | |
} | |
} | |
) | |
$state | |
.map(toLocalState) | |
.removeDuplicates() | |
.sink { [weak localStore] newValue in | |
localStore?.accept(.updateFromOutside(newValue)) | |
} | |
.store(in: &localStore.disposeBag) | |
return localStore | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment