throttle(for:scheduler:latest:)
Publishes either the most-recent or first element published by the upstream publisher in the specified time interval.
Declaration
func throttle<S>(for interval: S.SchedulerTimeType.Stride, scheduler: S, latest: Bool) -> Publishers.Throttle<Self, S> where S : SchedulerParameters
- interval:
The interval at which to find and emit either the most recent or the first element, expressed in the time system of the scheduler.
- scheduler:
The scheduler on which to publish elements.
- latest:
A Boolean value that indicates whether to publish the most recent element. If
false, the publisher emits the first element received during the interval.
Mentioned in
Return Value
A publisher that emits either the most-recent or first element received during the specified interval.
Discussion
Use throttle(for:scheduler:latest:) to selectively republish elements from an upstream publisher during an interval you specify. Other elements received from the upstream in the throttling interval aren’t republished.
In the example below, a Timer.TimerPublisher produces elements on one-second intervals; the throttle(for:scheduler:latest:) operator delivers the first event, then republishes only the latest event in the following ten second intervals:
cancellable = Timer.publish(every: 3.0, on: .main, in: .default)
.autoconnect()
.print("\(Date().description)")
.throttle(for: 10.0, scheduler: RunLoop.main, latest: true)
.sink(
receiveCompletion: { print ("Completion: \($0).") },
receiveValue: { print("Received Timestamp \($0).") }
)
// Prints:
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:26:57 +0000)
// Received Timestamp 2020-03-19 18:26:57 +0000.
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:27:00 +0000)
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:27:03 +0000)
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:27:06 +0000)
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:27:09 +0000)
// Received Timestamp 2020-03-19 18:27:09 +0000.