Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Proper Cleanup of Yorkie Initialization Process #210

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions frontend/src/hooks/useYorkieDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { selectAuth } from "../store/authSlice";
yorkie.setLogLevel(4);

export const useYorkieDocument = (
yorkieDocuentId?: string | null,
yorkieDocumentId?: string | null,
presenceName?: string | null
) => {
const [searchParams] = useSearchParams();
Expand All @@ -28,7 +28,8 @@ export const useYorkieDocument = (
}, [client, doc]);

useEffect(() => {
if (!yorkieDocuentId || !presenceName || doc || client) return;
let mounted = true;
if (!yorkieDocumentId || !presenceName || doc || client) return;

let yorkieToken = `default:${authStore.accessToken}`;

Expand All @@ -43,7 +44,10 @@ export const useYorkieDocument = (
});
await newClient.activate();

const newDoc = new yorkie.Document(yorkieDocuentId as string);
const newDoc = new yorkie.Document<
YorkieCodeMirrorDocType,
YorkieCodeMirrorPresenceType
>(yorkieDocumentId);

await newClient.attach(newDoc, {
initialPresence: {
Expand All @@ -53,13 +57,28 @@ export const useYorkieDocument = (
},
});

// Clean up if the component is unmounted before the initialization is done
if (!mounted) {
await newClient.detach(newDoc);
await newClient.deactivate();
return;
}

setClient(newClient);
setDoc(
newDoc as yorkie.Document<YorkieCodeMirrorDocType, YorkieCodeMirrorPresenceType>
);
setDoc(newDoc);
};
initializeYorkie();
}, [presenceName, yorkieDocuentId, doc, client, authStore.accessToken, searchParams]);

return { client, doc, cleanUpYorkieDocument };
return () => {
mounted = false;
};
}, [presenceName, yorkieDocumentId, doc, client, authStore.accessToken, searchParams]);

useEffect(() => {
return () => {
cleanUpYorkieDocument();
};
}, [cleanUpYorkieDocument]);

return { client, doc };
};
8 changes: 2 additions & 6 deletions frontend/src/pages/workspace/document/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ function DocumentIndex() {
const userStore = useSelector(selectUser);
const { data: workspace } = useGetWorkspaceQuery(params.workspaceSlug);
const { data: document } = useGetDocumentQuery(workspace?.id, params.documentId);
const { doc, client, cleanUpYorkieDocument } = useYorkieDocument(
document?.yorkieDocumentId,
userStore.data?.nickname
);
const { doc, client } = useYorkieDocument(document?.yorkieDocumentId, userStore.data?.nickname);

useEffect(() => {
if (!doc || !client) return;
Expand All @@ -28,11 +25,10 @@ function DocumentIndex() {
dispatch(setClient(client));

return () => {
cleanUpYorkieDocument();
dispatch(setDoc(null));
dispatch(setClient(null));
};
}, [cleanUpYorkieDocument, dispatch, client, doc]);
}, [dispatch, client, doc]);

return (
<Box height="calc(100% - 64px)">
Expand Down
8 changes: 2 additions & 6 deletions frontend/src/pages/workspace/document/share/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ function DocumentShareIndex() {
const [searchParams] = useSearchParams();
const shareToken = useMemo(() => searchParams.get("token"), [searchParams]);
const { data: sharedDocument } = useGetDocumentBySharingTokenQuery(shareToken);
const { doc, client, cleanUpYorkieDocument } = useYorkieDocument(
sharedDocument?.yorkieDocumentId,
"Anonymous"
);
const { doc, client } = useYorkieDocument(sharedDocument?.yorkieDocumentId, "Anonymous");

useEffect(() => {
if (!sharedDocument?.role) return;
Expand All @@ -35,11 +32,10 @@ function DocumentShareIndex() {
dispatch(setClient(client));

return () => {
cleanUpYorkieDocument();
dispatch(setDoc(null));
dispatch(setClient(null));
};
}, [cleanUpYorkieDocument, dispatch, client, doc]);
}, [dispatch, client, doc]);

if (!shareToken) return <Navigate to="/" state={{ from: location }} replace />;

Expand Down