Skip to content

Commit

Permalink
작업사항 저장 추후 삭제
Browse files Browse the repository at this point in the history
Co-authored-by: Yeonkyu Min <[email protected]>
  • Loading branch information
hyonun321 and Ludovico7 committed Nov 19, 2024
1 parent a253838 commit 359d346
Show file tree
Hide file tree
Showing 17 changed files with 740 additions and 193 deletions.
11 changes: 9 additions & 2 deletions @noctaCrdt/Crdt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class CRDT<T extends Node<NodeId>> {

localInsert(index: number, value: string, blockId?: BlockId) {}

Check warning on line 24 in @noctaCrdt/Crdt.ts

View workflow job for this annotation

GitHub Actions / Lint and Unit Test

'index' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 24 in @noctaCrdt/Crdt.ts

View workflow job for this annotation

GitHub Actions / Lint and Unit Test

'value' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 24 in @noctaCrdt/Crdt.ts

View workflow job for this annotation

GitHub Actions / Lint and Unit Test

'blockId' is defined but never used. Allowed unused args must match /^_/u

localDelete(index: number, blockId?: BlockId) {}
localDelete(index: number, blockId?: BlockId, pageId?: string) {}

Check warning on line 26 in @noctaCrdt/Crdt.ts

View workflow job for this annotation

GitHub Actions / Lint and Unit Test

'index' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 26 in @noctaCrdt/Crdt.ts

View workflow job for this annotation

GitHub Actions / Lint and Unit Test

'blockId' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 26 in @noctaCrdt/Crdt.ts

View workflow job for this annotation

GitHub Actions / Lint and Unit Test

'pageId' is defined but never used. Allowed unused args must match /^_/u

remoteInsert(operation: RemoteBlockInsertOperation | RemoteCharInsertOperation) {}

Check warning on line 28 in @noctaCrdt/Crdt.ts

View workflow job for this annotation

GitHub Actions / Lint and Unit Test

'operation' is defined but never used. Allowed unused args must match /^_/u

Expand Down Expand Up @@ -64,7 +64,7 @@ export class EditorCRDT extends CRDT<Block> {
return { node: remoteInsertion.node } as RemoteBlockInsertOperation;
}

localDelete(index: number): RemoteBlockDeleteOperation {
localDelete(index: number, blockId: undefined, pageId: string): RemoteBlockDeleteOperation {
if (index < 0 || index >= this.LinkedList.spread().length) {
throw new Error(`Invalid index: ${index}`);
}
Expand All @@ -77,6 +77,7 @@ export class EditorCRDT extends CRDT<Block> {
const operation: RemoteBlockDeleteOperation = {
targetId: nodeToDelete.id,
clock: this.clock + 1,
pageId,
};

this.LinkedList.deleteNode(nodeToDelete.id);
Expand Down Expand Up @@ -144,6 +145,9 @@ export class EditorCRDT extends CRDT<Block> {
this.clock = clock + 1;
}
}
deserialize(): void {
return;
}
}

export class BlockCRDT extends CRDT<Char> {
Expand Down Expand Up @@ -212,4 +216,7 @@ export class BlockCRDT extends CRDT<Char> {
this.clock = clock + 1;
}
}
deserialize(): void {
return;
}
}
8 changes: 8 additions & 0 deletions @noctaCrdt/Interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NodeId, BlockId, CharId } from "./NodeId";
import { Block, Char } from "./Node";
import { Page } from "./Page";

export type ElementType = "p" | "h1" | "h2" | "h3" | "ul" | "ol" | "li" | "checkbox" | "blockquote";

Expand All @@ -17,6 +18,7 @@ export interface RemoteBlockUpdateOperation {
}
export interface RemoteBlockInsertOperation {
node: Block;
pageId: string;
}

export interface RemoteCharInsertOperation {
Expand All @@ -27,6 +29,7 @@ export interface RemoteCharInsertOperation {
export interface RemoteBlockDeleteOperation {
targetId: BlockId;
clock: number;
pageId: string;
}

export interface RemoteCharDeleteOperation {
Expand Down Expand Up @@ -56,6 +59,11 @@ export interface ReorderNodesProps {
afterId: BlockId | null;
}

export interface WorkSpaceSerializedProps {
id: string;
pageList: Page[];
authUser: Map<string, string>;
}
export interface RemoteReorderOperation {
targetId: BlockId;
beforeId: BlockId | null;
Expand Down
12 changes: 10 additions & 2 deletions @noctaCrdt/LinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ export class LinkedList<T extends Node<NodeId>> {
}
}

export class BlockLinkedList extends LinkedList<Block> {}
export class BlockLinkedList extends LinkedList<Block> {
deserialize(): void {
return;
}
}

export class TextLinkedList extends LinkedList<Char> {}
export class TextLinkedList extends LinkedList<Char> {
deserialize(): void {
return;
}
}
19 changes: 19 additions & 0 deletions @noctaCrdt/Page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { EditorCRDT } from "./Crdt";

interface PageProps {
id: string;
title: string;
icon: string;
crdt: EditorCRDT;
}

export class Page {
id: string;
title: string;
Expand All @@ -13,4 +20,16 @@ export class Page {
this.icon = "icon";
this.crdt = editorCRDT;
}

serialize(): PageProps {
return {
id: this.id,
title: this.title,
icon: this.icon,
crdt: this.crdt,
};
}
fromJSON(): void {
return;
}
}
14 changes: 13 additions & 1 deletion @noctaCrdt/WorkSpace.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { Page } from "./Page";
import { WorkSpaceSerializedProps } from "./Interfaces";

export class WorkSpace {
id: string;
pageList: Page[];
authUser: object;
authUser: Map<string, string>;

constructor(id: string, pageList: Page[]) {
this.id = id;
this.pageList = pageList;
this.authUser = new Map();
// 직렬화, 역직렬화 메서드 추가
}

serialize(): WorkSpaceSerializedProps {
return {
id: this.id,
pageList: this.pageList,
authUser: this.authUser,
};
}
deserialize(): void {
return;
}
}
22 changes: 15 additions & 7 deletions client/src/apis/useSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
RemoteCharDeleteOperation,
RemoteBlockUpdateOperation,
CursorPosition,
SerializedProps,
WorkSpaceSerializedProps,
} from "@noctaCrdt/Interfaces";
import { Block, Char } from "@noctaCrdt/Node";
import { useEffect, useRef } from "react";

import { useEffect, useRef, useState } from "react";
import { io, Socket } from "socket.io-client";
// 구독 핸들러들의 타입 정의
interface RemoteOperationHandlers {
Expand All @@ -23,6 +23,7 @@ interface RemoteOperationHandlers {
// 훅의 반환 타입을 명시적으로 정의
interface UseSocketReturn {
socket: Socket | null;
fetchWorkspaceData: () => WorkSpaceSerializedProps;
sendBlockUpdateOperation: (operation: RemoteBlockUpdateOperation) => void;
sendBlockInsertOperation: (operation: RemoteBlockInsertOperation) => void;
sendCharInsertOperation: (operation: RemoteCharInsertOperation) => void;
Expand All @@ -35,7 +36,7 @@ interface UseSocketReturn {
// 반환 타입을 명시적으로 지정
export const useSocket = (): UseSocketReturn => {
const socketRef = useRef<Socket | null>(null);

const [workspace, setWorkspace] = useState<WorkSpaceSerializedProps | null>(null);
useEffect(() => {
const SERVER_URL =
process.env.NODE_ENV === "development" ? "http://localhost:3000" : "https://api.nocta.site";
Expand All @@ -52,9 +53,9 @@ export const useSocket = (): UseSocketReturn => {
console.log("Assigned client ID:", clientId);
});

socketRef.current.on("document", (document: SerializedProps<Block> | SerializedProps<Char>) => {
// 추후 확인 필요
console.log("Received initial document state:", document);
socketRef.current.on("workspace", (workspace: WorkSpaceSerializedProps) => {
console.log("Received initial workspace state:", workspace);
setWorkspace(workspace);
});

socketRef.current.on("connect", () => {
Expand All @@ -69,6 +70,8 @@ export const useSocket = (): UseSocketReturn => {
console.error("Socket error:", error);
});

console.log("소켓 이벤트 on");

return () => {
if (socketRef.current) {
socketRef.current.disconnect();
Expand Down Expand Up @@ -129,7 +132,12 @@ export const useSocket = (): UseSocketReturn => {
};
};

const fetchWorkspaceData = (): WorkSpaceSerializedProps => {
return workspace!;
};

return {
fetchWorkspaceData,
socket: socketRef.current,
sendBlockUpdateOperation,
sendBlockInsertOperation,
Expand Down
Loading

0 comments on commit 359d346

Please sign in to comment.