Skip to content

Commit

Permalink
fix(carousel): fix scrollend polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
alenaksu authored Nov 29, 2023
1 parent facb550 commit 78b2a9c
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/internal/scrollend-polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ type GenericCallback = (this: unknown, ...args: unknown[]) => unknown;

type MethodOf<T, K extends keyof T> = T[K] extends GenericCallback ? T[K] : never;

const debounce = <T extends GenericCallback>(fn: T, delay: number) => {
let timerId = 0;

return function (this: unknown, ...args: unknown[]) {
window.clearTimeout(timerId);
timerId = window.setTimeout(() => {
fn.call(this, ...args);
}, delay);
};
};

const decorate = <T, M extends keyof T>(
proto: T,
method: M,
Expand Down Expand Up @@ -43,18 +32,24 @@ if (!isSupported) {
document.addEventListener('pointerdown', handlePointerDown);
document.addEventListener('pointerup', handlePointerUp);

// If the pointer is used for scrolling, the browser fires a pointercancel event because it determines
// that there are unlikely to be any more pointer events.
document.addEventListener('pointercancel', handlePointerUp);

decorate(EventTarget.prototype, 'addEventListener', function (this: EventTarget, addEventListener, type) {
if (type !== 'scroll') return;

const handleScrollEnd = debounce(() => {
const handleScrollEnd = () => {
if (!pointers.size) {
// If no pointer is active in the scroll area then the scroll has ended
this.dispatchEvent(new Event('scrollend'));
} else {
// otherwise let's wait a bit more
handleScrollEnd();
setTimeout(() => {
handleScrollEnd();
}, 100);
}
}, 100);
};

addEventListener.call(this, 'scroll', handleScrollEnd, { passive: true });
scrollHandlers.set(this, handleScrollEnd);
Expand Down

0 comments on commit 78b2a9c

Please sign in to comment.