In my current project, I am in the process of adding a feature that shows when a user is typing in a multi-user chat room. Despite my limited understanding of RXJS, I managed to come up with the code snippet below which satisfies the basic requirements for the "User is Typing" feature:
this.messageForm.message.valueChanges
.pipe(
tap(() => this.convoService.isUserTyping(true)),
debounceTime(500),
map(() => this.convoService.isUserTyping(false))
)
.subscribe();
However, this implementation is not efficient as it sends too many requests to the server. My objective is to restrict requests to only 2, and utilize RXJS operators to achieve the following specific behaviors:
- Send a request on the initial keypress
- Disregard any subsequent keypresses
- Wait for a set duration after the last keypress
- Restart the timer if a new keypress occurs before the timer ends
- Send a request once the timer expires