杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//
//  Multicast.swift
//  RxSwift
//
//  Created by Krunoslav Zaher on 2/27/15.
//  Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
 
/**
 Represents an observable wrapper that can be connected and disconnected from its underlying observable sequence.
 */
public class ConnectableObservable<Element>
    : Observable<Element>
    , ConnectableObservableType {
 
    /**
     Connects the observable wrapper to its source. All subscribed observers will receive values from the underlying observable sequence as long as the connection is established.
 
     - returns: Disposable used to disconnect the observable wrapper from its source, causing subscribed observer to stop receiving values from the underlying observable sequence.
     */
    public func connect() -> Disposable {
        rxAbstractMethod()
    }
}
 
extension ObservableType {
 
    /**
    Multicasts the source sequence notifications through an instantiated subject into all uses of the sequence within a selector function.
 
    Each subscription to the resulting sequence causes a separate multicast invocation, exposing the sequence resulting from the selector function's invocation.
 
    For specializations with fixed subject types, see `publish` and `replay`.
 
    - seealso: [multicast operator on reactivex.io](http://reactivex.io/documentation/operators/publish.html)
 
    - parameter subjectSelector: Factory function to create an intermediate subject through which the source sequence's elements will be multicast to the selector function.
    - parameter selector: Selector function which can use the multicasted source sequence subject to the policies enforced by the created subject.
    - returns: An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.
    */
    public func multicast<Subject: SubjectType, Result>(_ subjectSelector: @escaping () throws -> Subject, selector: @escaping (Observable<Subject.Element>) throws -> Observable<Result>)
        -> Observable<Result> where Subject.Observer.Element == Element {
        return Multicast(
            source: self.asObservable(),
            subjectSelector: subjectSelector,
            selector: selector
        )
    }
}
 
extension ObservableType {
 
    /**
    Returns a connectable observable sequence that shares a single subscription to the underlying sequence.
 
    This operator is a specialization of `multicast` using a `PublishSubject`.
 
    - seealso: [publish operator on reactivex.io](http://reactivex.io/documentation/operators/publish.html)
 
    - returns: A connectable observable sequence that shares a single subscription to the underlying sequence.
    */
    public func publish() -> ConnectableObservable<Element> {
        self.multicast { PublishSubject() }
    }
}
 
extension ObservableType {
 
    /**
     Returns a connectable observable sequence that shares a single subscription to the underlying sequence replaying bufferSize elements.
 
     This operator is a specialization of `multicast` using a `ReplaySubject`.
 
     - seealso: [replay operator on reactivex.io](http://reactivex.io/documentation/operators/replay.html)
 
     - parameter bufferSize: Maximum element count of the replay buffer.
     - returns: A connectable observable sequence that shares a single subscription to the underlying sequence.
     */
    public func replay(_ bufferSize: Int)
        -> ConnectableObservable<Element> {
        self.multicast { ReplaySubject.create(bufferSize: bufferSize) }
    }
 
    /**
     Returns a connectable observable sequence that shares a single subscription to the underlying sequence replaying all elements.
 
     This operator is a specialization of `multicast` using a `ReplaySubject`.
 
     - seealso: [replay operator on reactivex.io](http://reactivex.io/documentation/operators/replay.html)
 
     - returns: A connectable observable sequence that shares a single subscription to the underlying sequence.
     */
    public func replayAll()
        -> ConnectableObservable<Element> {
        self.multicast { ReplaySubject.createUnbounded() }
    }
}
 
extension ConnectableObservableType {
 
    /**
    Returns an observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.
 
    - seealso: [refCount operator on reactivex.io](http://reactivex.io/documentation/operators/refcount.html)
 
    - returns: An observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.
    */
    public func refCount() -> Observable<Element> {
        RefCount(source: self)
    }
}
 
extension ObservableType {
 
    /**
     Multicasts the source sequence notifications through the specified subject to the resulting connectable observable.
 
     Upon connection of the connectable observable, the subject is subscribed to the source exactly one, and messages are forwarded to the observers registered with the connectable observable.
 
     For specializations with fixed subject types, see `publish` and `replay`.
 
     - seealso: [multicast operator on reactivex.io](http://reactivex.io/documentation/operators/publish.html)
 
     - parameter subject: Subject to push source elements into.
     - returns: A connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
     */
    public func multicast<Subject: SubjectType>(_ subject: Subject)
        -> ConnectableObservable<Subject.Element> where Subject.Observer.Element == Element {
        ConnectableObservableAdapter(source: self.asObservable(), makeSubject: { subject })
    }
 
    /**
     Multicasts the source sequence notifications through an instantiated subject to the resulting connectable observable.
 
     Upon connection of the connectable observable, the subject is subscribed to the source exactly one, and messages are forwarded to the observers registered with the connectable observable.
 
     Subject is cleared on connection disposal or in case source sequence produces terminal event.
 
     - seealso: [multicast operator on reactivex.io](http://reactivex.io/documentation/operators/publish.html)
 
     - parameter makeSubject: Factory function used to instantiate a subject for each connection.
     - returns: A connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
     */
    public func multicast<Subject: SubjectType>(makeSubject: @escaping () -> Subject)
        -> ConnectableObservable<Subject.Element> where Subject.Observer.Element == Element {
        ConnectableObservableAdapter(source: self.asObservable(), makeSubject: makeSubject)
    }
}
 
final private class Connection<Subject: SubjectType>: ObserverType, Disposable {
    typealias Element = Subject.Observer.Element
 
    private var lock: RecursiveLock
    // state
    private var parent: ConnectableObservableAdapter<Subject>?
    private var subscription : Disposable?
    private var subjectObserver: Subject.Observer
 
    private let disposed = AtomicInt(0)
 
    init(parent: ConnectableObservableAdapter<Subject>, subjectObserver: Subject.Observer, lock: RecursiveLock, subscription: Disposable) {
        self.parent = parent
        self.subscription = subscription
        self.lock = lock
        self.subjectObserver = subjectObserver
    }
 
    func on(_ event: Event<Subject.Observer.Element>) {
        if isFlagSet(self.disposed, 1) {
            return
        }
        if event.isStopEvent {
            self.dispose()
        }
        self.subjectObserver.on(event)
    }
 
    func dispose() {
        lock.lock(); defer { lock.unlock() }
        fetchOr(self.disposed, 1)
        guard let parent = self.parent else {
            return
        }
 
        if parent.connection === self {
            parent.connection = nil
            parent.subject = nil
        }
        self.parent = nil
 
        self.subscription?.dispose()
        self.subscription = nil
    }
}
 
final private class ConnectableObservableAdapter<Subject: SubjectType>
    : ConnectableObservable<Subject.Element> {
    typealias ConnectionType = Connection<Subject>
 
    private let source: Observable<Subject.Observer.Element>
    private let makeSubject: () -> Subject
 
    fileprivate let lock = RecursiveLock()
    fileprivate var subject: Subject?
 
    // state
    fileprivate var connection: ConnectionType?
 
    init(source: Observable<Subject.Observer.Element>, makeSubject: @escaping () -> Subject) {
        self.source = source
        self.makeSubject = makeSubject
        self.subject = nil
        self.connection = nil
    }
 
    override func connect() -> Disposable {
        return self.lock.performLocked {
            if let connection = self.connection {
                return connection
            }
 
            let singleAssignmentDisposable = SingleAssignmentDisposable()
            let connection = Connection(parent: self, subjectObserver: self.lazySubject.asObserver(), lock: self.lock, subscription: singleAssignmentDisposable)
            self.connection = connection
            let subscription = self.source.subscribe(connection)
            singleAssignmentDisposable.setDisposable(subscription)
            return connection
        }
    }
 
    private var lazySubject: Subject {
        if let subject = self.subject {
            return subject
        }
 
        let subject = self.makeSubject()
        self.subject = subject
        return subject
    }
 
    override func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Subject.Element {
        self.lazySubject.subscribe(observer)
    }
}
 
final private class RefCountSink<ConnectableSource: ConnectableObservableType, Observer: ObserverType>
    : Sink<Observer>
    , ObserverType where ConnectableSource.Element == Observer.Element {
    typealias Element = Observer.Element 
    typealias Parent = RefCount<ConnectableSource>
 
    private let parent: Parent
 
    private var connectionIdSnapshot: Int64 = -1
 
    init(parent: Parent, observer: Observer, cancel: Cancelable) {
        self.parent = parent
        super.init(observer: observer, cancel: cancel)
    }
 
    func run() -> Disposable {
        let subscription = self.parent.source.subscribe(self)
        self.parent.lock.lock(); defer { self.parent.lock.unlock() }
 
        self.connectionIdSnapshot = self.parent.connectionId
 
        if self.isDisposed {
            return Disposables.create()
        }
 
        if self.parent.count == 0 {
            self.parent.count = 1
            self.parent.connectableSubscription = self.parent.source.connect()
        }
        else {
            self.parent.count += 1
        }
 
        return Disposables.create {
            subscription.dispose()
            self.parent.lock.lock(); defer { self.parent.lock.unlock() }
            if self.parent.connectionId != self.connectionIdSnapshot {
                return
            }
            if self.parent.count == 1 {
                self.parent.count = 0
                guard let connectableSubscription = self.parent.connectableSubscription else {
                    return
                }
 
                connectableSubscription.dispose()
                self.parent.connectableSubscription = nil
            }
            else if self.parent.count > 1 {
                self.parent.count -= 1
            }
            else {
                rxFatalError("Something went wrong with RefCount disposing mechanism")
            }
        }
    }
 
    func on(_ event: Event<Element>) {
        switch event {
        case .next:
            self.forwardOn(event)
        case .error, .completed:
            self.parent.lock.lock()
            if self.parent.connectionId == self.connectionIdSnapshot {
                let connection = self.parent.connectableSubscription
                defer { connection?.dispose() }
                self.parent.count = 0
                self.parent.connectionId = self.parent.connectionId &+ 1
                self.parent.connectableSubscription = nil
            }
            self.parent.lock.unlock()
            self.forwardOn(event)
            self.dispose()
        }
    }
}
 
final private class RefCount<ConnectableSource: ConnectableObservableType>: Producer<ConnectableSource.Element> {
    fileprivate let lock = RecursiveLock()
 
    // state
    fileprivate var count = 0
    fileprivate var connectionId: Int64 = 0
    fileprivate var connectableSubscription = nil as Disposable?
 
    fileprivate let source: ConnectableSource
 
    init(source: ConnectableSource) {
        self.source = source
    }
 
    override func run<Observer: ObserverType>(_ observer: Observer, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable)
             where Observer.Element == ConnectableSource.Element {
        let sink = RefCountSink(parent: self, observer: observer, cancel: cancel)
        let subscription = sink.run()
        return (sink: sink, subscription: subscription)
    }
}
 
final private class MulticastSink<Subject: SubjectType, Observer: ObserverType>: Sink<Observer>, ObserverType {
    typealias Element = Observer.Element 
    typealias ResultType = Element
    typealias MutlicastType = Multicast<Subject, Observer.Element>
 
    private let parent: MutlicastType
 
    init(parent: MutlicastType, observer: Observer, cancel: Cancelable) {
        self.parent = parent
        super.init(observer: observer, cancel: cancel)
    }
 
    func run() -> Disposable {
        do {
            let subject = try self.parent.subjectSelector()
            let connectable = ConnectableObservableAdapter(source: self.parent.source, makeSubject: { subject })
 
            let observable = try self.parent.selector(connectable)
 
            let subscription = observable.subscribe(self)
            let connection = connectable.connect()
 
            return Disposables.create(subscription, connection)
        }
        catch let e {
            self.forwardOn(.error(e))
            self.dispose()
            return Disposables.create()
        }
    }
 
    func on(_ event: Event<ResultType>) {
        self.forwardOn(event)
        switch event {
        case .next: break
        case .error, .completed:
            self.dispose()
        }
    }
}
 
final private class Multicast<Subject: SubjectType, Result>: Producer<Result> {
    typealias SubjectSelectorType = () throws -> Subject
    typealias SelectorType = (Observable<Subject.Element>) throws -> Observable<Result>
 
    fileprivate let source: Observable<Subject.Observer.Element>
    fileprivate let subjectSelector: SubjectSelectorType
    fileprivate let selector: SelectorType
 
    init(source: Observable<Subject.Observer.Element>, subjectSelector: @escaping SubjectSelectorType, selector: @escaping SelectorType) {
        self.source = source
        self.subjectSelector = subjectSelector
        self.selector = selector
    }
 
    override func run<Observer: ObserverType>(_ observer: Observer, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where Observer.Element == Result {
        let sink = MulticastSink(parent: self, observer: observer, cancel: cancel)
        let subscription = sink.run()
        return (sink: sink, subscription: subscription)
    }
}