@@ -10,6 +10,7 @@ import { Server } from 'socket.io';
1010import { YSocketIO } from 'y-socket.io/dist/server' ;
1111import * as Y from 'yjs' ;
1212import { NodeService } from '../node/node.service' ;
13+ import { NodeCacheService } from 'src/node-cache/node-cache.service' ;
1314
1415@WebSocketGateway ( 1234 )
1516export class YjsService
@@ -18,7 +19,10 @@ export class YjsService
1819 private logger = new Logger ( 'YjsGateway' ) ;
1920 private ysocketio : YSocketIO ;
2021
21- constructor ( private readonly nodeService : NodeService ) { }
22+ constructor (
23+ private readonly nodeService : NodeService ,
24+ private readonly nodeCacheService : NodeCacheService ,
25+ ) { }
2226 @WebSocketServer ( )
2327 server : Server ;
2428
@@ -41,15 +45,28 @@ export class YjsService
4145
4246 this . ysocketio . on ( 'document-loaded' , ( doc : Y . Doc ) => {
4347 doc . on ( 'update' , ( update ) => {
44- // console.log(Y.decodeUpdate(update).structs);
45- // console.log(doc.share.get('default'));
4648 const nodes = Object . values ( doc . getMap ( 'nodes' ) . toJSON ( ) ) ;
47- console . log ( nodes ) ;
49+
50+ // 모든 노드에 대해 검사한다.
4851 nodes . forEach ( ( node ) => {
4952 const { title, id } = node . data ;
5053 const { x, y } = node . position ;
5154 console . log ( title , id , x , y ) ;
52- this . nodeService . updateNode ( id , { title, x, y } ) ;
55+ // 만약 캐쉬에 노드가 존재하지 않다면 갱신 후 캐쉬에 노드를 넣는다.
56+ if ( ! this . nodeCacheService . has ( id ) ) {
57+ console . log ( id ) ;
58+ this . nodeService . updateNode ( id , { title, x, y } ) ;
59+ this . nodeCacheService . set ( id , { title, x, y } ) ;
60+ return ;
61+ }
62+
63+ // 만약 캐쉬에 노드가 존재하고 title이 다르다면 갱신한다.
64+ if ( ! this . nodeCacheService . hasSameTitle ( id , title ) ) {
65+ this . nodeService . updateNode ( id , { title, x, y } ) ;
66+ this . nodeCacheService . set ( id , { title, x, y } ) ;
67+ return ;
68+ }
69+ // 만약 캐쉬에 노드가 존재하고 title이 동일하다면 패스한다.
5370 } ) ;
5471 } ) ;
5572 this . logger . log ( `Document loaded: ${ doc . guid } ` ) ;
0 commit comments