이니셜라이저는 두 개의 인자를 받는다.
다른 Publisher가 값을 낼 때까지 기존 Publisher가 요소를 발행하는 동작을 구현하기 위해 사용한다.
let sourceSubject = PassthroughSubject<Int, Never>()
let otherSubject = PassthroughSubject<Int, Never>()
// Publishers.PrefixUntilOutput Publisher
Publishers
.PrefixUntilOutput(upstream: sourceSubject, other: otherSubject)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine PrefixUntilOutput Error")
case .finished:
print("Combine PrefixUntilOutput Finish")
}
}, receiveValue: { value in
print("Combine PrefixUntilOutput : \(value)")
})
.store(in: &cancellables)
// prefix Operator
sourceSubject
.prefix(untilOutputFrom: otherSubject)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine PrefixUntilOutput Error")
case .finished:
print("Combine PrefixUntilOutput Finish")
}
}, receiveValue: { value in
print("Combine PrefixUntilOutput : \(value)")
})
.store(in: &cancellables)
// 1
sourceSubject.send(1)
// 2
otherSubject.send(2)
// 3
sourceSubject.send(3)
// Combine PrefixUntilOutput : 1
// Combine PrefixUntilOutput Finish
코드는 다음과 같은 순서로 동작한다.
let sourceSubject = PublishSubject<Int>()
let otherSubject = PublishSubject<Int>()
sourceSubject
.takeUntil(otherSubject)
.subscribe(onNext: { value in
print("RxSwift PrefixUntilOutput : \(value)")
}, onError: { _ in
print("RxSwift PrefixUntilOutput Error")
}, onCompleted: {
print("RxSwift PrefixUntilOutput Finish")
})
.disposed(by: disposeBag)
sourceSubject.onNext(1)
otherSubject.onNext(2)
sourceSubject.onNext(3)
// RxSwift PrefixUntilOutput : 1
// RxSwift PrefixUntilOutput Finish
let sourceProperty = MutableProperty(0)
let otherProperty = MutableProperty(0)
sourceProperty.signal
.take(until: otherProperty.signal.map(value: Void()))
.observe { event in
switch event {
case let .value(value):
print("ReactiveSwift PrefixUntilOutput : \(value)")
case .failed:
print("ReactiveSwift PrefixUntilOutput Error")
case .completed:
print("ReactiveSwift PrefixUntilOutput Finish")
default:
break
}
}
sourceProperty.value = 1
otherProperty.value = 2
sourceProperty.value = 3
// ReactiveSwift PrefixUntilOutput : 1
// ReactiveSwift PrefixUntilOutput Finish