Skip to content

Commit 9bd09a3

Browse files
committed
Feat: yjs doc 데이터 같은 곳꺼를 사용하도록 수정
1 parent 549feb9 commit 9bd09a3

2 files changed

Lines changed: 15 additions & 38 deletions

File tree

src/collaboration/infrastructure/gateway/collaboration.gateway.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,10 @@ export class CollaborationGateway
9494
this.pendingUpdates.delete(client.id);
9595
for (const pending of buffered) {
9696
const updateBuffer = new Uint8Array(pending.update);
97-
let pendingDoc = await this.yjsDocumentService.loadDocument(pending.cardsetId);
98-
if (!pendingDoc) pendingDoc = new Y.Doc();
99-
Y.applyUpdate(pendingDoc, updateBuffer);
100-
await this.yjsDocumentService.saveUpdate(pending.cardsetId, updateBuffer);
101-
const newState = Y.encodeStateAsUpdate(pendingDoc);
97+
const finalState = await this.yjsDocumentService.saveUpdate(pending.cardsetId, updateBuffer);
10298
this.server.to(`cardset:${pending.cardsetId}`).emit('sync', {
10399
cardsetId: pending.cardsetId,
104-
update: newState,
100+
update: finalState,
105101
});
106102
this.logger.log(
107103
`[버퍼 처리 완료] cardsetId=${pending.cardsetId}, clientId=${client.id}`,
@@ -204,23 +200,12 @@ export class CollaborationGateway
204200
return;
205201
}
206202

207-
let doc = await this.yjsDocumentService.loadDocument(cardsetId);
208-
if (!doc) {
209-
doc = new Y.Doc();
210-
this.logger.log(
211-
`Created new Yjs document for cardset ${cardsetId} during update`,
212-
);
213-
}
214-
215203
const updateBuffer = new Uint8Array(update);
216-
Y.applyUpdate(doc, updateBuffer);
204+
const finalState = await this.yjsDocumentService.saveUpdate(cardsetId, updateBuffer);
217205

218-
await this.yjsDocumentService.saveUpdate(cardsetId, updateBuffer);
219-
220-
const state = Y.encodeStateAsUpdate(doc);
221206
this.server.to(`cardset:${cardsetId}`).emit('sync', {
222207
cardsetId,
223-
update: state,
208+
update: finalState,
224209
});
225210
this.logger.log(
226211
`Sync update from user ${user.userId} broadcasted to all clients in cardset ${cardsetId}`,

src/collaboration/infrastructure/redis/yjs-document.service.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,36 +89,28 @@ export class YjsDocumentService implements OnModuleInit, OnModuleDestroy {
8989
}
9090
}
9191

92-
async saveUpdate(cardsetId: string, update: Uint8Array): Promise<void> {
92+
async saveUpdate(cardsetId: string, update: Uint8Array): Promise<Uint8Array> {
9393
try {
94-
if (!this.redisClient) return;
94+
if (!this.redisClient) return update;
9595
const key = `yjs:cardset:${cardsetId}`;
9696
const historyKey = `yjs:cardset:${cardsetId}:updates`;
97-
const updateBuffer = Buffer.from(update);
9897

98+
const doc = new Y.Doc();
9999
const existingData = await this.redisClient.getBuffer(key);
100100
if (existingData) {
101-
const doc = new Y.Doc();
102101
Y.applyUpdate(doc, existingData);
103-
Y.applyUpdate(doc, update);
104-
const newState = Y.encodeStateAsUpdate(doc);
105-
this.logger.log(
106-
`[saveUpdate] Redis 저장 전 doc 내용 - cardsetId=${cardsetId}, cards=${JSON.stringify(doc.getArray('cards').toJSON())}`,
107-
);
108-
await this.redisClient.set(key, Buffer.from(newState));
109-
} else {
110-
const doc = new Y.Doc();
111-
Y.applyUpdate(doc, update);
112-
const state = Y.encodeStateAsUpdate(doc);
113-
this.logger.log(
114-
`[saveUpdate] Redis 저장 전 doc 내용 (신규) - cardsetId=${cardsetId}, cards=${JSON.stringify(doc.getArray('cards').toJSON())}`,
115-
);
116-
await this.redisClient.set(key, Buffer.from(state));
117102
}
103+
Y.applyUpdate(doc, update);
104+
const newState = Y.encodeStateAsUpdate(doc);
105+
this.logger.log(
106+
`[saveUpdate] Redis 저장 전 doc 내용 - cardsetId=${cardsetId}, cards=${JSON.stringify(doc.getArray('cards').toJSON())}`,
107+
);
108+
await this.redisClient.set(key, Buffer.from(newState));
118109
await this.redisClient.expire(key, 86400 * 7);
119-
await this.redisClient.rpush(historyKey, updateBuffer.toString('base64'));
110+
await this.redisClient.rpush(historyKey, Buffer.from(update).toString('base64'));
120111

121112
this.scheduleMySqlPersistence(cardsetId);
113+
return newState;
122114
} catch (error) {
123115
this.logger.error(
124116
`Failed to save update for cardset ${cardsetId}:`,

0 commit comments

Comments
 (0)