-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathCombineLatestMany.swift
71 lines (65 loc) · 2.94 KB
/
CombineLatestMany.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// CombineLatestMany.swift
// CombineExt
//
// Created by Jasdev Singh on 22/03/2020.
// Copyright © 2020 Combine Community. All rights reserved.
//
#if canImport(Combine)
import Combine
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension Publisher {
/// Projects `self` and a `Collection` of `Publisher`s onto a type-erased publisher that chains `combineLatest` calls on
/// the inner publishers. This is a variadic overload on Combine’s variants that top out at arity three.
///
/// - parameter others: A `Collection`-worth of other publishers with matching output and failure types to combine with.
///
/// - returns: A type-erased publisher with value events from `self` and each of the inner publishers `combineLatest`’d
/// together in an array.
func combineLatest<Others: Collection>(with others: Others)
-> AnyPublisher<[Output], Failure>
where Others.Element: Publisher, Others.Element.Output == Output, Others.Element.Failure == Failure {
let seed = map { [$0] }.eraseToAnyPublisher()
return others.reduce(seed) { combined, next in
combined
.combineLatest(next)
.map { $0 + [$1] }
.eraseToAnyPublisher()
}
}
/// Projects `self` and a `Collection` of `Publisher`s onto a type-erased publisher that chains `combineLatest` calls on
/// the inner publishers. This is a variadic overload on Combine’s variants that top out at arity three.
///
/// - parameter others: A `Collection`-worth of other publishers with matching output and failure types to combine with.
///
/// - returns: A type-erased publisher with value events from `self` and each of the inner publishers `combineLatest`’d
/// together in an array.
func combineLatest<Other: Publisher>(with others: Other...)
-> AnyPublisher<[Output], Failure>
where Other.Output == Output, Other.Failure == Failure {
combineLatest(with: others)
}
}
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension Collection where Element: Publisher {
/// Projects a `Collection` of `Publisher`s onto a type-erased publisher that chains `combineLatest` calls on
/// the inner publishers. This is a variadic overload on Combine’s variants that top out at arity three.
///
/// - returns: A type-erased publisher with value events from each of the inner publishers `combineLatest`’d
/// together in an array.
func combineLatest() -> AnyPublisher<[Element.Output], Element.Failure> {
switch count {
case 0:
return Empty().eraseToAnyPublisher()
case 1:
return self[startIndex]
.combineLatest(with: [Element]())
default:
let first = self[startIndex]
let others = self[index(after: startIndex)...]
return first
.combineLatest(with: others)
}
}
}
#endif