Skip to content

Commit

Permalink
prevent early guard firing (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
VasilyStrelyaev authored Jan 12, 2024
1 parent 0a9ffe1 commit ddbdec4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ describe('option control', () => {
});
});

it('rolls back controlled options on second timer (to account for async React 18+ updates)', () => {
render(
<ControlledComponent everyOption={123} />,
);

fireOptionChange('everyOption', 234);
jest.runOnlyPendingTimers();

expect(Widget.option.mock.calls.length).toBe(0);

jest.runAllTimers();

expect(Widget.option.mock.calls.length).toBe(1);
expect(Widget.option.mock.calls[0]).toEqual(['everyOption', 123]);
});

it('rolls back controlled complex option', () => {
render(
<ControlledComponent complexOption={{ a: 123, b: 234 }} />,
Expand Down Expand Up @@ -412,6 +428,25 @@ describe('option control', () => {
expect(Widget.option.mock.calls[0]).toEqual(['everyOption', 234]);
});

it('applies option change with async React 18+ update', () => {
const { rerender } = render(
<ControlledComponent everyOption={123} />,
);

fireOptionChange('everyOption', 234);

jest.runOnlyPendingTimers();
expect(Widget.option.mock.calls.length).toBe(0);

rerender(
<ControlledComponent everyOption={234} />,
);

jest.runAllTimers();
expect(Widget.option.mock.calls.length).toBe(1);
expect(Widget.option.mock.calls[0]).toEqual(['everyOption', 234]);
});

it('applies complex option change', () => {
const { rerender } = render(
<ControlledComponent complexOption={{ a: 123 }} />,
Expand Down
6 changes: 5 additions & 1 deletion packages/devextreme-react/src/core/options-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import type { IConfigNode } from './configuration/config-node';

const optionsManagers = new Set<OptionsManager>();
let guardTimeoutHandler = -1;
let innerGuardTimeoutHandler = -1;

export function unscheduleGuards(): void {
clearTimeout(guardTimeoutHandler);
clearTimeout(innerGuardTimeoutHandler);
}
export function scheduleGuards(): void {
unscheduleGuards();
guardTimeoutHandler = window.setTimeout(() => {
optionsManagers.forEach((optionManager) => optionManager.execGuards());
innerGuardTimeoutHandler = window.setTimeout(() => {
optionsManagers.forEach((optionManager) => optionManager.execGuards());
});
});
}

Expand Down

0 comments on commit ddbdec4

Please sign in to comment.