mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-02 18:39:05 +01:00
Record a generation when the layout node gets updated to optimize backend calls (#27)
This commit is contained in:
parent
441463b172
commit
86109daeed
@ -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);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -106,6 +106,7 @@ export type LayoutTreeState<T> = {
|
||||
rootNode: LayoutNode<T>;
|
||||
leafs: LayoutNode<T>[];
|
||||
pendingAction: LayoutTreeAction;
|
||||
generation: number;
|
||||
};
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user