Skip to content

Commit

Permalink
fix: avoid memory leak by detaching up events
Browse files Browse the repository at this point in the history
  • Loading branch information
Orphoros committed Feb 24, 2024
1 parent 2bfea80 commit 966ada2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ function App() {
window.addEventListener('contextmenu', e => {
e.preventDefault();
}, false);

return () => {
window.removeEventListener('contextmenu', e => {
e.preventDefault();
}, false);
};
};

const correctIpcTime = () => {
Expand Down
7 changes: 1 addition & 6 deletions src/components/PreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ export default function PreviewCard() {
init().catch(console.error);
}, []);
useEffect(() => {
const id = setInterval(() => {
const today = new Date();
changeTime(today);
}, 1000);
const id = setInterval(checkTime, 1000);
return () => {
clearInterval(id);
};
Expand Down Expand Up @@ -70,8 +67,6 @@ export default function PreviewCard() {
changeTime(time);
}

setInterval(checkTime, 1000);

return (
<Card className={`w-[380px] h-[300px] ${showVibrancy ? 'bg-content1 bg-opacity-50' : 'bg-content2'} rounded-2xl`} shadow='none'>
<CardHeader>
Expand Down
4 changes: 3 additions & 1 deletion src/provider/AppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function AppProvider({children}: {children: React.ReactNode}) {
const [platformName, setPlatformName] = useState<OsType | undefined>(undefined);
useLayoutEffect(() => {
const init = async () => {
await attachConsole();
const detachConsole = await attachConsole();

void debug('initializing app...');

Expand All @@ -28,6 +28,8 @@ export default function AppProvider({children}: {children: React.ReactNode}) {
html.style.removeProperty('background');
}
}

detachConsole();
};

init().catch(error);
Expand Down

0 comments on commit 966ada2

Please sign in to comment.