Skip to content

Commit

Permalink
collaboration-service: fix editor debouncing
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinSoo committed Nov 7, 2023
1 parent 04c4c2e commit f62e3b0
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions client/src/components/EditorContainer/EditorContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ const EditorContainer = ({
roomId,
editorCode,
}) => {
const codeRef = useRef(editorCode)
const codeRef = useRef(editorCode);
const editorRef = useRef(null);
const cursorRef = useRef(null)
function onEditorDidMount(editor, monaco) {
editorRef.current = editor;
// Setting initial editor state
editorRef.current.getModel().setValue(editorCode);
}

function handleEditorChange(code, event) {
console.log(event);
if (codeRef.current !== code) {
// Code is different from the reference (redis)
console.log("local changes pushed", codeRef.current, code);
console.log("local changes pushed");
socket.emit("push-changes", event.changes, code, roomId);
}
}
Expand All @@ -29,8 +31,17 @@ const EditorContainer = ({
// Receiving changes from other server
socket.on("receive-changes", (changes, code) => {
codeRef.current = code;
console.log("Applying remote changes", codeRef.current, code);
console.log("Applying remote changes");
cursorRef.current = editorRef.current.getModel().getPosition();
console.log("saving pos", pos);
editorRef.current.getModel().applyEdits(changes);
if (editorRef.current.getModel().getValue() !== code) {
console.log("Client is out of sync");
editorRef.current.getModel().setValue(code);
// TODO: calculate pos for better precision
editorRef.current.getModel().setPosition(cursorRef.current);
console.log("Forced sync completed");
}
});
}
}, [socket]);
Expand Down

0 comments on commit f62e3b0

Please sign in to comment.