Throttle search result display

This commit is contained in:
Justin Mazzocchi 2021-01-24 14:14:35 -08:00
parent 9cbe7bc004
commit a7ec52fcab
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
2 changed files with 11 additions and 4 deletions

View file

@ -55,13 +55,13 @@ public class CollectionItemsViewModel: ObservableObject {
.store(in: &cancellables) .store(in: &cancellables)
} }
} }
}
extension CollectionItemsViewModel: CollectionViewModel {
public var updates: AnyPublisher<CollectionUpdate, Never> { public var updates: AnyPublisher<CollectionUpdate, Never> {
$lastUpdate.eraseToAnyPublisher() $lastUpdate.eraseToAnyPublisher()
} }
}
extension CollectionItemsViewModel: CollectionViewModel {
public var title: AnyPublisher<String, Never> { collectionService.title } public var title: AnyPublisher<String, Never> { collectionService.title }
public var titleLocalizationComponents: AnyPublisher<[String], Never> { public var titleLocalizationComponents: AnyPublisher<[String], Never> {

View file

@ -15,13 +15,20 @@ public final class SearchViewModel: CollectionItemsViewModel {
super.init(collectionService: searchService, identification: identification) super.init(collectionService: searchService, identification: identification)
$query.throttle(for: .seconds(Self.queryThrottleInterval), scheduler: DispatchQueue.global(), latest: true) $query.throttle(for: .seconds(Self.throttleInterval), scheduler: DispatchQueue.global(), latest: true)
.sink { [weak self] in self?.request(maxId: nil, minId: nil, search: .init(query: $0, limit: Self.limit)) } .sink { [weak self] in self?.request(maxId: nil, minId: nil, search: .init(query: $0, limit: Self.limit)) }
.store(in: &cancellables) .store(in: &cancellables)
} }
public override var updates: AnyPublisher<CollectionUpdate, Never> {
// Since results are processed through the DB to determine CW expansion state etc they can arrive erratically
super.updates
.throttle(for: .seconds(Self.throttleInterval), scheduler: DispatchQueue.global(), latest: true)
.eraseToAnyPublisher()
}
} }
private extension SearchViewModel { private extension SearchViewModel {
static let queryThrottleInterval: TimeInterval = 0.5 static let throttleInterval: TimeInterval = 0.5
static let limit = 5 static let limit = 5
} }