Skip to content

Commit

Permalink
replace window to globalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
mebtte committed Nov 27, 2023
1 parent a83e107 commit ef33d7d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/use_timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ function useTimer(speed = 1) {
useEffect(() => {
if (!paused) {
let last = Date.now();
const timer = window.setInterval(() => {
const timer = globalThis.setInterval(() => {
const now = Date.now();
setCurrentMillisecond((cm) => cm + (now - last) * speed);
last = now;
}, 97);
return () => window.clearInterval(timer);
return () => globalThis.clearInterval(timer);
}
}, [paused, speed]);

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-lrc",
"version": "3.1.0",
"version": "3.1.1",
"description": "The react component to display lyric by lrc format.",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down Expand Up @@ -76,7 +76,7 @@
"scripts": {
"prepare": "husky install",
"build": "rimraf build && rollup --bundleConfigAsCjs --config rollup.config.js",
"prepublish": "npm run build",
"prepulishOnly": "npm run build",
"lint-staged": "lint-staged",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
Expand Down
10 changes: 5 additions & 5 deletions src/components/base_lrc/use_auto_scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
useRef,
useCallback,
useMemo,
KeyboardEvent,
type KeyboardEvent,
} from 'react';
import throttle from '../../utils/throttle';

Expand All @@ -26,11 +26,11 @@ export default ({
}) => {
const [autoScroll, setAutoScroll] = useState(true);

const timerRef = useRef<number>();
const timerRef = useRef<ReturnType<typeof globalThis.setTimeout>>();
const handleUserScroll = useCallback(() => {
window.clearTimeout(timerRef.current);
globalThis.clearTimeout(timerRef.current);
setAutoScroll(false);
timerRef.current = window.setTimeout(
timerRef.current = globalThis.setTimeout(
() => setAutoScroll(true),
recoverAutoScrollInterval,
);
Expand Down Expand Up @@ -72,7 +72,7 @@ export default ({
* clear timer after unmount
* @author mebtte<hi@mebtte.com>
*/
useEffect(() => () => window.clearTimeout(timerRef.current), []);
useEffect(() => () => globalThis.clearTimeout(timerRef.current), []);

return {
autoScroll,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ function debounce<F extends (...params: unknown[]) => unknown>(
f: F,
wait = 300,
) {
let timer: number;
let timer: ReturnType<typeof globalThis.setTimeout>;
return (...params: Parameters<F>) => {
window.clearTimeout(timer);
timer = window.setTimeout(() => f(params), wait);
globalThis.clearTimeout(timer);
timer = globalThis.setTimeout(() => f(params), wait);
};
}

Expand Down

0 comments on commit ef33d7d

Please sign in to comment.