mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-25 17:50:59 +00:00
Throttle search result display
This commit is contained in:
parent
9cbe7bc004
commit
a7ec52fcab
2 changed files with 11 additions and 4 deletions
|
@ -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> {
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue