Skip to content

Commit 4d97cf1

Browse files
authored
Feat/#126-BK: 회의록 DB에 저장하기 (#131)
1 parent 9cdd99d commit 4d97cf1

File tree

7 files changed

+44
-21
lines changed

7 files changed

+44
-21
lines changed

@wabinar-crdt/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ class CRDT {
1616
private client: number;
1717
private structure: LinkedList;
1818

19-
constructor(initialclock: number = 1, client: number = 0) {
19+
constructor(
20+
initialclock: number = 1,
21+
client: number = 0,
22+
initialStructure: LinkedList,
23+
) {
2024
this.clock = initialclock;
2125
this.client = client;
22-
this.structure = new LinkedList();
26+
27+
Object.setPrototypeOf(initialStructure, LinkedList.prototype);
28+
this.structure = initialStructure as LinkedList;
2329
}
2430

25-
setClientId(id: number) {
26-
this.client = id;
27-
Object.setPrototypeOf(this.structure, LinkedList.prototype);
31+
get data() {
32+
return this.structure;
2833
}
2934

3035
generateNode(letter: string) {

@wabinar-crdt/linked-list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type RemoteIdentifier = Identifier | null;
55
type ModifiedIndex = number | null;
66

77
export default class LinkedList {
8-
private head?: Node;
8+
head?: Node;
99

1010
insertByIndex(index: number, node: Node): RemoteIdentifier {
1111
try {

client/src/components/Editor/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import style from './style.module.scss';
99

1010
function Editor() {
1111
const workspace = useParams();
12-
const socket: Socket = io(
13-
`${env.SERVER_PATH}/api/sc-workspace/${workspace.id}`,
14-
);
12+
const socket: Socket = io(`${env.SERVER_PATH}/sc-workspace/${workspace.id}`);
1513

1614
const {
1715
syncCRDT,

client/src/hooks/useCRDT.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import CRDT, {
22
RemoteInsertOperation,
33
RemoteDeleteOperation,
44
} from '@wabinar/crdt';
5+
import LinkedList from '@wabinar/crdt/linked-list';
56
import { useRef } from 'react';
67
import { useUserContext } from 'src/hooks/useUserContext';
78

@@ -16,17 +17,18 @@ interface RemoteOperation {
1617
}
1718

1819
export function useCRDT() {
19-
const crdtRef = useRef<CRDT>(new CRDT());
2020
const userContext = useUserContext();
21+
const clientId = userContext.userInfo?.user.id;
22+
23+
const crdtRef = useRef<CRDT>(new CRDT(1, clientId, new LinkedList()));
2124

2225
let initialized = false;
2326
const operationSet: RemoteOperation[] = [];
2427

2528
const syncCRDT = (object: unknown) => {
26-
Object.setPrototypeOf(object, CRDT.prototype);
29+
Object.setPrototypeOf(object, LinkedList.prototype);
2730

28-
crdtRef.current = object as CRDT;
29-
crdtRef.current.setClientId(userContext.userInfo?.user.id);
31+
crdtRef.current = new CRDT(1, clientId, object as LinkedList);
3032

3133
initialized = true;
3234
operationSet.forEach(({ type, op }) => {

server/apis/mom/model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import mongoose from '@db';
22
import { Schema } from 'mongoose';
3+
import LinkedList from '@wabinar/crdt/linked-list';
34

45
interface Block {
56
type: String;
@@ -9,11 +10,13 @@ interface Block {
910
interface Mom {
1011
name: string;
1112
blocks: Block[];
13+
structure: LinkedList;
1214
}
1315

1416
const momSchema = new Schema<Mom>({
1517
name: String,
1618
blocks: Array<Block>,
19+
structure: Object,
1720
});
1821

1922
const momModel = mongoose.model('Mom', momSchema);

server/apis/mom/service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import LinkedList from '@wabinar/crdt/linked-list';
12
import momModel from './model';
23

34
// TODO: 예외처리
4-
export const getMom = async (id: number) => {
5+
export const getMom = async (id: string) => {
56
const mom = await momModel.findOne({ _id: id });
67
return mom;
78
};
@@ -10,3 +11,7 @@ export const createMom = async () => {
1011
const mom = await momModel.create({ name: '', blocks: [] });
1112
return mom;
1213
};
14+
15+
export const putMom = async (id: string, structure: LinkedList) => {
16+
await momModel.updateOne({ _id: id }, { structure });
17+
};

server/socket/mom.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import { createMom, getMom } from '@apis/mom/service';
1+
import { createMom, getMom, putMom } from '@apis/mom/service';
22
import { Server } from 'socket.io';
33
import CRDT from '@wabinar/crdt';
44

5-
const crdt = new CRDT(1, -100);
5+
async function momSocketServer(io: Server) {
6+
const momId = '6380c9f7b757041ca21fe96c';
7+
8+
const { structure } = await getMom(momId);
9+
10+
const crdt = new CRDT(1, -1, structure);
611

7-
function momSocketServer(io: Server) {
812
const workspace = io.of(/^\/api\/sc-workspace\/\d+$/);
913

10-
workspace.on('connection', (socket) => {
14+
workspace.on('connection', async (socket) => {
1115
const name = socket.nsp.name;
1216
const workspaceId = name.match(/\d+/g)[0];
1317

@@ -16,9 +20,6 @@ function momSocketServer(io: Server) {
1620
return;
1721
}
1822

19-
// 초기화에 필요한 정보 전달
20-
socket.emit('mom-initialization', crdt);
21-
2223
/* 회의록 선택 시 회의록 정보 불러오기 */
2324
socket.on('select-mom', async (roomId) => {
2425
socket.join(roomId);
@@ -37,11 +38,15 @@ function momSocketServer(io: Server) {
3738
socket.on('mom-insertion', async (op) => {
3839
socket.broadcast.emit('mom-insertion', op);
3940
crdt.remoteInsert(op);
41+
42+
putMom(momId, crdt.data);
4043
});
4144

4245
socket.on('mom-deletion', async (op) => {
4346
socket.broadcast.emit('mom-deletion', op);
4447
crdt.remoteDelete(op);
48+
49+
putMom(momId, crdt.data);
4550
});
4651

4752
// 에러 시
@@ -53,6 +58,11 @@ function momSocketServer(io: Server) {
5358
socket.on('disconnect', () => {
5459
console.log('user disconnected', socket.id);
5560
});
61+
62+
// 초기화에 필요한 정보 전달
63+
const { structure } = await getMom(momId);
64+
65+
socket.emit('mom-initialization', structure);
5666
});
5767
}
5868

0 commit comments

Comments
 (0)