Skip to content

Commit

Permalink
Handle sync for redo
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax committed Aug 30, 2024
1 parent a5948cd commit 90a4106
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/hooks/useMultiplayerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,26 +407,28 @@ export function useMultiplayerState({
);

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

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);
}, []);

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

const assetsBeforeRedo = [...app.assets];
undoManager.redo();
const assetsAfterRedo = [...app.assets];
Expand All @@ -438,6 +440,7 @@ export function useMultiplayerState({
toast.error("An error occurred while redoing");
}

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

Expand Down
2 changes: 0 additions & 2 deletions src/stores/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ 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();
};
Expand Down

0 comments on commit 90a4106

Please sign in to comment.