Skip to content

Commit

Permalink
Add comment for cleanup codes
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Nov 22, 2024
1 parent 2c617e5 commit 708408a
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions frontend/src/hooks/useYorkieDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Color from "color";
import randomColor from "randomcolor";
import { useCallback, useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { useSearchParams } from "react-router-dom";
import { useBeforeUnload, useSearchParams } from "react-router-dom";
import * as yorkie from "yorkie-js-sdk";
import { selectAuth } from "../store/authSlice";
import { CodePairDocType } from "../store/editorSlice";
Expand Down Expand Up @@ -129,23 +129,17 @@ export const useYorkieDocument = (
createYorkieDocument,
]);

// Clean up yorkie document on unmount
// For example, when the user navigates to a different page
useEffect(() => {
return () => {
cleanUpYorkieDocument();
};
}, [cleanUpYorkieDocument]);

useEffect(() => {
const handleBeforeUnload = () => {
cleanUpYorkieDocument();
};

window.addEventListener("beforeunload", handleBeforeUnload);

return () => {
window.removeEventListener("beforeunload", handleBeforeUnload);
};
}, [cleanUpYorkieDocument]);
// Clean up yorkie document on beforeunload
// For example, when the user closes the tab or refreshes the page
useBeforeUnload(cleanUpYorkieDocument);

return { client, doc };
};

0 comments on commit 708408a

Please sign in to comment.