杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
//  GroupedObservable.swift
//  RxSwift
//
//  Created by Tomi Koskinen on 01/12/15.
//  Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
 
/// Represents an observable sequence of elements that share a common key.
/// `GroupedObservable` is typically created by the `groupBy` operator.
/// Each `GroupedObservable` instance represents a collection of elements
/// that are grouped by a specific key.
///
/// Example usage:
/// ```
/// let observable = Observable.of("Apple", "Banana", "Apricot", "Blueberry", "Avocado")
///
/// let grouped = observable.groupBy { fruit in
///     fruit.first! // Grouping by the first letter of each fruit
/// }
///
/// _ = grouped.subscribe { group in
///     print("Group: \(group.key)")
///     _ = group.subscribe { event in
///         print(event)
///     }
/// }
/// ```
/// This will print:
/// ```
/// Group: A
/// next(Apple)
/// next(Apricot)
/// next(Avocado)
/// Group: B
/// next(Banana)
/// next(Blueberry)
/// ```
public struct GroupedObservable<Key, Element> : ObservableType {
    /// The key associated with this grouped observable sequence.
    /// All elements emitted by this observable share this common key.
    public let key: Key
 
    private let source: Observable<Element>
 
    /// Initializes a grouped observable sequence with a key and a source observable sequence.
    ///
    /// - Parameters:
    ///   - key: The key associated with this grouped observable sequence.
    ///   - source: The observable sequence of elements for the specified key.
    ///
    /// Example usage:
    /// ```
    /// let sourceObservable = Observable.of("Apple", "Apricot", "Avocado")
    /// let groupedObservable = GroupedObservable(key: "A", source: sourceObservable)
    ///
    /// _ = groupedObservable.subscribe { event in
    ///     print(event)
    /// }
    /// ```
    /// This will print:
    /// ```
    /// next(Apple)
    /// next(Apricot)
    /// next(Avocado)
    /// ```
    public init(key: Key, source: Observable<Element>) {
        self.key = key
        self.source = source
    }
 
    /// Subscribes an observer to receive events emitted by the source observable sequence.
    ///
    /// - Parameter observer: The observer that will receive the events of the source observable.
    /// - Returns: A `Disposable` representing the subscription, which can be used to cancel the subscription.
    ///
    /// Example usage:
    /// ```
    /// let fruitsObservable = Observable.of("Apple", "Banana", "Apricot", "Blueberry", "Avocado")
    /// let grouped = fruitsObservable.groupBy { $0.first! } // Group by first letter
    ///
    /// _ = grouped.subscribe { group in
    ///     if group.key == "A" {
    ///         _ = group.subscribe { event in
    ///             print(event)
    ///         }
    ///     }
    /// }
    /// ```
    /// This will print:
    /// ```
    /// next(Apple)
    /// next(Apricot)
    /// next(Avocado)
    /// ```
    public func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element {
        self.source.subscribe(observer)
    }
 
    /// Converts this `GroupedObservable` into a regular `Observable` sequence.
    /// This allows you to work with the sequence without directly interacting with the key.
    ///
    /// - Returns: The underlying `Observable` sequence of elements for the specified key.
    ///
    /// Example usage:
    /// ```
    /// let fruitsObservable = Observable.of("Apple", "Banana", "Apricot", "Blueberry", "Avocado")
    /// let grouped = fruitsObservable.groupBy { $0.first! } // Group by first letter
    ///
    /// _ = grouped.subscribe { group in
    ///     if group.key == "A" {
    ///         let regularObservable = group.asObservable()
    ///         _ = regularObservable.subscribe { event in
    ///             print(event)
    ///         }
    ///     }
    /// }
    /// ```
    /// This will print:
    /// ```
    /// next(Apple)
    /// next(Apricot)
    /// next(Avocado)
    /// ```
    public func asObservable() -> Observable<Element> {
        self.source
    }
}