-
Notifications
You must be signed in to change notification settings - Fork 525
fix issue #1625 #1702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix issue #1625 #1702
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,22 +11,18 @@ import { LayoutModel } from "./layoutModel"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| import { LayoutNode, NodeModel, TileLayoutContents } from "./types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| const layoutModelMap: Map<string, LayoutModel> = new Map(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const timeoutMap: Map<string, NodeJS.Timeout | null> = new Map(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function getLayoutModelForTab(tabAtom: Atom<Tab>): LayoutModel { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const tabData = globalStore.get(tabAtom); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!tabData) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const tabId = tabData.oid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (layoutModelMap.has(tabId)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const layoutModel = layoutModelMap.get(tabData.oid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (layoutModel) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return layoutModel; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const layoutTreeStateAtom = withLayoutTreeStateAtomFromTab(tabAtom); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const layoutModel = new LayoutModel(layoutTreeStateAtom, globalStore.get, globalStore.set); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| globalStore.sub(layoutTreeStateAtom, () => fireAndForget(layoutModel.onTreeStateAtomUpdated.bind(layoutModel))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| layoutModelMap.set(tabId, layoutModel); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return layoutModel; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return computeIfAbsent(layoutModelMap, tabId, (_) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const layoutTreeStateAtom = withLayoutTreeStateAtomFromTab(tabAtom); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const layoutModel = new LayoutModel(layoutTreeStateAtom, globalStore.get, globalStore.set); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| globalStore.sub(layoutTreeStateAtom, () => fireAndForget(layoutModel.onTreeStateAtomUpdated.bind(layoutModel))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return layoutModel; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function getLayoutModelForTabById(tabId: string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -73,25 +69,26 @@ export function useDebouncedNodeInnerRect(nodeModel: NodeModel): CSSProperties { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| const isResizing = useAtomValue(nodeModel.isResizing); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const prefersReducedMotion = useAtomValue(atoms.prefersReducedMotionAtom); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [innerRect, setInnerRect] = useState<CSSProperties>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [innerRectDebounceTimeout, setInnerRectDebounceTimeout] = useState<NodeJS.Timeout>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| const setInnerRectDebounced = useCallback( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| (nodeInnerRect: CSSProperties) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| clearInnerRectDebounce(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| setInnerRectDebounceTimeout( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| setInnerRect(nodeInnerRect); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, animationTimeS * 1000) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const timeout = setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| setInnerRect(nodeInnerRect); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, animationTimeS * 1000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| computeIfAbsent(timeoutMap, nodeModel.blockId, (_) => timeout) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| [animationTimeS] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update dependency array for setInnerRectDebounced. The callback depends on Add missing dependencies: - [animationTimeS]
+ [animationTimeS, clearInnerRectDebounce, nodeModel.blockId]📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const clearInnerRectDebounce = useCallback(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (innerRectDebounceTimeout) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| clearTimeout(innerRectDebounceTimeout); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| setInnerRectDebounceTimeout(undefined); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const clearInnerRectDebounce = function () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (timeoutMap.has(nodeModel.blockId)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const innerRectDebounceTimeout = timeoutMap.get(nodeModel.blockId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (innerRectDebounceTimeout) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| clearTimeout(innerRectDebounceTimeout); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeoutMap.delete(nodeModel.blockId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [innerRectDebounceTimeout]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+88
to
+96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add cleanup on component unmount. The timeout needs to be cleared when the component unmounts to prevent memory leaks and potential state updates on unmounted components. Add a cleanup effect: + useEffect(() => {
+ return () => clearInnerRectDebounce();
+ }, []);
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (prefersReducedMotion || isMagnified || isResizing) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -104,3 +101,11 @@ export function useDebouncedNodeInnerRect(nodeModel: NodeModel): CSSProperties { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return innerRect; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| function computeIfAbsent<V, F>(map: Map<V, F>, key: V, mappingFunction: (a: V) => F): F { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!map.has(key)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const newValue = mappingFunction(key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| map.set(key, newValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return map.get(key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| function computeIfAbsent<V, F>(map: Map<V, F>, key: V, mappingFunction: (a: V) => F): F { | |
| if (!map.has(key)) { | |
| const newValue = mappingFunction(key); | |
| map.set(key, newValue); | |
| } | |
| return map.get(key); | |
| } | |
| /** | |
| * Returns the value associated with the key if it exists in the map, | |
| * otherwise computes a new value using the mapping function, stores it, and returns it. | |
| * @param map - The map to check/update | |
| * @param key - The key to look up | |
| * @param mappingFunction - Function to compute the value if not present | |
| * @throws Error if the computed value is undefined | |
| */ | |
| function computeIfAbsent<V, F>(map: Map<V, F>, key: V, mappingFunction: (a: V) => F): F { | |
| if (!map.has(key)) { | |
| const newValue = mappingFunction(key); | |
| if (newValue === undefined) { | |
| throw new Error(`Mapping function returned undefined for key: ${key}`); | |
| } | |
| map.set(key, newValue); | |
| } | |
| return map.get(key)!; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix incorrect usage of computeIfAbsent for timeouts.
The current implementation won't update existing timeouts because
computeIfAbsentonly sets the value if the key doesn't exist. This could lead to stale timeouts.Replace with direct map setting:
📝 Committable suggestion