Skip to content

Commit

Permalink
Merge pull request #139 from aeagle/hotfix/107-resize-race-condition
Browse files Browse the repository at this point in the history
Fix for race condition in resize operation
  • Loading branch information
aeagle authored Jan 7, 2023
2 parents 736bb59 + 2c08baa commit f2419c4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/components/stories/04-testing/Testing.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, Story, Canvas, Props } from "@storybook/addon-docs";
import { StateDriven, AnchoredDefaultOrdering, SpaceDemoStacked1 } from "../Utils";
import { StateDriven, AnchoredDefaultOrdering, SpaceDemoStacked1, StateDrivenSize } from "../Utils";

<Meta title="Testing/State" />

Expand All @@ -19,4 +19,10 @@ import { StateDriven, AnchoredDefaultOrdering, SpaceDemoStacked1 } from "../Util
<Story name="Test stacked spaces">
<SpaceDemoStacked1 />
</Story>
</Canvas>

<Canvas>
<Story name="State driven size">
<StateDrivenSize />
</Story>
</Canvas>
10 changes: 10 additions & 0 deletions src/components/stories/Utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ export const StateDriven: React.FC = () => {
);
};

export const StateDrivenSize = () => {
const [size, setSize] = React.useState<number | string | undefined>(250);
return (
<ViewPort>
<LeftResizable size={size} onResizeEnd={(s) => setSize(s)}></LeftResizable>
<Fill></Fill>
</ViewPort>
);
};

export const AnchoredDefaultOrdering = () => {
return (
<ViewPort as="main">
Expand Down
21 changes: 17 additions & 4 deletions src/core-resizing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,33 @@ export function createResize(store: ISpaceStore) {
e.preventDefault();

if (RESIZE_THROTTLE > 0) {
throttle((x, y) => window.requestAnimationFrame(() => resize(x, y)), RESIZE_THROTTLE)(lastX, lastY);
throttle(
(x, y) =>
window.requestAnimationFrame(() => {
if (space.resizing) {
resize(x, y);
}
}),
RESIZE_THROTTLE,
)(lastX, lastY);
} else {
window.requestAnimationFrame(() => resize(lastX, lastY));
window.requestAnimationFrame(() => {
if (space.resizing) {
resize(lastX, lastY);
}
});
}
};

const removeListener = () => {
space.resizing = false;

if (moved) {
resize(lastX, lastY);
}

window.removeEventListener(moveEvent, withPreventDefault as EventListener);
window.removeEventListener(endEvent, removeListener);

space.resizing = false;
space.updateParent();

const resizeEnd = onResizeEnd || space.onResizeEnd;
Expand Down

0 comments on commit f2419c4

Please sign in to comment.