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

Was this helpful?