Skip to content

Commit

Permalink
improved existing function to be more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed Oct 23, 2023
1 parent bc7b506 commit 3ee2e51
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
19 changes: 13 additions & 6 deletions ElementX/Sources/Other/Extensions/Publisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@ extension Publisher where Self.Failure == Never {
}
}

extension Publisher where Output == String, Failure == Never {
/// Debounce text queries and remove duplicates.
/// Clearing the text publishes the update immediately.
func debounceAndRemoveDuplicates() -> AnyPublisher<String, Never> {
extension Publisher where Output: Equatable, Failure == Never {
func debounceAndRemoveDuplicates<S: Scheduler>(on scheduler: S, delay: @escaping (Output) -> S.SchedulerTimeType.Stride) -> AnyPublisher<Output, Never> {
map { query in
let milliseconds = query.isEmpty ? 0 : 250
return Just(query).delay(for: .milliseconds(milliseconds), scheduler: DispatchQueue.main)
Just(query).delay(for: delay(query), scheduler: scheduler)
}
.switchToLatest()
.removeDuplicates()
.eraseToAnyPublisher()
}
}

extension Publisher where Output == String, Failure == Never {
/// Debounce text queries and remove duplicates.
/// Clearing the text publishes the update immediately.
func debounceTextQueriesAndRemoveDuplicates() -> AnyPublisher<String, Never> {
debounceAndRemoveDuplicates(on: DispatchQueue.main) { query in
query.isEmpty ? .milliseconds(0) : .milliseconds(250)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,9 @@ final class CompletionSuggestionService: CompletionSuggestionServiceProtocol {
}
}
// We only debounce if the suggestion is nil
.map { [weak self] value in
let delay = self?.suggestionTriggerSubject.value == nil ? 0.0 : 0.5
return Just(value).delay(for: .seconds(delay), scheduler: DispatchQueue.main)
.debounceAndRemoveDuplicates(on: DispatchQueue.main) { [weak self] _ in
self?.suggestionTriggerSubject.value != nil ? .milliseconds(500) : .milliseconds(0)
}
.switchToLatest()
.removeDuplicates()
.eraseToAnyPublisher()

Task {
switch await roomProxy.canUserTriggerRoomNotification(userID: roomProxy.ownUserID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class InviteUsersScreenViewModel: InviteUsersScreenViewModelType, InviteUsersScr
private func setupSubscriptions(selectedUsers: CurrentValuePublisher<[UserProfileProxy], Never>) {
context.$viewState
.map(\.bindings.searchQuery)
.debounceAndRemoveDuplicates()
.debounceTextQueriesAndRemoveDuplicates()
.sink { [weak self] _ in
self?.fetchUsers()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class StartChatScreenViewModel: StartChatScreenViewModelType, StartChatScreenVie
private func setupBindings() {
context.$viewState
.map(\.bindings.searchQuery)
.debounceAndRemoveDuplicates()
.debounceTextQueriesAndRemoveDuplicates()
.sink { [weak self] _ in
self?.fetchUsers()
}
Expand Down

0 comments on commit 3ee2e51

Please sign in to comment.