1919import java .util .List ;
2020import java .util .Objects ;
2121import java .util .Optional ;
22- import java .util .concurrent .atomic .AtomicBoolean ;
2322
2423import com .vaadin .flow .component .Component ;
2524import com .vaadin .flow .component .ComponentUtil ;
25+ import com .vaadin .flow .data .binder .HasDataProvider ;
2626import com .vaadin .flow .function .SerializableComparator ;
27+ import com .vaadin .flow .function .SerializableConsumer ;
2728import com .vaadin .flow .function .SerializableFunction ;
2829import com .vaadin .flow .function .SerializablePredicate ;
2930import 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