Created
September 2, 2023 13:48
-
-
Save JCSooHwanCho/a35a824c216a658930a4004d424c6cba to your computer and use it in GitHub Desktop.
Combine operator that mimics RxSwift's throttle when latest: false
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
감사합니다!