Skip to content

Commit

Permalink
fix: use request animate frame
Browse files Browse the repository at this point in the history
  • Loading branch information
KostyaCholak committed Jul 9, 2024
1 parent 462e9f8 commit 7d11349
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { dataclassDisplayConfig } from "./components/canvas/render";
import { IS_MOCKED, MOCK_VIEWS } from "./mock";
import { Representation } from "./sources";
import { pushHistory } from "./structures/history";
import React from "react";


function ServerURIInput() {
Expand Down Expand Up @@ -121,6 +122,25 @@ export default function App() {
s.views, s.setStructure, s.modifyStructure,
]);

const requestRef = React.useRef<number>(0);

const animate = (_: any) => {
for (let view_id of accumulatedDiffs.keys()) {
const diffs = accumulatedDiffs.get(view_id)!.slice();
accumulatedDiffs.get(view_id)!.length = 0;
if (!diffs.length) {
continue;
}
modifyStructure(view_id, data => {
for (let diff of diffs) {
pushHistory(data, diff);
}
return data;
});
}
requestRef.current = requestAnimationFrame(animate);
}

useEffect(() => {
api.onConnected(() => {
setIsConnected(true);
Expand Down Expand Up @@ -148,21 +168,12 @@ export default function App() {
}
});

setInterval(() => {
for (let view_id of accumulatedDiffs.keys()) {
const diffs = accumulatedDiffs.get(view_id)!.slice();
accumulatedDiffs.get(view_id)!.length = 0;
if (!diffs.length) {
continue;
}
modifyStructure(view_id, data => {
for (let diff of diffs) {
pushHistory(data, diff);
}
return data;
});
}
}, 50);
requestRef.current = requestAnimationFrame(animate);
return () => cancelAnimationFrame(requestRef.current);

// setInterval(() => {

// }, 50);
}, []);

let comp = null;
Expand Down

0 comments on commit 7d11349

Please sign in to comment.