prevent double update (#86794)

This commit is contained in:
Joe Reuter 2021-01-04 13:21:54 +01:00 committed by GitHub
parent c9d3fea9c5
commit d797b0c6b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View file

@ -146,6 +146,35 @@ describe('ExpressionRenderer', () => {
instance.unmount();
});
it('should not update twice immediately after rendering', () => {
jest.useFakeTimers();
const refreshSubject = new Subject();
const loaderUpdate = jest.fn();
(ExpressionLoader as jest.Mock).mockImplementation(() => {
return {
render$: new Subject(),
data$: new Subject(),
loading$: new Subject(),
update: loaderUpdate,
destroy: jest.fn(),
};
});
const instance = mount(
<ReactExpressionRenderer reload$={refreshSubject} expression="" debounce={1000} />
);
act(() => {
jest.runAllTimers();
});
expect(loaderUpdate).toHaveBeenCalledTimes(1);
instance.unmount();
});
it('waits for debounce period on other loader option change if specified', () => {
jest.useFakeTimers();

View file

@ -91,7 +91,12 @@ export const ReactExpressionRenderer = ({
);
const [debouncedExpression, setDebouncedExpression] = useState(expression);
const [waitingForDebounceToComplete, setDebouncePending] = useState(false);
const firstRender = useRef(true);
useShallowCompareEffect(() => {
if (firstRender.current) {
firstRender.current = false;
return;
}
if (debounce === undefined) {
return;
}