testcase:
func testDebounce() {
let exp = self.expectation(description: "Task")
Task {
var runCount = 0
for i in 0..<10 {
debounce(option: .runFirst) {
print(Date().printFormat(), "debounce test: (i)")
runCount += 1
}
try? await Task.sleep(for: .milliseconds(600))
}
// exp.fulfill()
}
self.wait(for: [exp], timeout: 10)
}
result:
2024-04-28 09:49:41.257 debounce test: 0
2024-04-28 09:49:41.879 debounce test: 1
2024-04-28 09:49:42.289 debounce test: 0
2024-04-28 01:49:42 +0000 task is cancelled
2024-04-28 01:49:43 +0000 task is cancelled
2024-04-28 01:49:43 +0000 task is cancelled
2024-04-28 01:49:44 +0000 task is cancelled
2024-04-28 01:49:44 +0000 task is cancelled
2024-04-28 01:49:45 +0000 task is cancelled
2024-04-28 01:49:46 +0000 task is cancelled
2024-04-28 01:49:46 +0000 task is cancelled
2024-04-28 09:49:47.855 debounce test: 9
two problem:
- first task run two times
- task delay more than 1 second(
duration)
the debounce expect in duration only trigger only once, but now, it delay much more time than duration
testcase:
func testDebounce() {
let exp = self.expectation(description: "Task")
Task {
var runCount = 0
for i in 0..<10 {
debounce(option: .runFirst) {
print(Date().printFormat(), "debounce test: (i)")
runCount += 1
}
try? await Task.sleep(for: .milliseconds(600))
}
// exp.fulfill()
}
self.wait(for: [exp], timeout: 10)
}
result:
2024-04-28 09:49:41.257 debounce test: 0
2024-04-28 09:49:41.879 debounce test: 1
2024-04-28 09:49:42.289 debounce test: 0
2024-04-28 01:49:42 +0000 task is cancelled
2024-04-28 01:49:43 +0000 task is cancelled
2024-04-28 01:49:43 +0000 task is cancelled
2024-04-28 01:49:44 +0000 task is cancelled
2024-04-28 01:49:44 +0000 task is cancelled
2024-04-28 01:49:45 +0000 task is cancelled
2024-04-28 01:49:46 +0000 task is cancelled
2024-04-28 01:49:46 +0000 task is cancelled
2024-04-28 09:49:47.855 debounce test: 9
two problem:
duration)the debounce expect in
durationonly trigger only once, but now, it delay much more time thanduration