throttle는 입력 소스로부터 값이 발행될 때, 지정된 시간동안 한 번 만 값을 발행하도록 하는 Operator에요.

기능

사용

let subject = PassthroughSubject<String, Never>()

subject
  .throttle(for: 1, scheduler: DispatchQueue.main, latest: true)
  .sink { value in
    print(value)
  }
  .store(in: &cancellables)

subject.send("H")
subject.send("He")
subject.send("Hel")
subject.send("Hell")
subject.send("Hello")

// 결과
(1초 기다린 뒤) Hello
let subject = PassthroughSubject<String, Never>()

subject
  .throttle(for: 1, scheduler: DispatchQueue.main, latest: false)
  .sink { value in
    print(value)
  }
  .store(in: &cancellables)

subject.send("H")
subject.send("He")
subject.send("Hel")
subject.send("Hell")
subject.send("Hello")

// 결과
(1초 기다린 뒤) H