Skip to content

Commit 6f51746

Browse files
committed
feat: 새로운 페이지 열 때, 이전 페이지 웹소켓 연결 끊기
1 parent 93fe456 commit 6f51746

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

apps/frontend/src/components/EditorView.tsx

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useMemo, useState } from "react";
1+
import { useEffect, useRef, useState } from "react";
22
import { EditorInstance } from "novel";
33
import { useDebouncedCallback } from "use-debounce";
44
import * as Y from "yjs";
@@ -16,15 +16,23 @@ export default function EditorView() {
1616
const { page, isLoading } = usePage(currentPage);
1717
const [saveStatus, setSaveStatus] = useState<"saved" | "unsaved">("saved");
1818

19-
const ydoc = useMemo(() => {
20-
return new Y.Doc();
21-
}, [currentPage]);
19+
const ydoc = useRef<Y.Doc>();
20+
const provider = useRef<SocketIOProvider>();
21+
22+
useEffect(() => {
23+
if (!currentPage) return;
24+
25+
if (provider.current) {
26+
provider.current.disconnect();
27+
}
28+
29+
const doc = new Y.Doc();
30+
ydoc.current = doc;
2231

23-
const provider = useMemo(() => {
24-
return new SocketIOProvider(
32+
const wsProvider = new SocketIOProvider(
2533
import.meta.env.VITE_WS_URL,
2634
`document-${currentPage}`,
27-
ydoc,
35+
doc,
2836
{
2937
autoConnect: true,
3038
disableBc: false,
@@ -35,32 +43,18 @@ export default function EditorView() {
3543
transports: ["websocket", "polling"],
3644
},
3745
);
46+
47+
provider.current = wsProvider;
48+
49+
return () => {
50+
wsProvider.disconnect();
51+
};
3852
}, [currentPage]);
3953

4054
const pageTitle = page?.title ?? "제목없음";
4155
const pageContent = page?.content ?? {};
4256

4357
const updatePageMutation = useUpdatePage();
44-
/* const optimisticUpdatePageMutation = useOptimisticUpdatePage({
45-
id: currentPage ?? 0,
46-
});
47-
48-
const handleTitleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
49-
if (currentPage === null) return;
50-
51-
setSaveStatus("unsaved");
52-
53-
optimisticUpdatePageMutation.mutate(
54-
{
55-
pageData: { title: e.target.value, content: pageContent },
56-
},
57-
{
58-
onSuccess: () => setSaveStatus("saved"),
59-
onError: () => setSaveStatus("unsaved"),
60-
},
61-
);
62-
}; */
63-
6458
const handleEditorUpdate = useDebouncedCallback(
6559
async ({ editor }: { editor: EditorInstance }) => {
6660
if (currentPage === null) {
@@ -85,7 +79,7 @@ export default function EditorView() {
8579
return null;
8680
}
8781

88-
if (!ydoc || !provider) return null;
82+
if (!ydoc.current || !provider.current) return null;
8983

9084
return (
9185
<EditorLayout>
@@ -96,11 +90,11 @@ export default function EditorView() {
9690
pageContent={pageContent}
9791
/>
9892
<Editor
99-
key={ydoc.guid}
93+
key={provider.current.doc.guid + currentPage}
10094
initialContent={pageContent}
10195
pageId={currentPage}
102-
ydoc={ydoc}
103-
provider={provider}
96+
ydoc={ydoc.current}
97+
provider={provider.current}
10498
onEditorUpdate={handleEditorUpdate}
10599
/>
106100
</EditorLayout>

0 commit comments

Comments
 (0)