Class AsyncItemsController<C,S>
- Type Parameters:
C- the component type that owns the async itemsS- the type of items being loaded
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
ConstructorsConstructorDescriptionAsyncItemsController(boolean checkDetached) Creates a controller with explicit control over detached-component handling. -
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(boolean checkDetached) Creates a controller with explicit control over detached-component handling.When
checkDetachedistrue, 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 viaclosest()on a detached element tree.Pass
falsefor components that intentionally load items while detached. For example,TreeViewItemkeeps 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-trueto discard results for detached components,falseto skip the check
-
-
Method Details
-
set
Registers the async items function and sets status toAsyncStatus.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 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 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 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).
-