Publishers.PrefixUntilOutput

μ œλ„€λ¦­ ꡬ쑰체

μ΄λ‹ˆμ…œλΌμ΄μ €λŠ” 두 개의 인자λ₯Ό λ°›λŠ”λ‹€.

  • upstream : μƒμœ„μ— 흐λ₯΄λŠ” Publisher

  • other : 첫 좜λ ₯이 upstream Publisherλ₯Ό μ’…λ£Œν•˜κ²Œ ν•˜λŠ” Publisher

λ‹€λ₯Έ Publisherκ°€ 값을 λ‚Ό λ•ŒκΉŒμ§€ κΈ°μ‘΄ Publisherκ°€ μš”μ†Œλ₯Ό λ°œν–‰ν•˜λŠ” λ™μž‘μ„ κ΅¬ν˜„ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•œλ‹€.

prefix μ˜€νΌλ ˆμ΄ν„°μ™€ 관련이 μžˆλ‹€.

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

μ½”λ“œλŠ” λ‹€μŒκ³Ό 같은 μˆœμ„œλ‘œ λ™μž‘ν•œλ‹€.

  1. 1의 μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ sourceSubject에 1의 값을 μ „λ‹¬ν•˜λ©°, otherSubjectκ°€ 값을 λ°œν–‰ν•œ 적이 μ—†μœΌλ―€λ‘œ κ·ΈλŒ€λ‘œ 1의 값을 λ‚Έλ‹€.

  2. 2의 μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ otherSubject에 2의 값을 μ „λ‹¬ν•œλ‹€. 이 λ•Œ sourceSubjectλŠ” λ‹€λ₯Έ Publisherκ°€ 값을 μ „λ‹¬λ°›μ•˜μœΌλ―€λ‘œ μ’…λ£Œν•œλ‹€.

  3. 3의 μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ sourceSubject에 3의 값을 μ „λ‹¬ν•œλ‹€. ν•˜μ§€λ§Œ sourceSubjectλŠ” 4의 μ½”λ“œμ˜ 싀행에 μ˜ν•΄ 이미 μ’…λ£Œν•˜μ˜€μœΌλ―€λ‘œ 효λ ₯이 μ—†λ‹€.

RxSwift

takeUntil μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ κ΅¬ν˜„ν•  수 μžˆλ‹€.

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

ReactiveSwift

take μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ κ΅¬ν˜„ν•  수 μžˆλ‹€.

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

μ°Έκ³ 

ReactiveX - Operators - Take

Last updated