Skip to content

Commit

Permalink
fix: stop reading file every second
Browse files Browse the repository at this point in the history
  • Loading branch information
ocapraro committed Apr 26, 2022
1 parent 7dc04d4 commit f010647
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ExcalidrawCanvas.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ const resolvablePromise = () => {
return promise;
};

const saveData = async (setInitialData, curData, id, saveToFile, readFile, closeDrawing=true, exportAsSvg=true) => {
const prevData = await readFile("data-"+id+".json", "excalidraw-files");
const saveData = async (setInitialData, curData, id, saveToFile, readFile, prevData, setData, closeDrawing=true, exportAsSvg=true) => {
// const prevData = await readFile("data-"+id+".json", "excalidraw-files");
let formattedData = {...curData};
formattedData.appState.collaborators = [];
if(prevData != JSON.stringify(formattedData)){
setInitialData((data)=>{
formattedData.appState.currentItemStrokeColor = data.appState.currentItemStrokeColor;
return formattedData
});
setData(JSON.stringify(formattedData));
saveToFile("data-"+id+".json", JSON.stringify(formattedData), "excalidraw-files", false);
}
if(exportAsSvg){
Expand Down Expand Up @@ -79,6 +80,7 @@ export function ExcalidrawCanvas({ id, saveToFile, gridMode, colors, readFile, p
libraryItems: []
}
);
const [lastSaveData, setLastSaveData] = useState(prevData||"");

let canvas = document.getElementById(`math-canvas-${id}`);
let mathBlock = document.querySelector(`.math-block-${id}`);
Expand Down Expand Up @@ -113,9 +115,9 @@ export function ExcalidrawCanvas({ id, saveToFile, gridMode, colors, readFile, p
};
// auto save
if(document.querySelector(`.math-block-${id}`)){
saveData(setInitialData, curData, id, saveToFile,readFile, false, false);
saveData(setInitialData, curData, id, saveToFile,readFile, lastSaveData, setLastSaveData, false, false);
}else{
saveData(setInitialData, curData, id, saveToFile,readFile, true, true);
saveData(setInitialData, curData, id, saveToFile,readFile, lastSaveData, setLastSaveData, true, true);
clearInterval(saveIntervalRef.current);
}
}, 1000);
Expand Down Expand Up @@ -208,7 +210,7 @@ export function ExcalidrawCanvas({ id, saveToFile, gridMode, colors, readFile, p
scrollToContent: false,
libraryItems: []
};
saveData(setInitialData, curData, id, saveToFile, readFile);
saveData(setInitialData, curData, id, saveToFile, readFile, lastSaveData, setLastSaveData);
clearInterval(saveIntervalRef.current);
}} style={{
zIndex:5,
Expand Down

0 comments on commit f010647

Please sign in to comment.