Skip to content

Commit

Permalink
Update debounce timing.
Browse files Browse the repository at this point in the history
  • Loading branch information
niw committed Sep 18, 2024
1 parent 236d777 commit 19f92e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Applications/Translator/Sources/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct MainView: View {
}
.toolbar {
ToolbarItemGroup {
if !translatorService.isAutoTranslationEnabled {
if !translatorService.isAutomaticTranslationEnabled {
Button {
Task {
do {
Expand All @@ -104,7 +104,7 @@ struct MainView: View {
.disabled(translatorService.inputString.isEmpty)
}

Toggle(isOn: translatorServiceBindable.isAutoTranslationEnabled) {
Toggle(isOn: translatorServiceBindable.isAutomaticTranslationEnabled) {
Label {
Text("Automatic")
} icon: {
Expand Down
25 changes: 13 additions & 12 deletions Sources/TranslatorSupport/TranslatorService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ private final class Box<T> {
@MainActor
@Observable
public final class TranslatorService {
public var isAutoTranslationEnabled: Bool = true {
public var isAutomaticTranslationEnabled: Bool = true {
didSet {
guard oldValue != isAutoTranslationEnabled else {
guard oldValue != isAutomaticTranslationEnabled else {
return
}
inputDidChange()
Expand Down Expand Up @@ -66,7 +66,7 @@ public final class TranslatorService {
}

private func inputDidChange() {
guard isAutoTranslationEnabled else {
guard isAutomaticTranslationEnabled else {
return
}

Expand Down Expand Up @@ -94,7 +94,7 @@ public final class TranslatorService {
}

public func translate() async throws {
guard !isAutoTranslationEnabled else {
guard !isAutomaticTranslationEnabled else {
return
}

Expand Down Expand Up @@ -129,6 +129,13 @@ public final class TranslatorService {
}
}

let trimmedInputString = inputString.trimmingCharacters(in: .whitespacesAndNewlines)

guard !trimmedInputString.isEmpty else {
translatedString = ""
return
}

if debounce {
try await Task.sleep(nanoseconds: 1.0.seconds)
}
Expand All @@ -137,20 +144,14 @@ public final class TranslatorService {
throw Error.failed(reason: "No model available.")
}

translatedString = ""

let trimmedInputString = inputString.trimmingCharacters(in: .whitespacesAndNewlines)

guard !trimmedInputString.isEmpty else {
return
}

let prompt = Translator.prompt(
mode: mode,
style: style,
input: trimmedInputString
)

translatedString = ""

for try await output in llamaModel.complete(prompt) {
translatedString.append(output)
}
Expand Down

0 comments on commit 19f92e5

Please sign in to comment.