Skip to content

Commit b7b73d4

Browse files
committed
prevent double update
1 parent ce546ad commit b7b73d4

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/plugins/expressions/public/react_expression_renderer.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,35 @@ describe('ExpressionRenderer', () => {
146146
instance.unmount();
147147
});
148148

149+
it('should not update twice immediately after rendering', () => {
150+
jest.useFakeTimers();
151+
152+
const refreshSubject = new Subject();
153+
const loaderUpdate = jest.fn();
154+
155+
(ExpressionLoader as jest.Mock).mockImplementation(() => {
156+
return {
157+
render$: new Subject(),
158+
data$: new Subject(),
159+
loading$: new Subject(),
160+
update: loaderUpdate,
161+
destroy: jest.fn(),
162+
};
163+
});
164+
165+
const instance = mount(
166+
<ReactExpressionRenderer reload$={refreshSubject} expression="" debounce={1000} />
167+
);
168+
169+
act(() => {
170+
jest.runAllTimers();
171+
});
172+
173+
expect(loaderUpdate).toHaveBeenCalledTimes(1);
174+
175+
instance.unmount();
176+
});
177+
149178
it('waits for debounce period on other loader option change if specified', () => {
150179
jest.useFakeTimers();
151180

src/plugins/expressions/public/react_expression_renderer.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ export const ReactExpressionRenderer = ({
9191
);
9292
const [debouncedExpression, setDebouncedExpression] = useState(expression);
9393
const [waitingForDebounceToComplete, setDebouncePending] = useState(false);
94+
const firstRender = useRef(true);
9495
useShallowCompareEffect(() => {
96+
if (firstRender.current) {
97+
firstRender.current = false;
98+
return;
99+
}
95100
if (debounce === undefined) {
96101
return;
97102
}

0 commit comments

Comments
 (0)