mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
fix(channels): stop stale typing loops on overwrite (#1392)
Co-authored-by: XYSK-lilong007 <267018309+XYSK-lilong007@users.noreply.github.com>
This commit is contained in:
@@ -127,7 +127,12 @@ func (m *Manager) SendPlaceholder(ctx context.Context, channel, chatID string) b
|
||||
// Implements PlaceholderRecorder.
|
||||
func (m *Manager) RecordTypingStop(channel, chatID string, stop func()) {
|
||||
key := channel + ":" + chatID
|
||||
m.typingStops.Store(key, typingEntry{stop: stop, createdAt: time.Now()})
|
||||
entry := typingEntry{stop: stop, createdAt: time.Now()}
|
||||
if previous, loaded := m.typingStops.Swap(key, entry); loaded {
|
||||
if oldEntry, ok := previous.(typingEntry); ok && oldEntry.stop != nil {
|
||||
oldEntry.stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RecordReactionUndo registers a reaction undo function for later invocation.
|
||||
|
||||
@@ -616,6 +616,37 @@ func TestRecordTypingStop_ConcurrentSafe(t *testing.T) {
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestRecordTypingStop_ReplacesExistingStop(t *testing.T) {
|
||||
m := newTestManager()
|
||||
var oldStopCalls int
|
||||
var newStopCalls int
|
||||
|
||||
m.RecordTypingStop("test", "123", func() {
|
||||
oldStopCalls++
|
||||
})
|
||||
|
||||
m.RecordTypingStop("test", "123", func() {
|
||||
newStopCalls++
|
||||
})
|
||||
|
||||
if oldStopCalls != 1 {
|
||||
t.Fatalf("expected previous typing stop to be called once when replaced, got %d", oldStopCalls)
|
||||
}
|
||||
if newStopCalls != 0 {
|
||||
t.Fatalf("expected replacement typing stop to stay active until preSend, got %d calls", newStopCalls)
|
||||
}
|
||||
|
||||
msg := bus.OutboundMessage{Channel: "test", ChatID: "123", Content: "hello"}
|
||||
m.preSend(context.Background(), "test", msg, &mockChannel{})
|
||||
|
||||
if newStopCalls != 1 {
|
||||
t.Fatalf("expected replacement typing stop to be called by preSend, got %d", newStopCalls)
|
||||
}
|
||||
if oldStopCalls != 1 {
|
||||
t.Fatalf("expected previous typing stop to not be called again, got %d", oldStopCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendWithRetry_PreSendEditsPlaceholder(t *testing.T) {
|
||||
m := newTestManager()
|
||||
var sendCalled bool
|
||||
|
||||
Reference in New Issue
Block a user