Skip to content

Commit

Permalink
fix: optimize performance when enter a lot of text quickly (#519)
Browse files Browse the repository at this point in the history
Co-authored-by: Tisfeng <[email protected]>
  • Loading branch information
phlpsong and tisfeng authored Apr 22, 2024
1 parent 9b6215f commit 8df27dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,26 @@ private class CustomOpenAIViewModel: ObservableObject {

init(service: CustomOpenAIService) {
self.service = service
cancellables.append(
Defaults.publisher(.customOpenAIModel, options: [])
.removeDuplicates()
.sink { _ in
self.modelChanged()
}
)
cancellables.append(
Defaults.publisher(.customOpenAINameKey, options: [])
.removeDuplicates()
.sink { _ in
self.serviceConfigChanged()
}
)
cancellables.append(
Defaults.publisher(.customOpenAIAvailableModels)
.removeDuplicates()
.sink { _ in
self.modelsTextChanged()
}
)
Defaults.publisher(.customOpenAIModel, options: [])
.removeDuplicates()
.sink { _ in
self.modelChanged()
}
.store(in: &cancellables)
Defaults.publisher(.customOpenAINameKey, options: [])
.removeDuplicates()
.throttle(for: 0.1, scheduler: DispatchQueue.main, latest: true)
.sink { _ in
self.serviceConfigChanged()
}
.store(in: &cancellables)
Defaults.publisher(.customOpenAIAvailableModels)
.removeDuplicates()
.throttle(for: 0.1, scheduler: DispatchQueue.main, latest: true)
.sink { _ in
self.modelsTextChanged()
}
.store(in: &cancellables)
}

// MARK: Internal
Expand All @@ -150,7 +149,7 @@ private class CustomOpenAIViewModel: ObservableObject {

// MARK: Private

private var cancellables: [AnyCancellable] = []
private var cancellables: Set<AnyCancellable> = []

private func modelChanged() {
if !validModels.contains(model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,19 @@ private class OpenAIServiceViewModel: ObservableObject {

init(service: OpenAIService) {
self.service = service
cancellables.append(
Defaults.publisher(.openAIModel, options: [])
.removeDuplicates()
.sink { _ in
self.modelChanged()
}
)
cancellables.append(
Defaults.publisher(.openAIAvailableModels)
.removeDuplicates()
.sink { _ in
self.modelsTextChanged()
}
)
Defaults.publisher(.openAIModel, options: [])
.removeDuplicates()
.sink { _ in
self.modelChanged()
}
.store(in: &cancellables)
Defaults.publisher(.openAIAvailableModels)
.removeDuplicates()
.throttle(for: 0.1, scheduler: DispatchQueue.main, latest: true)
.sink { _ in
self.modelsTextChanged()
}
.store(in: &cancellables)
}

// MARK: Internal
Expand All @@ -135,7 +134,7 @@ private class OpenAIServiceViewModel: ObservableObject {

// MARK: Private

private var cancellables: [AnyCancellable] = []
private var cancellables: Set<AnyCancellable> = []

private func modelChanged() {
// Currently, user of low os versions can change OpenAI model using URL scheme, like easydict://writeKeyValue?EZOpenAIModelKey=gpt-4
Expand Down

0 comments on commit 8df27dd

Please sign in to comment.