Skip to content

Commit

Permalink
Ensure Proper Cleanup of Yorkie Initialization Process (#210)
Browse files Browse the repository at this point in the history
* Clean up yorkie client if component unmounts before initialization is done

* Fix typo
  • Loading branch information
chacha912 authored Jun 24, 2024
1 parent 1c72c20 commit 3d58f83
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
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

0 comments on commit 3d58f83

Please sign in to comment.