ReactiveSwift 中 PublishSubject 的等价物是什么?

What is the equivalent for PublishSubject in ReactiveSwift?

我在我的项目中使用 ReactiveSwift,我想知道 PublishSubject 的等价物是什么?

例如在 RXSwift 中我们可以这样做:

let disposeBag = DisposeBag()

let pubSubj = PublishSubject<String>()

pubSubj.on(.next("(next 1")) //event emitted to no subscribers

pubSubj.subscribe({ //subscriber added, but no replay of "next 1"
                print("line: \(#line),", "event: \([=10=])")
            })
.disposed(by: disposeBag)

pubSubj.on(.next("(next 2")) //event emitted and received by subscriber
pubSubj.onError(MyError.error1) //emits error and terminates sequence

pubSubj.on(.next("next 3")) //pubSubj cannot emit this event
/* prints: 
line: 8, event: next((next 2)
line: 8, event: error(error1) 
*/

ReactiveSwift 没有像 Subject 这样可以同时作为信号输入和输出的单一类型。相反,您可以使用 Signal.pipe() 创建输入及其相应的输出信号:

import ReactiveSwift

enum MyError: Error { case error1 }

let (lifetime, token) = Lifetime.make()

let (signal, input) = Signal<String, MyError>.pipe()

input.send(value: "1")

signal
    .take(during: lifetime)
    .observe {
        print("line: \(#line),", "event: \([=10=])")
    }

input.send(value: "2")
input.send(error: .error1)

input.send(value: "3")

/* prints:
line: 14, event: VALUE 2
line: 14, event: FAILED error1
*/