Class AsyncItemsController<C,S>
java.lang.Object
org.patternfly.async.AsyncItemsController<C,S>
- Type Parameters:
C- the component type that owns the async itemsS- the type of items being loaded
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif an async function has been registered and the status isAsyncStatus.pending(i.e., items have not yet been loaded or have been reset).load(C component, Consumer<S> onItem, Runnable onEmpty, Consumer<Object> onError, Runnable onBefore, Runnable onAfter) Loads items if status isAsyncStatus.pendingand an async function is registered.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.voidResets the controller toAsyncStatus.pending, allowing a subsequentload(C, Consumer, Runnable, Consumer, Runnable, Runnable)call.voidset(AsyncItems<C, S> asyncItems) Registers the async items function and sets status toAsyncStatus.pending.status()
-
Constructor Details
-
AsyncItemsController
public AsyncItemsController()
-
-
Method Details
-
set
Registers the async items function and sets status toAsyncStatus.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 isAsyncStatus.pendingand 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 anotherload(C, Consumer, Runnable, Consumer, Runnable, Runnable)was called in the meantime), the result is silently discarded.- Parameters:
component- the component instance passed to theAsyncItemsfunctiononItem- called for each item in the resultonEmpty- called when the result is empty (may benull)onError- called when the promise rejects (may benull)onBefore- called before the async fetch starts, e.g., to show a loading indicator (may benull)onAfter- called after items have been processed, e.g., to remove the loading indicator (may benull)- Returns:
- a promise that resolves with the loaded items, or an empty list if skipped or stale
-
reset
Resets the controller toAsyncStatus.pending, allowing a subsequentload(C, Consumer, Runnable, Consumer, Runnable, Runnable)call. This works even when status is alreadypending— it increments the generation counter to invalidate any in-flight load.- Parameters:
onClear- called to clear existing items from the DOM (may benull)
-
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 theAsyncItemsfunctiononItem- called for each item in the resultonEmpty- called when the result is empty (may benull)onError- called when the promise rejects (may benull)onBefore- called before the async fetch starts, e.g., to show a loading indicator (may benull)onAfter- called after items have been processed, e.g., to remove the loading indicator (may benull)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
-
hasAsyncItems
public boolean hasAsyncItems()Returnstrueif an async function has been registered and the status isAsyncStatus.pending(i.e., items have not yet been loaded or have been reset).
-