Skip to content

Commit

Permalink
Disconnect websocket provider on undo
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax committed Aug 29, 2024
1 parent 367fe81 commit a5948cd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/hooks/useMultiplayerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
yShapes,
user,
envs,
pauseSync,
resumeSync,
} from "../stores/setup";
import { STORAGE_SETTINGS_KEY } from "../utils/userSettings";
import { UserPresence } from "../types/UserPresence";
Expand Down Expand Up @@ -405,18 +407,21 @@ export function useMultiplayerState({
);

const onUndo = useCallback(async (app: TldrawApp) => {
pauseSync();
setIsReadOnlyToTrue(app);

const assetsBeforeUndo = [...app.assets];
undoManager.undo();
const assetsAfterUndo = [...app.assets];

try {
await handleAssets(assetsBeforeUndo, assetsAfterUndo);
console.log("handle Assets done");
} catch (error) {
undoManager.redo();
toast.error("An error occurred while undoing");
}

resumeSync();
setIsReadOnlyToFalse(app);
}, []);

Expand Down
12 changes: 12 additions & 0 deletions src/stores/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ const yBindings: Map<TDBinding> = doc.getMap("bindings");
const yAssets: Map<TDAsset> = doc.getMap("assets");
const undoManager = new UndoManager([yShapes, yBindings, yAssets]);

// Function to pause the sync
const pauseSync = () => {
provider.disconnect();
};

// Function to resume the sync
const resumeSync = () => {
provider.connect();
};

export {
envs,
user,
Expand All @@ -51,4 +61,6 @@ export {
yBindings,
yAssets,
undoManager,
pauseSync,
resumeSync,
};

0 comments on commit a5948cd

Please sign in to comment.