Publishers.DropUntilOutput

μ œλ„€λ¦­ ꡬ쑰체 | 두 번째 Publisherλ‘œλΆ€ν„° μš”μ†Œλ₯Ό 전달받을 λ•ŒκΉŒμ§€ μƒμœ„μ— 흐λ₯΄λŠ” Publisherλ‘œλΆ€ν„° μš”μ†Œλ₯Ό λ¬΄μ‹œν•˜λŠ” Publisher

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

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

  • other : 처음 λ°°μΆœλ˜λŠ” μš”μ†Œλ₯Ό λͺ¨λ‹ˆν„°λ§ν•  Publisher

otherμ—μ„œ μš”μ†Œκ°€ 처음으둜 λ°œν–‰λ˜κΈ° μ „κΉŒμ§€λŠ” upstreamμ—μ„œ λ°œν–‰ν•˜λŠ” μš”μ†Œλ₯Ό λ¬΄μ‹œν•œλ‹€.

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

let sourceSubject = PassthroughSubject<Int, Never>()
let otherSubject = PassthroughSubject<Int, Never>()

// Publishers.DropUntilOutput Publisher
Publishers
  .DropUntilOutput(upstream: sourceSubject, other: otherSubject)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine DropUntilOutput Error")
    case .finished:
      print("Combine DropUntilOutput Finish")
    }
  }, receiveValue: { value in
    print("Combine DropUntilOutput : \(value)")
  })
  .store(in: &cancellables)

// drop Operator
sourceSubject
  .drop(untilOutputFrom: otherSubject)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine DropUntilOutput Error")
    case .finished:
      print("Combine DropUntilOutput Finish")
    }
  }, receiveValue: { value in
    print("Combine DropUntilOutput : \(value)")
  })
  .store(in: &cancellables)

// 1
sourceSubject.send(1)
// 2
otherSubject.send(2)
// 3
sourceSubject.send(3)

// Combine DropUntilOutput : 3

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

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

  2. 2의 μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ otherSubject Subject에 2의 값을 μ „λ‹¬ν•œλ‹€.

  3. 3의 μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ sourceSubject Subject에 3의 값을 μ „λ‹¬ν•˜λ©°, otherSubjectκ°€ μš”μ†Œλ₯Ό λ°œν–‰ν•œ 적이 μžˆμœΌλ―€λ‘œ μ „λ‹¬λœ 값을 λ°œν–‰ν•œλ‹€.

결과적으둜 값을 전달받을 λ•Œ μˆ˜ν–‰ν•  ν΄λ‘œμ €λ₯Ό μ‹€ν–‰ν•œλ‹€.

RxSwift

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

let sourceSubject = PublishSubject<Int>()
let otherSubject = PublishSubject<Int>()

sourceSubject.skipUntil(otherSubject)
  .subscribe(onNext: { value in
    print("RxSwift DropUntilOutput : \(value)")
  }, onError: { _ in
    print("RxSwift DropUntilOutput Error")
  }, onCompleted: {
    print("RxSwift DropUntilOutput Finish")
  })
  .disposed(by: disposeBag)

sourceSubject.onNext(1)
otherSubject.onNext(2)
sourceSubject.onNext(3)

// RxSwift DropUntilOutput : 3

ReactiveSwift

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

let sourceProperty = MutableProperty(0)
let otherProperty = MutableProperty(0)

sourceProperty.signal
  .skip(until: otherProperty.signal)
  .observe { event in
    switch event {
    case let .value(value):
      print("ReactiveSwift DropUntilOutput : \(value)")
    case .failed:
      print("ReactiveSwift DropUntilOutput Error")
    case .completed:
      print("ReactiveSwift DropUntilOutput Finish")
    default:
      break
    }
  }

sourceProperty.value = 1
otherProperty.value = 2
sourceProperty.value = 3

// ReactiveSwift DropUntilOutput : 3
// ReactiveSwift DropUntilOutput Finish

μ°Έκ³ 

ReactiveX - Operators - Skip

Last updated