Skip to content

Commit

Permalink
fix: Improvment on chat.active_chat event
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Aug 15, 2024
1 parent f33374e commit 8c5ae19
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/chat/events/eventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface ChatEventTypes {
* });
* ```
*/
'chat.active_chat': ChatModel;
'chat.active_chat': ChatModel | null;
/**
* Triggered when new message is received
*
Expand Down
13 changes: 8 additions & 5 deletions src/chat/events/registerActiveChatEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import * as webpack from '../../webpack';
import { ChatModel, ChatStore } from '../../whatsapp';

webpack.onInjected(() => register());
let emitTimeout: any = null;

function register() {
ChatStore.on('change:active', (chat: ChatModel, actve: boolean) => {
if (actve) {
queueMicrotask(() => {
internalEv.emit('chat.active_chat', chat);
});
ChatStore.on('change:active', (chat: ChatModel) => {
if (emitTimeout) {
clearTimeout(emitTimeout);
}

emitTimeout = setTimeout(() => {
internalEv.emit('chat.active_chat', chat.active ? chat : null);
}, 50);
});
}

0 comments on commit 8c5ae19

Please sign in to comment.