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

Was this helpful?