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(boolean checkDetached)
      Creates a controller with explicit control over detached-component handling.

      When checkDetached is true, async results are silently discarded if the component has been detached from the DOM between the time the request was made and the time the promise resolved. This prevents errors in components like menus, where a closed menu's items would try to look up a parent component via closest() on a detached element tree.

      Pass false for components that intentionally load items while detached. For example, TreeViewItem keeps its children element (<ul>) detached until the item is expanded. During programmatic loading of a deep path, items are loaded into detached child elements — the detached check would incorrectly discard those items.

      Parameters:
      checkDetached - true to discard results for detached components, false to skip the check
  • Method Details

    • set

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

      public elemental2.promise.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 elemental2.promise.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).