Class AsyncItemsController<C,S>

java.lang.Object
org.patternfly.async.AsyncItemsController<C,S>
Type Parameters:
C - the component type that owns the async items
S - the type of items being loaded

public class AsyncItemsController<C,S> extends Object
A delegate that manages the async loading state machine for components implementing HasAsyncItems.

Handles status transitions, a generation counter to discard stale responses from concurrent loads, and reset-during-pending. Components provide callbacks for DOM-specific operations (adding items, clearing, showing errors).

  • Constructor Details

    • AsyncItemsController

      public AsyncItemsController()
  • Method Details

    • set

      public void set(AsyncItems<C,S> asyncItems)
      Registers the async items function and sets status to AsyncStatus.pending.
    • load

      public Promise<Iterable<S>> load(C component, Consumer<S> onItem, Runnable onEmpty, Consumer<Object> onError, Runnable onBefore, Runnable onAfter)
      Loads items if status is AsyncStatus.pending and an async function is registered.

      Increments the generation counter before starting the load. When the promise resolves, the generation is checked — if it no longer matches (because reset(Runnable) or another load(C, Consumer, Runnable, Consumer, Runnable, Runnable) was called in the meantime), the result is silently discarded.

      Parameters:
      component - the component instance passed to the AsyncItems function
      onItem - called for each item in the result
      onEmpty - called when the result is empty (may be null)
      onError - called when the promise rejects (may be null)
      onBefore - called before the async fetch starts, e.g., to show a loading indicator (may be null)
      onAfter - called after items have been processed, e.g., to remove the loading indicator (may be null)
      Returns:
      a promise that resolves with the loaded items, or an empty list if skipped or stale
    • reset

      public void reset(Runnable onClear)
      Resets the controller to AsyncStatus.pending, allowing a subsequent load(C, Consumer, Runnable, Consumer, Runnable, Runnable) call. This works even when status is already pending — it increments the generation counter to invalidate any in-flight load.
      Parameters:
      onClear - called to clear existing items from the DOM (may be null)
    • refresh

      public Promise<Iterable<S>> refresh(C component, Consumer<S> onItem, Runnable onEmpty, Consumer<Object> onError, Runnable onBefore, Runnable onAfter, Runnable onClear)
      Re-fetches items while keeping old items visible, then swaps them after the new items arrive. This avoids the visual flash caused by clearing items before the async fetch completes.
      Parameters:
      component - the component instance passed to the AsyncItems function
      onItem - called for each item in the result
      onEmpty - called when the result is empty (may be null)
      onError - called when the promise rejects (may be null)
      onBefore - called before the async fetch starts, e.g., to show a loading indicator (may be null)
      onAfter - called after items have been processed, e.g., to remove the loading indicator (may be null)
      onClear - called to clear existing items from the DOM before adding new ones
      Returns:
      a promise that resolves with the loaded items, or an empty list if skipped or stale
    • status

      public AsyncStatus status()
    • hasAsyncItems

      public boolean hasAsyncItems()
      Returns true if an async function has been registered and the status is AsyncStatus.pending (i.e., items have not yet been loaded or have been reset).