Skip to content

Commit

Permalink
Merge branch 'Feature/#156_server에_Page별_EditorCRDT_적용' of https://gi…
Browse files Browse the repository at this point in the history
…thub.com/boostcampwm-2024/web33-Nocta into Feature/#156_server에_Page별_EditorCRDT_적용
  • Loading branch information
hyonun321 committed Nov 19, 2024
2 parents 359d346 + eba25a7 commit c6d7dfd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
5 changes: 3 additions & 2 deletions server/src/crdt/crdt.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Module } from "@nestjs/common";
import { workSpaceService } from "./crdt.service";
import { MongooseModule } from "@nestjs/mongoose";
import { Workspace, WorkspaceSchema } from "./schemas/workspace.schema";

import { CrdtGateway } from "./crdt.gateway";

@Module({
imports: [MongooseModule.forFeature([{ name: Workspace.name, schema: WorkspaceSchema }])],
providers: [workSpaceService, CrdtGateway],
exports: [workSpaceService],
providers: [CrdtService, CrdtGateway],
exports: [CrdtService],
})
export class CrdtModule {}
42 changes: 31 additions & 11 deletions server/src/crdt/crdt.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Injectable, OnModuleInit } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { Workspace, WorkspaceDocument } from "./schemas/workspace.schema";
import { WorkSpace as CRDTWorkSpace } from "@noctaCrdt/WorkSpace";
import { Block,Char } from "@noctaCrdt/Node";
import { BlockId,CharId } from "@noctaCrdt/NodeId";
import { Page as CRDTPage } from "@noctaCrdt/Page";
import { Workspace as CRDTWorkSpace, WorkspaceDocument } from "./schemas/workspace.schema";
import { Model } from "mongoose";
import { EditorCRDT, BlockCRDT } from "@noctaCrdt/Crdt";
import { BlockId, CharId } from "@noctaCrdt/NodeId";
import { Block, Char } from "@noctaCrdt/Node";

import { EditorCRDT,BlockCRDT } from "@noctaCrdt/Crdt";
import { BlockLinkedList, TextLinkedList } from "@noctaCrdt/LinkedList";
import { Page as CRDTPage } from "@noctaCrdt/Page";

@Injectable()
export class workSpaceService implements OnModuleInit {
Expand All @@ -17,8 +15,8 @@ export class workSpaceService implements OnModuleInit {
constructor(@InjectModel(Workspace.name) private workspaceModel: Model<WorkspaceDocument>) {
this.tempPage = new CRDTPage();
this.workspace = new CRDTWorkSpace("test", [this.tempPage]);
}


async onModuleInit() {
try {
// MongoDB에서 Workspace 문서 가져오기
Expand All @@ -33,10 +31,31 @@ export class workSpaceService implements OnModuleInit {
const page = await this.deserializePage(pageData);
this.workspace.pageList.push(page);
}

}
// 3. AuthUser Map 복원
if (doc.authUser) {
this.workspace.authUser = new Map(Array.from(doc.authUser.entries()));


this.crdt.LinkedList.nodeMap = {};
for (const [key, node] of Object.entries(doc.crdt.LinkedList.nodeMap)) {
const reconstructedNode = new Char(
node.value,
new CharId(node.id.clock, node.id.client),
);

if (node.next) {
reconstructedNode.next = new CharId(node.next.clock, node.next.client);
}
if (node.prev) {
reconstructedNode.prev = new CharId(node.prev.clock, node.prev.client);
}

this.crdt.LinkedList.nodeMap[??].crdt.LinkedList.nodeMap[key] = reconstructedNode;
}
} catch (e) {
console.error("Error reconstructing CRDT:", e);
}
}
} catch (error) {
Expand Down Expand Up @@ -148,7 +167,7 @@ export class workSpaceService implements OnModuleInit {

return char;
}
async getDocument(): Promise<Workspace | null> {
async getDocument(): Promise<CRDTWorkSpace | null> {
let doc = await this.workspaceModel.findOne();
if (!doc) {
const serializedWorkspace = this.workspace.serialize();
Expand Down Expand Up @@ -186,7 +205,7 @@ export class workSpaceService implements OnModuleInit {
}
return doc;
}
async updateDocument(): Promise<Workspace | null> {
async updateDocument(): Promise<CRDTWorkSpace | null> {
const serializedWorkspace = this.workspace.serialize();
return await this.workspaceModel
.findOneAndUpdate(
Expand All @@ -200,7 +219,7 @@ export class workSpaceService implements OnModuleInit {
{ new: true, upsert: true },
)
.exec();
}
}

// 각 레벨별 구체적인 Insert/Delete 처리 메서드들
async handleWorkSpaceInsert(payload: any): Promise<void> {
Expand Down Expand Up @@ -249,6 +268,7 @@ export class workSpaceService implements OnModuleInit {
return this.workspace;
}
}
}

// this.crdt = new EditorCRDT(0);
// try {
Expand Down

0 comments on commit c6d7dfd

Please sign in to comment.