Created
December 22, 2017 10:10
-
-
Save devxoul/4c8257ef35ecaf509199aa4f1b0e1ef8 to your computer and use it in GitHub Desktop.
Self triggering mutation concept
This file contains 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
extension Reactor { | |
public func plugin<PluginReactor>( | |
keyPath: KeyPath<State, PluginReactor?>, | |
mutation mutationFactory: @escaping (PluginReactor) -> Mutation | |
) -> Observable<Mutation> where PluginReactor: Reactor { | |
return self.stateDidCreate.flatMapLatest { [weak self] () -> Observable<Mutation> in | |
guard let `self` = self else { return .empty() } | |
return self.state.flatMapLatest { state -> Observable<Mutation> in | |
guard let pluginReactor = state[keyPath: keyPath] else { return .empty() } | |
let pluginStates = Observable.merge(pluginReactor.state.skip(1)) | |
return pluginStates.map { _ in mutationFactory(pluginReactor) } | |
} | |
} | |
} | |
public func plugin<PluginReactor>( | |
keyPath: KeyPath<State, [PluginReactor]>, | |
mutation mutationFactory: @escaping ([PluginReactor]) -> Mutation | |
) -> Observable<Mutation> where PluginReactor: Reactor { | |
return self.stateDidCreate.flatMapLatest { [weak self] () -> Observable<Mutation> in | |
guard let `self` = self else { return .empty() } | |
return self.state.flatMapLatest { state -> Observable<Mutation> in | |
let pluginReactors = state[keyPath: keyPath] | |
let pluginStates = Observable.merge(pluginReactors.map { $0.state.skip(1) }) | |
return pluginStates.map { _ in mutationFactory(pluginReactors) } | |
} | |
} | |
} | |
public func plugin<PluginReactor>( | |
keyPath: KeyPath<State, [PluginReactor]?>, | |
mutation mutationFactory: @escaping ([PluginReactor]) -> Mutation | |
) -> Observable<Mutation> where PluginReactor: Reactor { | |
return self.stateDidCreate.flatMapLatest { [weak self] () -> Observable<Mutation> in | |
guard let `self` = self else { return .empty() } | |
return self.state.flatMapLatest { state -> Observable<Mutation> in | |
guard let pluginReactors = state[keyPath: keyPath] else { return .empty() } | |
let pluginStates = Observable.merge(pluginReactors.map { $0.state.skip(1) }) | |
return pluginStates.map { _ in mutationFactory(pluginReactors) } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment