Skip to content

Commit

Permalink
js: reimplement setState-callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hartmann authored and JackUrb committed Feb 21, 2023
1 parent de687db commit 910339c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ function App() {
const _timeoutID = useRef(null);
const _pendingPanes = useRef([]);

// flush pre-render callbacks
const callbacks = useRef([]);
callbacks.current.forEach((cb) => {
if (cb) cb();
});
callbacks.current = [];

// --------------------- //
// grid helper functions //
// --------------------- //
Expand Down Expand Up @@ -280,7 +287,9 @@ function App() {
// check if is mounted. error can appear on unmounted component
if (mounted.current) {
setConnected(false);
socket.current = null;
callbacks.current.push(() => {
socket.current = null;
});
}
};

Expand Down Expand Up @@ -432,10 +441,9 @@ function App() {
...prev,
layout: newLayout,
panes: newPanes,
// TODO: before function based react, this has called a relayout (right after setState)
// () => relayout();
}));
setFocusedPaneID(focusedPaneID === paneID ? null : focusedPaneID);
callbacks.current.push(relayout);
}
};

Expand Down Expand Up @@ -561,8 +569,10 @@ function App() {
};

const focusPane = (paneID, callback) => {
if (callback) callback();
if (focusedPaneID != paneID) setFocusedPaneID(paneID);
if (focusedPaneID != paneID) {
setFocusedPaneID(paneID);
if (callback) callbacks.current.push(callback);
} else if (callback) callback();
};

const blurPane = () => {
Expand Down

0 comments on commit 910339c

Please sign in to comment.