Skip to content

Commit eeaabe6

Browse files
authored
feat: Add bindItems to HasDataProvider components (#23761)
1 parent bc37622 commit eeaabe6

4 files changed

Lines changed: 386 additions & 81 deletions

File tree

‎flow-data/src/main/java/com/vaadin/flow/data/binder/HasDataProvider.java‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
package com.vaadin.flow.data.binder;
1717

1818
import java.util.Collection;
19+
import java.util.List;
1920

2021
import com.vaadin.flow.data.provider.DataProvider;
22+
import com.vaadin.flow.data.provider.DataViewUtils;
23+
import com.vaadin.flow.signals.BindingActiveException;
24+
import com.vaadin.flow.signals.Signal;
2125

2226
/**
2327
* A generic interface for listing components that use a data provider for
@@ -50,4 +54,48 @@ default void setItems(Collection<T> items) {
5054
setDataProvider(DataProvider.ofCollection(items));
5155
}
5256

57+
/**
58+
* Binds a signal containing a list of item signals to this component,
59+
* enabling fine-grained reactive updates. This method establishes a
60+
* reactive binding where:
61+
* <ul>
62+
* <li>Changes to the outer signal (list structure changes such as adding or
63+
* removing items) trigger {@link DataProvider#refreshAll()}</li>
64+
* <li>Changes to any inner signal (individual item value changes) trigger
65+
* {@link DataProvider#refreshItem(Object)} for that specific item</li>
66+
* </ul>
67+
* <p>
68+
* The binding is automatically managed based on the component's lifecycle.
69+
* When the component is attached, the effects are activated; when detached,
70+
* they are deactivated.
71+
* <p>
72+
* Example usage:
73+
*
74+
* <pre>
75+
* ListSignal&lt;String&gt; items = new ListSignal&lt;&gt;();
76+
* items.insertLast("Item 1");
77+
* items.insertLast("Item 2");
78+
*
79+
* component.bindItems(items);
80+
*
81+
* // Structural change - triggers refreshAll()
82+
* items.insertLast("Item 3");
83+
*
84+
* // Item value change - triggers refreshItem()
85+
* items.peek().get(0).set("Updated Item 1");
86+
* </pre>
87+
*
88+
* @param itemsSignal
89+
* the signal containing a list of item signals (e.g.,
90+
* ListSignal), not {@code null}
91+
* @throws IllegalArgumentException
92+
* if this is not a Component instance
93+
* @throws BindingActiveException
94+
* if there is already an active items binding
95+
*/
96+
default void bindItems(
97+
Signal<? extends List<? extends Signal<T>>> itemsSignal) {
98+
DataViewUtils.bindItems(this, itemsSignal);
99+
}
100+
53101
}

‎flow-data/src/main/java/com/vaadin/flow/data/provider/DataViewUtils.java‎

Lines changed: 140 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import java.util.List;
2020
import java.util.Objects;
2121
import java.util.Optional;
22-
import java.util.concurrent.atomic.AtomicBoolean;
2322

2423
import com.vaadin.flow.component.Component;
2524
import com.vaadin.flow.component.ComponentUtil;
25+
import com.vaadin.flow.data.binder.HasDataProvider;
2626
import com.vaadin.flow.function.SerializableComparator;
27+
import com.vaadin.flow.function.SerializableConsumer;
2728
import com.vaadin.flow.function.SerializableFunction;
2829
import com.vaadin.flow.function.SerializablePredicate;
2930
import com.vaadin.flow.internal.nodefeature.SignalBindingFeature;
@@ -169,17 +170,16 @@ public static Query getQuery(Component component, boolean withSorting) {
169170
}
170171

171172
/**
172-
* Binds a signal containing a list of item signals to a component, enabling
173-
* fine-grained reactive updates. This is a helper method used by
174-
* {@link HasDataView#bindItems(Signal)}.
173+
* Binds a signal containing a list of item signals to a {@link HasDataView}
174+
* component, enabling fine-grained reactive updates.
175175
*
176176
* @param hasDataView
177-
* the component that implements HasDataView, not {@code null}
177+
* the component that implements HasDataView not {@code null}
178178
* @param itemsSignal
179179
* the signal containing a list of item signals, not {@code null}
180180
* @param dataProviderSetter
181-
* function that sets the data provider and returns a DataView,
182-
* not {@code null}
181+
* the function that sets the generated data provider and returns
182+
* a DataView object, not {@code null}
183183
* @param <T>
184184
* item type
185185
* @param <V>
@@ -194,7 +194,8 @@ public static <T, V extends DataView<T>> V bindItems(
194194
HasDataView<T, ?, V> hasDataView,
195195
Signal<? extends List<? extends Signal<T>>> itemsSignal,
196196
SerializableFunction<List<T>, V> dataProviderSetter) {
197-
Objects.requireNonNull(hasDataView, "HasDataView cannot be null");
197+
Objects.requireNonNull(hasDataView,
198+
"HasDataView component cannot be null");
198199
Objects.requireNonNull(itemsSignal, "Items signal cannot be null");
199200
Objects.requireNonNull(dataProviderSetter,
200201
"Data provider setter cannot be null");
@@ -204,6 +205,71 @@ public static <T, V extends DataView<T>> V bindItems(
204205
"bindItems can only be used with Component instances");
205206
}
206207

208+
return bindItemsInternal(component, itemsSignal, dataProviderSetter);
209+
}
210+
211+
/**
212+
* Binds a signal containing a list of item signals to a
213+
* {@link HasDataProvider} component, enabling fine-grained reactive
214+
* updates.
215+
*
216+
* @param hasDataProvider
217+
* the component that implements HasDataProvider, not
218+
* {@code null}
219+
* @param itemsSignal
220+
* the signal containing a list of item signals, not {@code null}
221+
* @param <T>
222+
* item type
223+
* @throws IllegalArgumentException
224+
* if hasDataProvider is not a Component instance
225+
* @throws BindingActiveException
226+
* if there is already an active items binding
227+
*/
228+
public static <T> void bindItems(HasDataProvider<T> hasDataProvider,
229+
Signal<? extends List<? extends Signal<T>>> itemsSignal) {
230+
Objects.requireNonNull(hasDataProvider,
231+
"HasDataProvider component cannot be null");
232+
Objects.requireNonNull(itemsSignal, "Items signal cannot be null");
233+
234+
if (!(hasDataProvider instanceof Component component)) {
235+
throw new IllegalArgumentException(
236+
"bindItems can only be used with Component instances");
237+
}
238+
239+
bindItemsInternal(component, itemsSignal, items -> {
240+
ListDataProvider<T> dataProvider = DataProvider.ofCollection(items);
241+
hasDataProvider.setDataProvider(dataProvider);
242+
// a dummy implementation of data view for just to delegate items
243+
// refresh to the backed data provider
244+
return new AbstractListDataView<T>(() -> dataProvider, component,
245+
(filter, comparator) -> {
246+
}) {
247+
};
248+
});
249+
}
250+
251+
/**
252+
* Internal implementation for binding items to a component with reactive
253+
* updates.
254+
*
255+
* @param component
256+
* the component to bind to
257+
* @param itemsSignal
258+
* the signal containing a list of item signals
259+
* @param dataProviderSetter
260+
* function that sets the data provider and returns a DataView
261+
* @param <T>
262+
* item type
263+
* @param <V>
264+
* DataView type
265+
* @return the DataView providing access to the items
266+
* @throws BindingActiveException
267+
* if there is already an active items binding
268+
*/
269+
private static <T, V extends DataView<T>> V bindItemsInternal(
270+
Component component,
271+
Signal<? extends List<? extends Signal<T>>> itemsSignal,
272+
SerializableFunction<List<T>, V> dataProviderSetter) {
207273
// Check if there's already an active binding
208274
SignalBindingFeature bindingFeature = component.getElement().getNode()
209275
.getFeature(SignalBindingFeature.class);
@@ -219,6 +285,36 @@ public static <T, V extends DataView<T>> V bindItems(
219285
// Create and set the data provider using the provided setter
220286
V dataView = dataProviderSetter.apply(backingList);
221287

288+
setupItemsEffect(component, itemsSignal, backingList,
289+
dataView::refreshAll, dataView::refreshItem);
290+
291+
// Store the binding in SignalBindingFeature to track active binding
292+
bindingFeature.setBinding(SignalBindingFeature.ITEMS, itemsSignal);
293+
294+
return dataView;
295+
}
296+
297+
/**
298+
* Internal implementation for setting up the effect to track changes in the
299+
* items signal.
300+
*
301+
* @param component
302+
* the component to bind to
303+
* @param itemsSignal
304+
* the signal containing a list of item signals
305+
* @param backingList
306+
* the backing list to update
307+
* @param refreshAll
308+
* callback to refresh all items
309+
* @param refreshItem
310+
* callback to refresh a single item
311+
* @param <T>
312+
* item type
313+
*/
314+
private static <T> void setupItemsEffect(Component component,
315+
Signal<? extends List<? extends Signal<T>>> itemsSignal,
316+
List<T> backingList, Runnable refreshAll,
317+
SerializableConsumer<T> refreshItem) {
222318
// List to store inner effect registrations
223319
List<Registration> innerEffectRegistrations = new ArrayList<>();
224320

@@ -235,69 +331,16 @@ public static <T, V extends DataView<T>> V bindItems(
235331
updateBackingList(currentSignals, backingList);
236332

237333
// Refresh all data
238-
dataView.refreshAll();
334+
refreshAll.run();
239335

240336
// Set up new inner effects for each signal
241-
createInnerEffects(component, currentSignals, backingList, dataView,
242-
innerEffectRegistrations);
337+
for (int i = 0; i < currentSignals.size(); i++) {
338+
Signal<T> itemSignal = currentSignals.get(i);
339+
Registration innerEffect = createItemEffect(component,
340+
itemSignal, i, backingList, refreshItem);
341+
innerEffectRegistrations.add(innerEffect);
342+
}
243343
});
244-
245-
// Store the binding in SignalBindingFeature to track active binding
246-
bindingFeature.setBinding(SignalBindingFeature.ITEMS, itemsSignal);
247-
248-
return dataView;
249-
}
250-
251-
/**
252-
* Updates the backing list with current values from the item signals.
253-
*
254-
* @param currentSignals
255-
* list of signals containing item values
256-
* @param backingList
257-
* the backing list to update
258-
* @param <T>
259-
* item type
260-
*/
261-
private static <T> void updateBackingList(
262-
List<? extends Signal<T>> currentSignals, List<T> backingList) {
263-
backingList.clear();
264-
for (Signal<T> signal : currentSignals) {
265-
T value = signal.peek();
266-
backingList.add(value);
267-
}
268-
}
269-
270-
/**
271-
* Creates inner effects for each item signal to track individual item
272-
* changes.
273-
*
274-
* @param component
275-
* the component to bind effects to
276-
* @param currentSignals
277-
* list of signals containing item values
278-
* @param backingList
279-
* the backing list to update
280-
* @param dataView
281-
* the data view to refresh
282-
* @param innerEffectRegistrations
283-
* list to store the effect registrations
284-
* @param <T>
285-
* item type
286-
* @param <V>
287-
* DataView type
288-
*/
289-
private static <T, V extends DataView<T>> void createInnerEffects(
290-
Component component, List<? extends Signal<T>> currentSignals,
291-
List<T> backingList, V dataView,
292-
List<Registration> innerEffectRegistrations) {
293-
for (int i = 0; i < currentSignals.size(); i++) {
294-
Signal<T> itemSignal = currentSignals.get(i);
295-
296-
Registration innerEffect = createItemEffect(component, itemSignal,
297-
i, backingList, dataView);
298-
299-
innerEffectRegistrations.add(innerEffect);
300-
}
301344
}
302345

303346
/**
@@ -313,29 +356,45 @@ private static <T, V extends DataView<T>> void createInnerEffects(
313356
* the index of the item in the backing list
314357
* @param backingList
315358
* the backing list to update
316-
* @param dataView
317-
* the data view to refresh
359+
* @param refreshItem
360+
* callback to refresh the item
318361
* @param <T>
319362
* item type
320-
* @param <V>
321-
* DataView type
322363
* @return the registration for the effect
323364
*/
324-
private static <T, V extends DataView<T>> Registration createItemEffect(
325-
Component component, Signal<T> itemSignal, int index,
326-
List<T> backingList, V dataView) {
327-
// Track whether this is the first run of the effect
328-
AtomicBoolean isFirstRun = new AtomicBoolean(true);
329-
return Signal.effect(component, () -> {
365+
private static <T> Registration createItemEffect(Component component,
366+
Signal<T> itemSignal, int index, List<T> backingList,
367+
SerializableConsumer<T> refreshItem) {
368+
return Signal.effect(component, context -> {
369+
// register a dependency on the initial run
330370
T newValue = itemSignal.get();
331371

332372
// Skip refreshItem on the first run since refreshAll was just
333373
// called
334-
if (!isFirstRun.get()) {
374+
if (!context.isInitialRun()) {
335375
backingList.set(index, newValue);
336-
dataView.refreshItem(newValue);
376+
refreshItem.accept(newValue);
337377
}
338-
isFirstRun.set(false);
339378
});
340379
}
380+
381+
/**
382+
* Updates the backing list with current values from the item signals.
383+
*
384+
* @param currentSignals
385+
* list of signals containing item values
386+
* @param backingList
387+
* the backing list to update
388+
* @param <T>
389+
* item type
390+
*/
391+
private static <T> void updateBackingList(
392+
List<? extends Signal<T>> currentSignals, List<T> backingList) {
393+
backingList.clear();
394+
for (Signal<T> signal : currentSignals) {
395+
T value = signal.peek();
396+
backingList.add(value);
397+
}
398+
}
399+
341400
}

0 commit comments

Comments
 (0)