Skip to content

Throttle and debounce timing diagram maybe wrong #257

@okmyself

Description

@okmyself

My device is M1 Macbook Air. I tested it in Swift Playground, but the result does not match the timing diagram. I think the time of the result in the third row is drawn in the wrong place. All codes and results are as follows:

import Foundation import Combine let bounces:[(Int,TimeInterval)] = [ (1, 10), (2, 20), (3, 80), (4, 90), (5, 150), (6, 160) ] var startTime = Date().timeIntervalSince1970 let subject = PassthroughSubject<Int, Never>() var cancellable = subject .throttle(for: .seconds(50), scheduler: RunLoop.main, latest: true) .sink { index in let offset = Date().timeIntervalSince1970 - startTime print ("Received index \(index) at \(offset)") } for bounce in bounces { DispatchQueue.main.asyncAfter(deadline: .now() + bounce.1) { subject.send(bounce.0) } }

Received index 1 at 10. 297373294830322
Received index 2 at 60. 29852819442749
Received index 4 at 110. 29963707923889
Received index 6 at 164. 99856996536255

import Foundation import Combine let bounces:[(Int,TimeInterval)] = [ (1, 10), (2, 20), (3, 80), (4, 90), (5, 150), (6, 160) ] var startTime = Date().timeIntervalSince1970 let subject = PassthroughSubject<Int, Never>() var cancellable = subject .throttle(for: .seconds(50), scheduler: RunLoop.main, latest: false) .sink { index in let offset = Date().timeIntervalSince1970 - startTime print ("Received index \(index) at \(offset)") } for bounce in bounces { DispatchQueue.main.asyncAfter(deadline: .now() + bounce.1) { subject.send(bounce.0) } }

Received index 1 at 10. 309597969055176
Received index 2 at 60 .30997681617737
Received index 3 at 110. 31065487861633
Received index 5 at 164 .84435486793518

import Foundation import Combine let bounces:[(Int,TimeInterval)] = [ (1, 10), (2, 20), (3, 60), (4, 70), (5, 110), (6, 120) ] var startTime = Date().timeIntervalSince1970 let subject = PassthroughSubject<Int, Never>() var cancellable = subject .debounce(for: .seconds(50), scheduler: RunLoop.main) .sink { index in let offset = Date().timeIntervalSince1970 - startTime print ("Received index \(index) at \(offset)") } for bounce in bounces { DispatchQueue.main.asyncAfter(deadline: .now() + bounce.1) { subject.send(bounce.0) } }

Received index 6 at 171 .00314784049988

import Foundation import Combine let bounces:[(Int,TimeInterval)] = [ (1, 10), (2, 20), (5, 110), (6, 120) ] var startTime = Date().timeIntervalSince1970 let subject = PassthroughSubject<Int, Never>() var cancellable = subject .debounce(for: .seconds(50), scheduler: RunLoop.main) .sink { index in let offset = Date().timeIntervalSince1970 - startTime print ("Received index \(index) at \(offset)") } for bounce in bounces { DispatchQueue.main.asyncAfter(deadline: .now() + bounce.1) { subject.send(bounce.0) } }

Received index 2 at 71. 99511885643005
Received index 6 at 170 .99344301223755

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions