Memoise selection of nodes

Summary:
For the visualiser we use the same trick as with the hover state. We subscribe to selection changes and only render if the prev or new state concerns us.

For the tree we change from object identity to the node id + and indent guide are added to the memoisation equal check.

Depending on teh change this tree memoisation can vary in effectiveness. If you go from nothing selecting to selecting the top element nothing is memoised since react needs to render every element to draw the indent guide. If you have somethign selected and select a nearby element the memoisation works well.

There are ways to improve this more down the road

changelog: UIDebugger improve performance of selecting nodes

Reviewed By: lblasa

Differential Revision: D43305979

fbshipit-source-id: 5d90e806ed7b6a8401e9968be398d4a67ed0c294
This commit is contained in:
Luke De Feo
2023-02-17 02:45:05 -08:00
committed by Facebook GitHub Bot
parent 786ae04d21
commit 8581aa1944
6 changed files with 91 additions and 47 deletions

View File

@@ -0,0 +1,40 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {Atom} from 'flipper-plugin';
import {useEffect, useState} from 'react';
/**
* A hook similar to useValue that Subscribes to an atom and returns the current value.
* However the value only updates if the predicate passes.
*
* Usefull for skipping expensive react renders if an update doesnt concern you
* @param atom
* @param predicate
*/
export function useFilteredValue<T>(
atom: Atom<T>,
predicate: (newValue: T, prevValue?: T) => boolean,
) {
const [value, setValue] = useState(atom.get());
useEffect(() => {
const listener = (newValue: T, prevValue?: T) => {
if (predicate(newValue, prevValue)) {
setValue(newValue);
}
};
atom.subscribe(listener);
return () => {
atom.unsubscribe(listener);
};
}, [atom, predicate]);
return value;
}