Skip to content

Commit 33ad662

Browse files
committed
refctor: event listener 분리
1 parent 3762ca6 commit 33ad662

File tree

1 file changed

+83
-72
lines changed

1 file changed

+83
-72
lines changed

apps/backend/src/yjs/yjs.service.ts

Lines changed: 83 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,8 @@ export class YjsService
122122

123123
// 페이지 내용 변경 사항을 감지해서 데이터베이스에 갱신합니다.
124124
editorDoc.observeDeep(() => {
125-
const document = editorDoc.doc as CustomDoc;
126-
const pageId = parseInt(document.name.split('-')[1]);
127-
128-
this.redisService.setField(
129-
`page:${pageId.toString()}`,
130-
'content',
131-
JSON.stringify(yXmlFragmentToProsemirrorJSON(editorDoc)),
132-
);
125+
this.observeEditor(editorDoc);
133126
});
134-
return;
135127
}
136128

137129
handleConnection() {
@@ -142,7 +134,6 @@ export class YjsService
142134
this.logger.log('접속 해제');
143135
}
144136

145-
146137
/**
147138
* initialize 관련 메소드
148139
*/
@@ -160,70 +151,19 @@ export class YjsService
160151
this.initializeYEdgeMap(edges, edgesMap);
161152

162153
// title의 변경 사항을 감지한다.
163-
title.observeDeep(async (event) => {
164-
// path가 존재할 때만 페이지 갱신
165-
166-
event[0].path.toString().split('_')[1] &&
167-
this.redisService.setField(
168-
`page:${event[0].path.toString().split('_')[1]}`,
169-
'title',
170-
event[0].target.toString(),
171-
);
172-
});
173-
emoji.observeDeep((event) => {
174-
// path가 존재할 때만 페이지 갱신
175-
event[0].path.toString().split('_')[1] &&
176-
this.pageService.updatePage(
177-
parseInt(event[0].path.toString().split('_')[1]),
178-
{
179-
emoji: event[0].target.toString(),
180-
},
181-
);
182-
});
183-
// node의 변경 사항을 감지한다.
184-
nodesMap.observe(async (event) => {
185-
for (const [key, change] of event.changes.keys) {
186-
if (change.action === 'update') {
187-
const node: any = nodesMap.get(key);
188-
if (node.type !== 'note') {
189-
continue;
190-
}
154+
title.observeDeep(this.observeTitle);
191155

192-
// node.data는 페이지에 대한 정보
193-
const { title, id } = node.data;
194-
const { x, y } = node.position;
195-
const isHolding = node.isHolding;
196-
if (!isHolding) {
197-
// TODO : node의 경우 key 값을 page id가 아닌 node id로 변경
198-
const findPage = await this.pageService.findPageById(id);
199-
await this.nodeService.updateNode(findPage.node.id, {
200-
title,
201-
x,
202-
y,
203-
});
204-
}
205-
}
206-
}
156+
// emoji의 변경 사항을 감지한다.
157+
emoji.observeDeep(this.observeEmoji);
158+
159+
// node의 변경 사항을 감지한다.
160+
nodesMap.observe((event) => {
161+
this.observeNodeMap(event, nodesMap);
207162
});
208163

209164
// edge의 변경 사항을 감지한다.
210165
edgesMap.observe(async (event) => {
211-
for (const [key, change] of event.changes.keys) {
212-
if (change.action === 'add') {
213-
const edge = edgesMap.get(key) as YMapEdge;
214-
const findEdge = await this.edgeService.findEdgeByFromNodeAndToNode(
215-
parseInt(edge.source),
216-
parseInt(edge.target),
217-
);
218-
// 연결된 노드가 없을 때만 edge 생성
219-
if (!findEdge) {
220-
await this.edgeService.createEdge({
221-
fromNode: parseInt(edge.source),
222-
toNode: parseInt(edge.target),
223-
});
224-
}
225-
}
226-
}
166+
this.observeEdgeMap(event, edgesMap);
227167
});
228168
}
229169
// YMap에 노드 정보를 넣어준다.
@@ -293,15 +233,86 @@ export class YjsService
293233
/**
294234
* event listener 관련
295235
*/
296-
private observeTitle(){
236+
private async observeTitle(event: Y.YEvent<any>[]) {
237+
// path가 존재할 때만 페이지 갱신
297238

239+
event[0].path.toString().split('_')[1] &&
240+
this.redisService.setField(
241+
`page:${event[0].path.toString().split('_')[1]}`,
242+
'title',
243+
event[0].target.toString(),
244+
);
298245
}
299246

300-
private observeEmoji(){
247+
private async observeEmoji(event: Y.YEvent<any>[]) {
248+
// path가 존재할 때만 페이지 갱신
249+
event[0].path.toString().split('_')[1] &&
250+
this.pageService.updatePage(
251+
parseInt(event[0].path.toString().split('_')[1]),
252+
{
253+
emoji: event[0].target.toString(),
254+
},
255+
);
256+
}
257+
private async observeNodeMap(
258+
event: Y.YMapEvent<unknown>,
259+
nodesMap: Y.Map<unknown>,
260+
) {
261+
for (const [key, change] of event.changes.keys) {
262+
if (change.action === 'update') {
263+
const node: any = nodesMap.get(key);
264+
if (node.type !== 'note') {
265+
continue;
266+
}
301267

268+
// node.data는 페이지에 대한 정보
269+
const { title, id } = node.data;
270+
const { x, y } = node.position;
271+
const isHolding = node.isHolding;
272+
if (!isHolding) {
273+
// TODO : node의 경우 key 값을 page id가 아닌 node id로 변경
274+
const findPage = await this.pageService.findPageById(id);
275+
await this.nodeService.updateNode(findPage.node.id, {
276+
title,
277+
x,
278+
y,
279+
});
280+
}
281+
}
282+
}
283+
}
284+
private async observeEdgeMap(
285+
event: Y.YMapEvent<unknown>,
286+
edgesMap: Y.Map<unknown>,
287+
) {
288+
for (const [key, change] of event.changes.keys) {
289+
if (change.action === 'add') {
290+
const edge = edgesMap.get(key) as YMapEdge;
291+
const findEdge = await this.edgeService.findEdgeByFromNodeAndToNode(
292+
parseInt(edge.source),
293+
parseInt(edge.target),
294+
);
295+
// 연결된 노드가 없을 때만 edge 생성
296+
if (!findEdge) {
297+
await this.edgeService.createEdge({
298+
fromNode: parseInt(edge.source),
299+
toNode: parseInt(edge.target),
300+
});
301+
}
302+
}
303+
}
302304
}
303-
private observeContent(){
304305

306+
private async observeEditor(editorDoc: Y.XmlFragment) {
307+
const document = editorDoc.doc as CustomDoc;
308+
const pageId = parseInt(document.name.split('-')[1]);
309+
310+
this.redisService.setField(
311+
`page:${pageId.toString()}`,
312+
'content',
313+
JSON.stringify(yXmlFragmentToProsemirrorJSON(editorDoc)),
314+
);
315+
return;
305316
}
306317

307318
// editor에서 paragraph 내부 text 노드의 text 값의 빈 문자열을 제거한다.

0 commit comments

Comments
 (0)