Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debouncing resize event #566

13 changes: 11 additions & 2 deletions packages/clarity-js/src/interaction/resize.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Event } from "@clarity-types/data";
import { ResizeData } from "@clarity-types/interaction";
import { ResizeData, Setting } from "@clarity-types/interaction";
import { clearTimeout, setTimeout } from "@src/core/timeout";
import { bind } from "@src/core/event";
import encode from "./encode";
import { schedule } from "@src/core/task";

export let data: ResizeData;
let timeout: number = null;

export function start(): void {
bind(window, "resize", recompute);
Expand All @@ -18,7 +21,12 @@ function recompute(): void {
width: de && "clientWidth" in de ? Math.min(de.clientWidth, window.innerWidth) : window.innerWidth,
height: de && "clientHeight" in de ? Math.min(de.clientHeight, window.innerHeight) : window.innerHeight,
};
encode(Event.Resize);
clearTimeout(timeout);
timeout = setTimeout(process, Setting.LookAhead, Event.Resize);
}

function process(event: Event): void {
schedule(encode.bind(this, event));
}

export function reset(): void {
Expand All @@ -27,4 +35,5 @@ export function reset(): void {

export function stop(): void {
reset();
clearTimeout(timeout);
AbdelrhmanMagdy marked this conversation as resolved.
Show resolved Hide resolved
}