Skip to content

Commit fbad58b

Browse files
ugur-vaadinvursen
andauthored
fix: prevent debouncer override in callback (#3749)
* fix: prevent debouncer override in callback Prevent debouncer override in combobox connector callback * refactor: format * refactor: remove unnecessary use of page number Remove unnecessary use of page number. Change the location of the new code block in which the presence of a debouncer is checked. Change the name of the debouncer variable. * refactor: add comments and issue link Add comments for the changes. Add link to the issue above the relevant test. * Update vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/test/java/com/vaadin/flow/component/combobox/test/LazyLoadingIT.java Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com> * Update vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/test/java/com/vaadin/flow/component/combobox/test/LazyLoadingIT.java Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com> * fix: revert unintentional title change and update id in test component Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
1 parent 86fc178 commit fbad58b

3 files changed

Lines changed: 48 additions & 9 deletions

File tree

  • vaadin-combo-box-flow-parent

‎vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/main/java/com/vaadin/flow/component/combobox/test/LazyLoadingPage.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public LazyLoadingPage() {
6868
addSeparator();
6969
createComboBoxWithDisabledLazyLoading();
7070
addSeparator();
71+
createComboBoxWithSmallPageSizeAndLazyLoading();
72+
addSeparator();
7173
}
7274

7375
private void createListDataProviderWithStringsAutoOpenDisabled() {
@@ -295,6 +297,18 @@ private void createComboBoxWithDisabledLazyLoading() {
295297
add(comboBox, enableLazyLoading);
296298
}
297299

300+
private void createComboBoxWithSmallPageSizeAndLazyLoading() {
301+
addTitle("Callback data provider with small page size 2");
302+
ComboBox<String> comboBox = new ComboBox<>(2);
303+
comboBox.setId("lazy-small-custom-page-size");
304+
305+
comboBox.setItems(
306+
query -> IntStream.range(0, 500).mapToObj(String::valueOf)
307+
.skip(query.getOffset()).limit(query.getLimit()));
308+
309+
add(comboBox);
310+
}
311+
298312
public static List<String> generateStrings(int count) {
299313
List<String> items = IntStream.range(0, count)
300314
.mapToObj(i -> "Item " + i).collect(Collectors.toList());

‎vaadin-combo-box-flow-parent/vaadin-combo-box-flow-integration-tests/src/test/java/com/vaadin/flow/component/combobox/test/LazyLoadingIT.java‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class LazyLoadingIT extends AbstractComboBoxIT {
3838
private ComboBoxElement emptyCallbackBox;
3939
private ComboBoxElement lazyCustomPageSize;
4040
private ComboBoxElement disabledLazyLoadingBox;
41+
private ComboBoxElement lazyWithSmallCustomPageSize;
4142

4243
private WebElement lazySizeRequestCountSpan;
4344

@@ -62,6 +63,8 @@ public void init() {
6263
By.id("callback-dataprovider-size-request-count"));
6364
disabledLazyLoadingBox = $(ComboBoxElement.class)
6465
.id("disabled-lazy-loading");
66+
lazyWithSmallCustomPageSize = $(ComboBoxElement.class)
67+
.id("lazy-small-custom-page-size");
6568
}
6669

6770
@Test
@@ -562,7 +565,7 @@ public void disabledLazyLoading_reducePageSize_enablesLazyLoading() {
562565
disabledLazyLoadingBox.openPopup();
563566
assertLoadedItemsCount("Initially all 100 items should be loaded", 100,
564567
disabledLazyLoadingBox);
565-
lazyCustomPageSize.closePopup();
568+
disabledLazyLoadingBox.closePopup();
566569

567570
clickButton("enable-lazy-loading");
568571
disabledLazyLoadingBox.openPopup();
@@ -576,6 +579,24 @@ public void disabledLazyLoading_reducePageSize_enablesLazyLoading() {
576579
assertRendered("99");
577580
}
578581

582+
@Test
583+
// https://github.com/vaadin/flow-components/issues/3595
584+
public void smallCustomPageSize_filter_selectItem_loadingStateResolved() {
585+
String item = "2";
586+
587+
lazyWithSmallCustomPageSize.selectByText(item);
588+
lazyWithSmallCustomPageSize.setFilter(item);
589+
Assert.assertEquals(item,
590+
getSelectedItemLabel(lazyWithSmallCustomPageSize));
591+
592+
lazyWithSmallCustomPageSize.closePopup();
593+
Assert.assertEquals(item,
594+
getSelectedItemLabel(lazyWithSmallCustomPageSize));
595+
596+
lazyWithSmallCustomPageSize.click();
597+
assertLoadingStateResolved(lazyWithSmallCustomPageSize);
598+
}
599+
579600
private void assertMessage(String expectedMessage) {
580601
Assert.assertEquals(expectedMessage, $("div").id("message").getText());
581602
}

‎vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/resources/META-INF/resources/frontend/comboBoxConnector.js‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import { ComboBoxPlaceholder } from '@vaadin/combo-box/src/vaadin-combo-box-plac
9696
if (filterChanged) {
9797
cache = {};
9898
lastFilter = params.filter;
99-
this._debouncer = Debouncer.debounce(this._debouncer, timeOut.after(500), () => {
99+
this._filterDebouncer = Debouncer.debounce(this._filterDebouncer, timeOut.after(500), () => {
100100
if (serverFacade.getLastFilterSentToServer() === params.filter) {
101101
// Fixes the case when the filter changes
102102
// to something else and back to the original value
@@ -107,13 +107,23 @@ import { ComboBoxPlaceholder } from '@vaadin/combo-box/src/vaadin-combo-box-plac
107107
if (params.filter !== lastFilter) {
108108
throw new Error("Expected params.filter to be '" + lastFilter + "' but was '" + params.filter + "'");
109109
}
110+
// Remove the debouncer before clearing page callbacks.
111+
// This makes sure that they are executed.
112+
this._filterDebouncer = undefined;
110113
// Call the method again after debounce.
111114
clearPageCallbacks();
112115
comboBox.dataProvider(params, callback);
113116
});
114117
return;
115118
}
116119

120+
// Postpone the execution of new callbacks if there is an active debouncer.
121+
// They will be executed when the page callbacks are cleared within the debouncer.
122+
if (this._filterDebouncer) {
123+
pageCallbacks[params.page] = callback;
124+
return;
125+
}
126+
117127
if (cache[params.page]) {
118128
// This may happen after skipping pages by scrolling fast
119129
commitPage(params.page, callback);
@@ -138,13 +148,7 @@ import { ComboBoxPlaceholder } from '@vaadin/combo-box/src/vaadin-combo-box-plac
138148
const startIndex = params.pageSize * rangeMin;
139149
const endIndex = params.pageSize * (rangeMax + 1);
140150

141-
if (!this._debouncer || !this._debouncer.isActive()) {
142-
serverFacade.requestData(startIndex, endIndex, params);
143-
} else {
144-
this._debouncer = Debouncer.debounce(this._debouncer, timeOut.after(200), () =>
145-
serverFacade.requestData(startIndex, endIndex, params)
146-
);
147-
}
151+
serverFacade.requestData(startIndex, endIndex, params);
148152
}
149153
}
150154
};

0 commit comments

Comments
 (0)