Skip to content
This repository was archived by the owner on Mar 16, 2020. It is now read-only.
This repository was archived by the owner on Mar 16, 2020. It is now read-only.

Debounce doesn't deliver last call to update #58

Description

@yood

Debounce seems to deliver the first skipped call to update, instead of the last call to update.

I think this happens because the skipped calls to update are scheduled to be evaluated again 1) without previous skipped calls being cancelled, and 2) are scheduled to be evaluated again very close to each other temporally. Observable.delay uses DispatchQueue.asyncAfter which isn't very precise, so whichever one fires first (usually the first one scheduled in my search-as-you-type use case) will set the new lastCalled value and ignore the others.

// skip result if there was a newer result
if currentTime.compare(lastCalled!) == .orderedDescending {
    let s = Observable<T>()
    s.delay(seconds - timeSinceLastCall!).subscribe(updateIfNeeded(observable))
    s.update(value)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions