Skip to content

Commit

Permalink
修复输入卡顿的问题 (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy authored Sep 25, 2024
1 parent a28e4a9 commit 3e168ae
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void OnSystemBoxTextChanged(object sender, TextChangedEventArgs e)
}

ViewModel.Data.SystemInstruction = SystemBox.Text;
ViewModel.CalcTotalTokenCountCommand.Execute(default);
ViewModel.ResetLastInputTimeCommand.Execute(default);
_textChanged = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,18 @@ await Task.Run(() =>
});
});
}

[RelayCommand]
private void TryAutoCalcUserInputToken()
{
if (_lastInputTime is not null && DateTimeOffset.Now - _lastInputTime >= TimeSpan.FromSeconds(1))
{
_lastInputTime = default;
CalcTotalTokenCountCommand.Execute(default);
}
}

[RelayCommand]
private void ResetLastInputTime()
=> _lastInputTime = DateTimeOffset.Now;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public sealed partial class ChatSessionViewModel
private readonly IStorageService _storageService;
private readonly ILogger<ChatSessionViewModel> _logger;
private CancellationTokenSource _cancellationTokenSource;
private DateTimeOffset? _lastInputTime;

private int _baseTokenCount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ private void Initialize(ChatSession data)
/// 在进入视图开始显示时执行.
/// </summary>
[RelayCommand]
private async Task EnterViewAsync()
{
await CalcTotalTokenCountAsync();
}
private void EnterView()
=> CalcTotalTokenCountCommand.Execute(default);

[RelayCommand]
private void NewSession()
Expand Down Expand Up @@ -251,5 +249,5 @@ partial void OnModelChanged(string value)
=> CheckCurrentModelStatus();

partial void OnUserInputChanged(string value)
=> CalcUserInputTokenCountCommand.Execute(default);
=> _lastInputTime = DateTimeOffset.Now;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public sealed partial class ChatServicePageViewModel
private readonly ILogger<ChatServicePageViewModel> _logger;
private readonly ChatPresetModuleViewModel _chatPresetModuleVM;
private readonly GroupPresetModuleViewModel _groupPresetModuleVM;
private readonly DispatcherTimer? _tokenTimer;
private bool _isPluginInitialized;

[ObservableProperty]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,7 @@ private void ExitGroupChat()
CurrentGroup?.SaveSessionToDatabaseCommand.ExecuteAsync(default);
CurrentGroup = default;
}

private void OnTokenTimerTick(object? sender, object e)
=> CurrentSession?.TryAutoCalcUserInputTokenCommand.Execute(default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public ChatServicePageViewModel(
HistoryGroupSessions.CollectionChanged += OnHistorySessionsCountChanged;
Plugins.CollectionChanged += OnPluginsCountChanged;
CheckPluginsCount();

if (_tokenTimer is null)
{
_tokenTimer = new DispatcherTimer();
_tokenTimer.Interval = TimeSpan.FromMilliseconds(400);
_tokenTimer.Tick += OnTokenTimerTick;
_tokenTimer.Start();
}
}

/// <inheritdoc/>
Expand Down

0 comments on commit 3e168ae

Please sign in to comment.