-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add typing indicator utility (#25)
* feat: Add typing indicator utility * chore: Update version
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |