Record a generation when the layout node gets updated to optimize backend calls (#27)

This commit is contained in:
Evan Simkowitz 2024-06-06 18:04:54 -07:00 committed by GitHub
parent 441463b172
commit 86109daeed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 2 deletions

View File

@ -32,15 +32,17 @@ export function newLayoutTreeStateAtom<T>(rootNode: LayoutNode<T>): PrimitiveAto
*/
export function withLayoutTreeState<T>(layoutNodeAtom: WritableLayoutNodeAtom<T>): WritableLayoutTreeStateAtom<T> {
const pendingActionAtom = atom<LayoutTreeAction>(null) as PrimitiveAtom<LayoutTreeAction>;
const generationAtom = atom(0) as PrimitiveAtom<number>;
return atom(
(get) => {
const layoutState = newLayoutTreeState(get(layoutNodeAtom));
layoutState.pendingAction = get(pendingActionAtom);
layoutState.generation = get(generationAtom);
return layoutState;
},
(_get, set, value) => {
(get, set, value) => {
set(pendingActionAtom, value.pendingAction);
set(layoutNodeAtom, value.rootNode);
if (get(generationAtom) !== value.generation) set(layoutNodeAtom, value.rootNode);
}
);
}

View File

@ -36,6 +36,7 @@ export function newLayoutTreeState<T>(rootNode: LayoutNode<T>): LayoutTreeState<
rootNode: balancedRootNode,
leafs,
pendingAction: undefined,
generation: 0,
};
}
@ -77,12 +78,15 @@ function layoutTreeStateReducerInner<T>(layoutTreeState: LayoutTreeState<T>, act
break;
case LayoutTreeActionType.Move:
moveNode(layoutTreeState, action as LayoutTreeMoveNodeAction<T>);
layoutTreeState.generation++;
break;
case LayoutTreeActionType.InsertNode:
insertNode(layoutTreeState, action as LayoutTreeInsertNodeAction<T>);
layoutTreeState.generation++;
break;
case LayoutTreeActionType.DeleteNode:
deleteNode(layoutTreeState, action as LayoutTreeDeleteNodeAction);
layoutTreeState.generation++;
break;
default: {
console.error("Invalid reducer action", layoutTreeState, action);

View File

@ -106,6 +106,7 @@ export type LayoutTreeState<T> = {
rootNode: LayoutNode<T>;
leafs: LayoutNode<T>[];
pendingAction: LayoutTreeAction;
generation: number;
};
/**