Skip to content

Commit

Permalink
fix: streamline the process
Browse files Browse the repository at this point in the history
  • Loading branch information
Neumann615 committed Aug 24, 2024
1 parent 2ce104c commit ca1b25a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
35 changes: 12 additions & 23 deletions src/hooks/useTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import useEvent from 'rc-util/lib/hooks/useEvent';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
import { useMemo, useState } from 'react';
import type { TourStepInfo } from '..';
import { isInViewPort,debounce } from '../util';
import { isInViewPort } from '../util';

export interface Gap {
offset?: number | [number, number];
Expand Down Expand Up @@ -62,37 +62,26 @@ export default function useTarget(
}
});


let ticking = false;
const debounceUpdatePos = debounce(updatePos,1000/60*2);
let a = []
const scrollUpdatePos = window.requestAnimationFrame ? ()=>{
if (!ticking) {
window.requestAnimationFrame(() => {
debounceUpdatePos();
a.push(Date.now())
ticking = false;
});
ticking = true;
}
console.log(a)

}: debounce(updatePos,1000/60*3)



const getGapOffset = (index: number) =>
(Array.isArray(gap?.offset) ? gap?.offset[index] : gap?.offset) ?? 6;

const scrollEndUpdatePos = ()=>{
if(window.requestAnimationFrame){
window.requestAnimationFrame(updatePos);
return;
}
setTimeout(updatePos,1000/60);
}

useLayoutEffect(() => {
updatePos();
// update when window resize
window.addEventListener('resize', updatePos);
// update when window scroll stop
window.addEventListener('scroll',scrollUpdatePos)
// update when window scroll end
window.addEventListener('scrollend',scrollEndUpdatePos);
return () => {
window.removeEventListener('resize', updatePos);
window.removeEventListener('scroll', scrollUpdatePos);
window.removeEventListener('scrollend',scrollEndUpdatePos);
};
}, [targetElement, open, updatePos]);

Expand Down
13 changes: 0 additions & 13 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,3 @@ export function getPlacement(
stepPlacement ?? placement ?? (targetElement === null ? 'center' : 'bottom')
);
}

export function debounce<T extends (...args: any[]) => any>(func: T, wait: number): T {
let timeoutId: ReturnType<typeof setTimeout>;

const debouncedFunc = (...args: Parameters<T>): void => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func(...args);
}, wait);
};

return debouncedFunc as T;
}

0 comments on commit ca1b25a

Please sign in to comment.