"This ensures proper saving of collaboration data when user leaves the room Fixes #9104"

This commit is contained in:
Shivansh Kumar 2025-02-10 01:14:48 +05:30
parent 9e49c9254b
commit 74b8f7ee26
3 changed files with 16 additions and 11 deletions

View file

@ -476,7 +476,7 @@ const ExcalidrawWrapper = () => {
collabAPI?.isCollaborating() && collabAPI?.isCollaborating() &&
!isCollaborationLink(window.location.href) !isCollaborationLink(window.location.href)
) { ) {
collabAPI.stopCollaboration(false); await collabAPI.stopCollaboration(false);
} }
excalidrawAPI.updateScene({ appState: { isLoading: true } }); excalidrawAPI.updateScene({ appState: { isLoading: true } });
@ -968,9 +968,9 @@ const ExcalidrawWrapper = () => {
"exit", "exit",
"collaboration", "collaboration",
], ],
perform: () => { perform: async () => {
if (collabAPI) { if (collabAPI) {
collabAPI.stopCollaboration(); await collabAPI.stopCollaboration();
if (!collabAPI.isCollaborating()) { if (!collabAPI.isCollaborating()) {
setShareDialogState({ isOpen: false }); setShareDialogState({ isOpen: false });
} }

View file

@ -340,13 +340,14 @@ class Collab extends PureComponent<CollabProps, CollabState> {
} }
}; };
stopCollaboration = (keepRemoteState = true) => { stopCollaboration = async (keepRemoteState = true) => {
this.queueBroadcastAllElements.cancel(); this.queueBroadcastAllElements.cancel();
this.queueSaveToFirebase.cancel(); this.queueSaveToFirebase.cancel();
this.loadImageFiles.cancel(); this.loadImageFiles.cancel();
this.resetErrorIndicator(true); this.resetErrorIndicator(true);
this.saveCollabRoomToFirebase( // Ensure we save the latest state before stopping collaboration
await this.saveCollabRoomToFirebase(
getSyncableElements( getSyncableElements(
this.excalidrawAPI.getSceneElementsIncludingDeleted(), this.excalidrawAPI.getSceneElementsIncludingDeleted(),
), ),
@ -367,6 +368,9 @@ class Collab extends PureComponent<CollabProps, CollabState> {
// that could have been saved in other tabs while we were collaborating // that could have been saved in other tabs while we were collaborating
resetBrowserStateVersions(); resetBrowserStateVersions();
// Ensure all pending changes are saved before pushing new state
this.queueSaveToFirebase.flush();
window.history.pushState({}, APP_NAME, window.location.origin); window.history.pushState({}, APP_NAME, window.location.origin);
this.destroySocketClient(); this.destroySocketClient();
@ -938,9 +942,9 @@ class Collab extends PureComponent<CollabProps, CollabState> {
}, SYNC_FULL_SCENE_INTERVAL_MS); }, SYNC_FULL_SCENE_INTERVAL_MS);
queueSaveToFirebase = throttle( queueSaveToFirebase = throttle(
() => { async () => {
if (this.portal.socketInitialized) { if (this.portal.socketInitialized) {
this.saveCollabRoomToFirebase( await this.saveCollabRoomToFirebase(
getSyncableElements( getSyncableElements(
this.excalidrawAPI.getSceneElementsIncludingDeleted(), this.excalidrawAPI.getSceneElementsIncludingDeleted(),
), ),

View file

@ -164,10 +164,11 @@ const ActiveRoomDialog = ({
icon={playerStopFilledIcon} icon={playerStopFilledIcon}
onClick={() => { onClick={() => {
trackEvent("share", "room closed"); trackEvent("share", "room closed");
collabAPI.stopCollaboration(); collabAPI.stopCollaboration().then(() => {
if (!collabAPI.isCollaborating()) { if (!collabAPI.isCollaborating()) {
handleClose(); handleClose();
} }
});
}} }}
/> />
</div> </div>