Skip to content

Commit

Permalink
fix: send items only once to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-vaadin committed Nov 12, 2024
1 parent 361ccde commit 4128359
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,6 @@ public Column<T> setRenderer(Renderer<T> renderer) {
getElement().getNode()
.runWhenAttached(ui -> scheduleRendererSetup());
addAttachListener(e -> scheduleRendererSetup());
getGrid().getDataCommunicator().reset();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.renderer.LitRenderer;
import com.vaadin.flow.data.renderer.Renderer;
import com.vaadin.flow.function.ValueProvider;
Expand Down Expand Up @@ -218,6 +219,42 @@ public void initiallyHiddenColumnWithCustomRenderer_detachAndReattachGrid_render
column);
}

@Test
public void columnWithCustomRenderer_setAnotherRenderer_onlyNewRendererCalled() {
Grid.Column<String> column = addColumnWithCustomRenderer();
fakeClientCommunication();
callCount.set(0);
AtomicInteger newRendererCallCount = new AtomicInteger(0);
Renderer<String> newRenderer = LitRenderer
.<String> of("<span>${item.displayName}</span>")
.withProperty("displayName", s -> {
newRendererCallCount.incrementAndGet();
return s;
});
column.setRenderer(newRenderer);
fakeClientCommunication();
Assert.assertEquals(0, callCount.get());
Assert.assertEquals(ITEM_COUNT, newRendererCallCount.get());
}

@Test
public void addColumn_itemsSentOnlyOnce() {
List<String> items = getItems();
AtomicInteger fetchCount = new AtomicInteger(0);
DataProvider<String, Void> dataProvider = DataProvider
.fromCallbacks(query -> {
fetchCount.incrementAndGet();
return items.stream().skip(query.getOffset())
.limit(query.getLimit());
}, query -> items.size());
grid.setDataProvider(dataProvider);
fakeClientCommunication();
fetchCount.set(0);
addColumnWithValueProvider();
fakeClientCommunication();
Assert.assertEquals(1, fetchCount.get());
}

private Grid.Column<String> addColumnWithValueProvider() {
return grid.addColumn(getValueProvider());
}
Expand Down

0 comments on commit 4128359

Please sign in to comment.