Skip to content

Commit

Permalink
feat: Add typing indicator utility (#25)
Browse files Browse the repository at this point in the history
* feat: Add typing indicator utility

* chore: Update version
  • Loading branch information
iamsivin authored Dec 20, 2023
1 parent 697b775 commit ae83040
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chatwoot/utils",
"version": "0.0.18",
"version": "0.0.19",
"description": "Chatwoot utils",
"private": false,
"license": "MIT",
Expand Down
44 changes: 44 additions & 0 deletions src/typingStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Creates a typing indicator utility.
* @param onStartTyping Callback function to be called when typing starts
* @param onStopTyping Callback function to be called when typing stops after delay
* @param idleTime Delay for idle time in ms before considering typing stopped
* @returns An object with start and stop methods for typing indicator
*/

type CallbackFunction = () => void;
type Timeout = ReturnType<typeof setTimeout>;

export const createTypingIndicator = (
onStartTyping: CallbackFunction,
onStopTyping: CallbackFunction,
idleTime: number
) => {
let timer: Timeout | null = null;

const start = (): void => {
if (!timer) {
onStartTyping();
}
reset();
};

const stop = (): void => {
if (timer) {
clearTimeout(timer as Timeout);
timer = null;
onStopTyping();
}
};

const reset = (): void => {
if (timer) {
clearTimeout(timer as Timeout);
}
timer = setTimeout(() => {
stop();
}, idleTime) as Timeout;
};

return { start, stop };
};

0 comments on commit ae83040

Please sign in to comment.