Avoid broadcasting what was just received (#1116)

Fixes #1115

The issue is that replaceAllElements calls a render synchronously, preventing lastBroadcastedOrReceivedSceneVersion from being set correctly.

I tried using batchUpdate but it only takes a single argument ( c5d2fc7127/packages/react-reconciler/src/ReactFiberWorkLoop.js (L1088) ) whereas the callback takes two.

Test Plan:
- Add a console.log before `this.broadcastScene("SCENE_UPDATE");` in App.tsx
- Connect a bunch of clients
- Have one move a shape
- Make sure that this client has the console logged
- Make sure the other clients don't have it
This commit is contained in:
Christopher Chedeau 2020-03-28 21:25:40 -07:00 committed by GitHub
parent 763735ac84
commit a7bd21ccf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -731,8 +731,7 @@ export class App extends React.Component<any, AppState> {
);
// Reconcile
globalSceneState.replaceAllElements(
restoredState.elements
const newElements = restoredState.elements
.reduce((elements, element) => {
// if the remote element references one that's currently
// edited on local, skip it (it'll be added in the next
@ -777,13 +776,19 @@ export class App extends React.Component<any, AppState> {
return elements;
}, [] as Mutable<typeof restoredState.elements>)
// add local elements that weren't deleted or on remote
.concat(...Object.values(localElementMap)),
);
}
.concat(...Object.values(localElementMap));
// Avoid broadcasting to the rest of the collaborators the scene
// we just received!
// Note: this needs to be set before replaceAllElements as it
// syncronously calls render.
this.lastBroadcastedOrReceivedSceneVersion = getDrawingVersion(
globalSceneState.getAllElements(),
newElements,
);
globalSceneState.replaceAllElements(newElements);
}
// We haven't yet implemented multiplayer undo functionality, so we clear the undo stack
// when we receive any messages from another peer. This UX can be pretty rough -- if you
// undo, a user makes a change, and then try to redo, your element(s) will be lost. However,