Skip to content

Commit e8c94e8

Browse files
committed
Code Refactoring & Fixes
1 parent 5e8cec3 commit e8c94e8

File tree

33 files changed

+168
-118
lines changed

33 files changed

+168
-118
lines changed

code/client/src/domain/editor/hooks/useEvents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function useEvents(fugueOperations: FugueDomainOperations, { socket }: Communica
1616
}
1717

1818
useSocketListeners(socket, {
19-
operation: onOperation,
19+
operations: onOperation,
2020
});
2121
}
2222

code/client/src/domain/editor/operations/history/operations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default (fugue: Fugue, { socket }: Communication): HistoryDomainOperation
2525
.flat()
2626
.filter(operation => operation !== undefined && operation !== null);
2727

28-
socket.emit('operation', communicationOperations);
28+
socket.emit('operations', communicationOperations);
2929
};
3030

3131
function getOperation(operation: HistoryOperation) {

code/client/src/domain/editor/operations/input/operations.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,39 @@ export default (fugue: Fugue, { socket }: Communication): InputDomainOperations
1111
function insertCharacter(char: string, cursor: Cursor, styles: InlineStyle[] = []) {
1212
if (char.length !== 1) throw new Error('Invalid character');
1313
const operations = fugue.insertLocal(cursor, nodeInsert(char, styles));
14-
socket.emit('operation', operations);
14+
socket.emit('operations', operations);
1515
}
1616

1717
function insertLineBreak(cursor: Cursor) {
1818
const operations = fugue.insertLocal(cursor, '\n');
1919
const styleOperation = fugue.updateBlockStyleLocal(cursor.line + 1, 'paragraph', true);
20-
socket.emit('operation', [styleOperation, ...operations]);
20+
socket.emit('operations', [styleOperation, ...operations]);
2121
}
2222

2323
function deleteCharacter(cursor: Cursor) {
2424
// don't delete line if it's not a paragraph - this is to prevent deleting the block style & line simultaneously
2525
if (cursor.column === 0 && fugue.getBlockStyle(cursor.line) !== 'paragraph') return;
2626
const operations = fugue.deleteLocalByCursor(cursor);
27-
if (operations) socket.emit('operation', operations);
27+
if (operations) socket.emit('operations', operations);
2828
}
2929

3030
function deleteSelection(selection: Selection) {
3131
const operations = fugue.deleteLocal(selection);
32-
socket.emit('operation', operations);
32+
socket.emit('operations', operations);
3333
}
3434

3535
function deleteWord(cursor: Cursor, reverse: boolean) {
3636
const operations = fugue.deleteWordByCursor(cursor, reverse);
3737
if (!operations) return;
38-
socket.emit('operation', operations);
38+
socket.emit('operations', operations);
3939
}
4040

4141
function pasteText(start: Cursor, text: string) {
4242
const chars = text.split('');
4343
const lineNodes = chars.filter(char => char === '\n');
4444
const insertOperations: Operation[] = fugue.insertLocal(start, ...text);
4545
const styleOperations = lineNodes.map(() => fugue.updateBlockStyleLocal(start.line + 1, 'paragraph', true));
46-
socket.emit('operation', [...styleOperations, ...insertOperations]);
46+
socket.emit('operations', [...styleOperations, ...insertOperations]);
4747
}
4848

4949
function updateSelection(range: BaseSelection, styles: InlineStyle[]) {

code/client/src/domain/editor/operations/markdown/operations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default (fugue: Fugue, { socket }: Communication): MarkdownDomainOperatio
3737
operations.push(styleOperation);
3838

3939
// emit operations
40-
socket.emit('operation', operations);
40+
socket.emit('operations', operations);
4141
}
4242

4343
/**
@@ -64,7 +64,7 @@ export default (fugue: Fugue, { socket }: Communication): MarkdownDomainOperatio
6464
operations.push(...styleOperations);
6565

6666
// emit operations
67-
socket.emit('operation', operations);
67+
socket.emit('operations', operations);
6868
}
6969

7070
function deleteBlockStyles(selection: Selection) {
@@ -75,7 +75,7 @@ export default (fugue: Fugue, { socket }: Communication): MarkdownDomainOperatio
7575
if ((start === end && start.column === 0) || start.line !== end.line) {
7676
const newSelection = start.column !== 0 ? { start: { line: start.line + 1, column: 0 }, end } : selection;
7777
const operations = fugue.updateBlockStylesLocalBySelection('paragraph', newSelection);
78-
socket.emit('operation', operations);
78+
socket.emit('operations', operations);
7979
}
8080
}
8181

code/client/src/domain/editor/slate/hooks/useRenderers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function useRenderers(editor: Editor, fugue: Fugue, { socket }: Communication) {
1717
const line = path[path.length - 1];
1818
const updateBlockStyle = (style: BlockStyle) => {
1919
const operation = fugue.updateBlockStyleLocal(line, style);
20-
socket.emit('operation', [operation]);
20+
socket.emit('operations', [operation]);
2121
};
2222
return getElementRenderer(type, props, updateBlockStyle);
2323
},

code/client/src/domain/workspace/WorkspaceContext.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@ import { useParams } from 'react-router-dom';
77

88
export type WorkspaceContextType = {
99
workspace?: Workspace;
10-
filePath?: string;
11-
setFilePath: (path: string) => void;
1210
};
1311

1412
export const WorkspaceContext = createContext<WorkspaceContextType>({
1513
workspace: undefined,
16-
filePath: undefined,
17-
setFilePath: () => {},
1814
});
1915

2016
export function WorkspaceProvider({ children }: { children: React.ReactNode }) {
2117
const [workspace, setWorkspace] = useState<Workspace | undefined>(undefined);
22-
const [filePath, setFilePath] = useState<string | undefined>(undefined);
23-
const { http } = useCommunication();
18+
const { http, socket } = useCommunication();
2419
const { publishError } = useError();
2520
const { wid } = useParams();
2621

@@ -30,8 +25,12 @@ export function WorkspaceProvider({ children }: { children: React.ReactNode }) {
3025
const ws = await http.get(`/workspaces/${wid}`);
3126
setWorkspace(ws);
3227
}
28+
socket.emit('joinWorkspace', wid);
3329
getResources().catch(publishError);
34-
}, [http, publishError, wid]);
30+
return () => {
31+
socket.emit('leaveWorkspace');
32+
};
33+
}, [wid, http, socket, publishError]);
3534

36-
return <WorkspaceContext.Provider value={{ workspace, filePath, setFilePath }}>{children}</WorkspaceContext.Provider>;
35+
return <WorkspaceContext.Provider value={{ workspace }}>{children}</WorkspaceContext.Provider>;
3736
}

code/client/src/services/communication/socket/operationEmitter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class OperationEmitter {
99
private readonly socket: Socket;
1010
private readonly operationBuffer: Operation[] = [];
1111
private readonly timeoutDuration;
12-
private readonly chunkSize = 100;
12+
private readonly chunkSize = 300;
1313
private readonly maxBufferedOperations = 20;
1414
private timeoutId: NodeJS.Timeout | null = null;
1515

@@ -34,7 +34,7 @@ export class OperationEmitter {
3434
if (this.operationBuffer.length > this.chunkSize) {
3535
this.emitChunked();
3636
} else {
37-
this.socket.emit('operation', this.operationBuffer);
37+
this.socket.emit('operations', this.operationBuffer);
3838
}
3939
this.operationBuffer.length = 0;
4040
}
@@ -46,12 +46,12 @@ export class OperationEmitter {
4646
let chunkIndex = 0;
4747
const onAcknowledge = () => {
4848
if (chunkIndex < chunks.length) {
49-
this.socket.emit('operation', chunks[chunkIndex++]);
49+
this.socket.emit('operations', chunks[chunkIndex++]);
5050
return;
5151
}
5252
this.socket.off('ack', onAcknowledge);
5353
};
54-
this.socket.emit('operation', chunks[chunkIndex++]);
54+
this.socket.emit('operations', chunks[chunkIndex++]);
5555
this.socket.on('ack', onAcknowledge);
5656
}
5757
}

code/client/src/services/communication/socket/socketCommunication.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const operationEmitter = new OperationEmitter(socket, OPERATION_DELAY);
2121

2222
function emit(event: string, data: any) {
2323
switch (event) {
24-
case 'operation':
24+
case 'operations':
2525
operationEmitter.addOperation(...data);
2626
break;
2727
case 'cursorChange':

code/client/src/services/documentServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function documentServices(http: HttpCommunication, wid: string) {
1313
}
1414

1515
async function deleteDocument(id: string) {
16-
await http.delete(`/workspace/${wid}/${id}`);
16+
await http.delete(`/workspaces/${wid}/${id}`);
1717
}
1818

1919
async function updateDocument(id: string, name: string) {

code/client/src/ui/components/header/Header.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ header {
1010
box-shadow: black 0 0 1px;
1111
display: flex;
1212
align-items: center;
13-
z-index: 1;
1413

1514
p {
1615
font-size: 2.5vh;

0 commit comments

Comments
 (0)