Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttle and debounce timing diagram maybe wrong #257

Open
okmyself opened this issue Aug 9, 2022 · 0 comments
Open

Throttle and debounce timing diagram maybe wrong #257

okmyself opened this issue Aug 9, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@okmyself
Copy link

okmyself commented Aug 9, 2022

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

@okmyself okmyself added the enhancement New feature or request label Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant