Skip to content

Instantly share code, notes, and snippets.

@wotjd
Created December 1, 2020 17:26
Show Gist options
  • Save wotjd/22950b2fa3d68cf431c59ce8672ff740 to your computer and use it in GitHub Desktop.
Save wotjd/22950b2fa3d68cf431c59ce8672ff740 to your computer and use it in GitHub Desktop.
import RxSwift
extension ObservableType {
public static func concatEager<S: Sequence>(_ sequence: S) -> Observable<Self.Element>
where S.Element == Observable<Self.Element> {
let multicastedObservables = sequence.map {
$0.multicast(ReplaySubject.createUnbounded())
}
var disposables: [Disposable] = []
return Self
.deferred {
disposables = multicastedObservables.map({ $0.connect() })
return Self.concat(multicastedObservables)
}
.do(onDispose: { disposables.forEach({ $0.dispose() }) })
}
public static func concatEager<C: Collection>(_ collection: C) -> Observable<Self.Element>
where C.Element == Observable<Self.Element> {
let multicastedObservables = collection.map {
$0.multicast(ReplaySubject.createUnbounded())
}
var disposables: [Disposable] = []
return Self
.deferred {
disposables = multicastedObservables.map({ $0.connect() })
return Self.concat(multicastedObservables)
}
.do(onDispose: { disposables.forEach({ $0.dispose() }) })
}
public static func concatEager(_ sources: Observable<Self.Element>...) -> Observable<Self.Element> {
let multicastedObservables = sources.map {
$0.multicast(ReplaySubject.createUnbounded())
}
var disposables: [Disposable] = []
return Self
.deferred {
disposables = multicastedObservables.map({ $0.connect() })
return Self.concat(multicastedObservables)
}
.do(onDispose: { disposables.forEach({ $0.dispose() }) })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment