Skip to content

Commit 37267bb

Browse files
committed
fix: clear typing id after 1s
1 parent d07bc41 commit 37267bb

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

src/classes/Channel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export class Channel {
3737
readonly #collection: ChannelCollection;
3838
readonly id: string;
3939

40+
_typingTimers: Record<string, number> = {};
41+
4042
/**
4143
* Construct Channel
4244
* @param collection Collection

src/events/v1.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,24 @@ export async function handleEvent(
504504
if (channel) {
505505
if (!channel.typingIds.has(event.user)) {
506506
channel.typingIds.add(event.user);
507-
} else if (!client.channels.isPartial(event.id)) {
508-
return;
509-
}
510507

511-
client.emit(
512-
"channelStartTyping",
513-
channel,
514-
client.users.getOrPartial(event.user)!,
515-
);
508+
clearTimeout(channel._typingTimers[event.user]);
509+
channel._typingTimers[event.user] = setTimeout(
510+
() =>
511+
handleEvent(
512+
client,
513+
{ ...event, type: "ChannelStopTyping" },
514+
setReady,
515+
),
516+
1000,
517+
) as never;
518+
519+
client.emit(
520+
"channelStartTyping",
521+
channel,
522+
client.users.getOrPartial(event.user)!,
523+
);
524+
}
516525
}
517526
break;
518527
}
@@ -521,15 +530,16 @@ export async function handleEvent(
521530
if (channel) {
522531
if (channel.typingIds.has(event.user)) {
523532
channel.typingIds.delete(event.user);
524-
} else if (!client.channels.isPartial(event.id)) {
525-
return;
526-
}
527533

528-
client.emit(
529-
"channelStopTyping",
530-
channel,
531-
client.users.getOrPartial(event.user)!,
532-
);
534+
clearTimeout(channel._typingTimers[event.user]);
535+
delete channel._typingTimers[event.user];
536+
537+
client.emit(
538+
"channelStopTyping",
539+
channel,
540+
client.users.getOrPartial(event.user)!,
541+
);
542+
}
533543
}
534544
break;
535545
}

0 commit comments

Comments
 (0)