Skip to content

Instantly share code, notes, and snippets.

@JCSooHwanCho
Created September 2, 2023 13:48
Show Gist options
  • Save JCSooHwanCho/a35a824c216a658930a4004d424c6cba to your computer and use it in GitHub Desktop.
Save JCSooHwanCho/a35a824c216a658930a4004d424c6cba to your computer and use it in GitHub Desktop.
Combine operator that mimics RxSwift's throttle when latest: false
extension Publisher {
func coolDown<S: Scheduler>(for cooltime: S.SchedulerTimeType.Stride,
scheduler: S) -> some Publisher<Self.Output, Self.Failure> {
return self.receive(on: scheduler)
.scan((S.SchedulerTimeType?.none, Self.Output?.none)) {
let eventTime = scheduler.now
let minimumTolerance = scheduler.minimumTolerance
guard let lastSentTime = $0.0 else {
return (eventTime, $1)
}
let diff = lastSentTime.distance(to: eventTime)
guard diff >= (cooltime - minimumTolerance) else {
return (lastSentTime, nil)
}
return (eventTime, $1)
}
.compactMap { $0.1 }
}
}
@applebuddy
Copy link

감사합니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment