Skip to content

Commit 7d425cc

Browse files
authored
Excalidraw loadFiles behaviour improvements (#7635)
* loadFiles behaviour improvements * added comments and added one early exit to loadFiles * more code improvements
1 parent 992295b commit 7d425cc

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

src/domain/common/whiteboard/excalidraw/collab/Collab.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -319,17 +319,20 @@ class Collab {
319319
restoredRemoteElements as RemoteExcalidrawElement[],
320320
appState
321321
);
322-
323-
// Download the files that this instance is missing:
324-
await this.filesManager.loadFiles({ files: remoteFiles });
325-
326322
// Avoid broadcasting to the rest of the collaborators the scene
327323
// we just received!
328324
// Note: this needs to be set before updating the scene as it
329325
// synchronously calls render.
330326
this.lastBroadcastedOrReceivedSceneVersion = hashElementsVersion(reconciledElements);
331327

332-
return reconciledElements;
328+
// Download the files that this instance is missing:
329+
return this.filesManager.loadFiles({ files: remoteFiles }).then(() => {
330+
// once the files are loaded, we need to update the scene again to render them
331+
// instead of updating the scene twice, which is already expensive, we
332+
// return the elements once the files have been loaded
333+
// that way we render the elements and images at the same time
334+
return reconciledElements;
335+
});
333336
};
334337

335338
private handleRemoteSceneUpdate = async (

src/domain/common/whiteboard/excalidraw/useWhiteboardFilesManager.ts

+31-27
Original file line numberDiff line numberDiff line change
@@ -201,38 +201,42 @@ const useWhiteboardFilesManager = ({
201201
}
202202

203203
const files = whiteboard.files;
204+
// leave only the incoming files that don't have a dataURL and are not in the fileStore
205+
const pendingFileIds = Object.keys(files).filter(fileId => !files[fileId]?.dataURL && !fileStore.current[fileId]);
206+
207+
if (!pendingFileIds.length) {
208+
return;
209+
}
204210

205-
const pendingFileIds = Object.keys(files).filter(fileId => !files[fileId]?.dataURL);
206211
log('I need to download these files', pendingFileIds);
207-
const newFiles: BinaryFilesWithUrl = {};
208212

209213
setDownloadingFiles(true);
210214

211-
try {
212-
await Promise.all(
213-
pendingFileIds.map(async fileId => {
214-
const file = whiteboard!.files![fileId];
215-
if (fileStore.current[fileId]?.dataURL) {
216-
log(`No need to download ${fileId} already in the store`, fileStore.current[fileId]);
217-
return;
218-
}
219-
if (file.url) {
220-
log('DOWNLOADING ', file);
221-
try {
222-
const dataURL = await fetchFileToDataURL(file.url);
223-
newFiles[fileId] = { ...file, dataURL } as BinaryFileDataWithUrl;
224-
fileStoreAddFile(fileId, newFiles[fileId]);
225-
} catch (e) {
226-
error(`Error downloading file: ${file.url}`, { label: 'whiteboard-file-manager' });
227-
}
228-
} else {
229-
error(`Cannot download: ${file.id}`, { label: 'whiteboard-file-manager' });
230-
}
231-
})
232-
);
233-
} finally {
234-
setDownloadingFiles(false);
235-
}
215+
await Promise.allSettled(
216+
pendingFileIds.map(async fileId => {
217+
if (fileStore.current[fileId]?.dataURL) {
218+
log(`No need to download ${fileId} already in the store`, fileStore.current[fileId]);
219+
return;
220+
}
221+
const file = whiteboard!.files![fileId];
222+
if (!file.url) {
223+
error(`Cannot download: ${file.id}`, { label: 'whiteboard-file-manager' });
224+
throw new Error(`Cannot download: ${file.id}`);
225+
}
226+
227+
log('DOWNLOADING ', file);
228+
try {
229+
const dataURL = await fetchFileToDataURL(file.url);
230+
// try-catch will avoid putting the file in the store if fetching fails
231+
fileStoreAddFile(fileId, { ...file, dataURL } as BinaryFileDataWithUrl);
232+
} catch (e) {
233+
error(`Error downloading file: ${file.url}`, { label: 'whiteboard-file-manager' });
234+
throw e;
235+
}
236+
})
237+
);
238+
239+
setDownloadingFiles(false);
236240
};
237241

238242
/**

0 commit comments

Comments
 (0)