|
6 | 6 | // Copyright © 2015 Krunoslav Zaher. All rights reserved.
|
7 | 7 | //
|
8 | 8 |
|
9 |
| -/// Represents an observable sequence of elements that have a common key. |
| 9 | +/// Represents an observable sequence of elements that share a common key. |
| 10 | +/// `GroupedObservable` is typically created by the `groupBy` operator. |
| 11 | +/// Each `GroupedObservable` instance represents a collection of elements |
| 12 | +/// that are grouped by a specific key. |
| 13 | +/// |
| 14 | +/// Example usage: |
| 15 | +/// ``` |
| 16 | +/// let observable = Observable.of("Apple", "Banana", "Apricot", "Blueberry", "Avocado") |
| 17 | +/// |
| 18 | +/// let grouped = observable.groupBy { fruit in |
| 19 | +/// fruit.first! // Grouping by the first letter of each fruit |
| 20 | +/// } |
| 21 | +/// |
| 22 | +/// _ = grouped.subscribe { group in |
| 23 | +/// print("Group: \(group.key)") |
| 24 | +/// _ = group.subscribe { event in |
| 25 | +/// print(event) |
| 26 | +/// } |
| 27 | +/// } |
| 28 | +/// ``` |
| 29 | +/// This will print: |
| 30 | +/// ``` |
| 31 | +/// Group: A |
| 32 | +/// next(Apple) |
| 33 | +/// next(Apricot) |
| 34 | +/// next(Avocado) |
| 35 | +/// Group: B |
| 36 | +/// next(Banana) |
| 37 | +/// next(Blueberry) |
| 38 | +/// ``` |
10 | 39 | public struct GroupedObservable<Key, Element> : ObservableType {
|
11 |
| - /// Gets the common key. |
| 40 | + /// The key associated with this grouped observable sequence. |
| 41 | + /// All elements emitted by this observable share this common key. |
12 | 42 | public let key: Key
|
13 | 43 |
|
14 | 44 | private let source: Observable<Element>
|
15 | 45 |
|
16 |
| - /// Initializes grouped observable sequence with key and source observable sequence. |
| 46 | + /// Initializes a grouped observable sequence with a key and a source observable sequence. |
17 | 47 | ///
|
18 |
| - /// - parameter key: Grouped observable sequence key |
19 |
| - /// - parameter source: Observable sequence that represents sequence of elements for the key |
20 |
| - /// - returns: Grouped observable sequence of elements for the specific key |
| 48 | + /// - Parameters: |
| 49 | + /// - key: The key associated with this grouped observable sequence. |
| 50 | + /// - source: The observable sequence of elements for the specified key. |
| 51 | + /// |
| 52 | + /// Example usage: |
| 53 | + /// ``` |
| 54 | + /// let sourceObservable = Observable.of("Apple", "Apricot", "Avocado") |
| 55 | + /// let groupedObservable = GroupedObservable(key: "A", source: sourceObservable) |
| 56 | + /// |
| 57 | + /// _ = groupedObservable.subscribe { event in |
| 58 | + /// print(event) |
| 59 | + /// } |
| 60 | + /// ``` |
| 61 | + /// This will print: |
| 62 | + /// ``` |
| 63 | + /// next(Apple) |
| 64 | + /// next(Apricot) |
| 65 | + /// next(Avocado) |
| 66 | + /// ``` |
21 | 67 | public init(key: Key, source: Observable<Element>) {
|
22 | 68 | self.key = key
|
23 | 69 | self.source = source
|
24 | 70 | }
|
25 | 71 |
|
26 |
| - /// Subscribes `observer` to receive events for this sequence. |
| 72 | + /// Subscribes an observer to receive events emitted by the source observable sequence. |
| 73 | + /// |
| 74 | + /// - Parameter observer: The observer that will receive the events of the source observable. |
| 75 | + /// - Returns: A `Disposable` representing the subscription, which can be used to cancel the subscription. |
| 76 | + /// |
| 77 | + /// Example usage: |
| 78 | + /// ``` |
| 79 | + /// let fruitsObservable = Observable.of("Apple", "Banana", "Apricot", "Blueberry", "Avocado") |
| 80 | + /// let grouped = fruitsObservable.groupBy { $0.first! } // Group by first letter |
| 81 | + /// |
| 82 | + /// _ = grouped.subscribe { group in |
| 83 | + /// if group.key == "A" { |
| 84 | + /// _ = group.subscribe { event in |
| 85 | + /// print(event) |
| 86 | + /// } |
| 87 | + /// } |
| 88 | + /// } |
| 89 | + /// ``` |
| 90 | + /// This will print: |
| 91 | + /// ``` |
| 92 | + /// next(Apple) |
| 93 | + /// next(Apricot) |
| 94 | + /// next(Avocado) |
| 95 | + /// ``` |
27 | 96 | public func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element {
|
28 | 97 | self.source.subscribe(observer)
|
29 | 98 | }
|
30 | 99 |
|
31 |
| - /// Converts `self` to `Observable` sequence. |
| 100 | + /// Converts this `GroupedObservable` into a regular `Observable` sequence. |
| 101 | + /// This allows you to work with the sequence without directly interacting with the key. |
| 102 | + /// |
| 103 | + /// - Returns: The underlying `Observable` sequence of elements for the specified key. |
| 104 | + /// |
| 105 | + /// Example usage: |
| 106 | + /// ``` |
| 107 | + /// let fruitsObservable = Observable.of("Apple", "Banana", "Apricot", "Blueberry", "Avocado") |
| 108 | + /// let grouped = fruitsObservable.groupBy { $0.first! } // Group by first letter |
| 109 | + /// |
| 110 | + /// _ = grouped.subscribe { group in |
| 111 | + /// if group.key == "A" { |
| 112 | + /// let regularObservable = group.asObservable() |
| 113 | + /// _ = regularObservable.subscribe { event in |
| 114 | + /// print(event) |
| 115 | + /// } |
| 116 | + /// } |
| 117 | + /// } |
| 118 | + /// ``` |
| 119 | + /// This will print: |
| 120 | + /// ``` |
| 121 | + /// next(Apple) |
| 122 | + /// next(Apricot) |
| 123 | + /// next(Avocado) |
| 124 | + /// ``` |
32 | 125 | public func asObservable() -> Observable<Element> {
|
33 | 126 | self.source
|
34 | 127 | }
|
35 | 128 | }
|
| 129 | + |
0 commit comments