Create a gist now

Instantly share code, notes, and snippets.

Embed
Self triggering mutation concept
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