Skip to content

Commit

Permalink
force update. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek authored Oct 17, 2024
1 parent 4a76f77 commit aa4a4e0
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface ContextElementRender extends ContextRender {
@NotNull
Element element();

boolean forced();
}
3 changes: 3 additions & 0 deletions common/src/main/java/net/infumia/frame/element/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ public interface Element {

@NotNull
CompletableFuture<ConsumerService.State> update();

@NotNull
CompletableFuture<ConsumerService.State> forceUpdate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

public interface PipelineExecutorElement {
@NotNull
CompletableFuture<ConsumerService.State> executeRender(@NotNull ContextRender context);
CompletableFuture<ConsumerService.State> executeRender(
@NotNull ContextRender context,
boolean forced
);

@NotNull
CompletableFuture<ConsumerService.State> executeUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
public class ContextElementRenderImpl extends ContextRenderImpl implements ContextElementRender {

private final Element element;
private final boolean forced;

public ContextElementRenderImpl(
@NotNull final ContextRender context,
@NotNull final Element element
@NotNull final Element element,
final boolean forced
) {
super(context);
this.element = element;
this.forced = forced;
}

public ContextElementRenderImpl(@NotNull final ContextElementRender context) {
this(context, context.element(), context.forced());
}

@NotNull
Expand All @@ -23,7 +30,8 @@ public Element element() {
return this.element;
}

public ContextElementRenderImpl(@NotNull final ContextElementRender context) {
this(context, context.element());
@Override
public boolean forced() {
return this.forced;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CompletableFuture<ConsumerService.State> handleRender(
final ContextElementRender context = ctx.context();
final ElementItemRich element = (ElementItemRich) context.element();
if (element.shouldRender(context)) {
this.forceRender(element, context);
this.renderInternally(element, context);
return CompletableFuture.completedFuture(ConsumerService.State.CONTINUE);
}
element.visible(false);
Expand Down Expand Up @@ -80,7 +80,7 @@ public CompletableFuture<ConsumerService.State> handleUpdate(
if (context.cancelled()) {
return CompletableFuture.completedFuture(ConsumerService.State.CONTINUE);
}
return element.pipelines().executeRender(context);
return element.pipelines().executeRender(context, context.forced());
}

@NotNull
Expand All @@ -96,7 +96,7 @@ private CompletableFuture<ConsumerService.State> checkOverlapping(
final ElementRich overlapping = overlappingOptional.get();
return overlapping
.pipelines()
.executeRender(context)
.executeRender(context, false)
.thenCompose(__ -> {
if (overlapping.visible()) {
return CompletableFuture.completedFuture(ConsumerService.State.CONTINUE);
Expand Down Expand Up @@ -150,7 +150,7 @@ private Optional<ElementRich> findOverlappingElement(
return Optional.empty();
}

private void forceRender(
private void renderInternally(
@NotNull final ElementItemRich element,
@NotNull final ContextElementRender delegate
) {
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/net/infumia/frame/element/ElementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public CompletableFuture<ConsumerService.State> update() {
throw new UnsupportedOperationException("This element cannot be updated!");
}

@NotNull
@Override
public CompletableFuture<ConsumerService.State> forceUpdate() {
throw new UnsupportedOperationException("This element cannot be updated!");
}

@Override
public String key() {
return this.key;
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/net/infumia/frame/element/ElementItemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ public CompletableFuture<ConsumerService.State> update() {
return this.pipelines.executeUpdate((ContextRender) this.parent, false);
}

@NotNull
@Override
public CompletableFuture<ConsumerService.State> forceUpdate() {
Preconditions.state(
this.parent instanceof ContextRender,
"You cannot update the element '%s' when the parent is not a ContextRender!",
this
);
return this.pipelines.executeUpdate((ContextRender) this.parent, true);
}

@NotNull
@Override
public PipelineExecutorElement pipelines() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public CompletableFuture<ConsumerService.State> handleRender(
) {
final ContextElementRender context = ctx.context();
final ElementPaginationRich<?> pagination = (ElementPaginationRich<?>) context.element();
if (pagination.initialized() && !pagination.pageWasChanged()) {
if (pagination.initialized() && !pagination.pageWasChanged() && !context.forced()) {
pagination.visible(true);
return this.renderChild(context, pagination);
}
if (!pagination.initialized()) {
pagination.updatePageSize(context);
}
return pagination
.loadCurrentPage(context)
.loadCurrentPage(context, context.forced())
.thenCompose(__ -> {
pagination.visible(true);
pagination.initialized(true);
Expand Down Expand Up @@ -109,13 +109,13 @@ public CompletableFuture<ConsumerService.State> handleUpdate(
) {
final ContextElementUpdate context = ctx.context();
final ElementPaginationRich<?> pagination = (ElementPaginationRich<?>) context.element();
if (pagination.pageWasChanged()) {
if (pagination.pageWasChanged() || context.forced()) {
return pagination
.pipelines()
.executeClear(context)
.thenCompose(__ -> {
pagination.clearElements();
return pagination.pipelines().executeRender(context);
return pagination.pipelines().executeRender(context, context.forced());
})
.thenApply(__ -> {
pagination.pageWasChanged(false);
Expand All @@ -128,8 +128,12 @@ public CompletableFuture<ConsumerService.State> handleUpdate(
final List<Element> elements = pagination.elements();
final CompletableFuture<?>[] futures = new CompletableFuture<?>[elements.size()];
for (int i = 0; i < futures.length; i++) {
futures[i] = ((ElementRich) elements.get(i)).pipelines()
.executeUpdate(context, context.forced());
final ElementRich element = (ElementRich) elements.get(i);
if (context.forced()) {
futures[i] = element.forceUpdate();
} else {
futures[i] = element.update();
}
}
return CompletableFuture.allOf(futures).thenApply(__ -> ConsumerService.State.CONTINUE);
}
Expand All @@ -143,7 +147,7 @@ private CompletableFuture<ConsumerService.State> renderChild(
final CompletableFuture<?>[] futures = new CompletableFuture[elements.size()];
for (int i = 0; i < futures.length; i++) {
final ElementRich element = (ElementRich) elements.get(i);
futures[i] = element.pipelines().executeRender(context);
futures[i] = element.pipelines().executeRender(context, context.forced());
}
return CompletableFuture.allOf(futures).thenApply(__ -> ConsumerService.State.CONTINUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import net.infumia.frame.service.ConsumerService;
import net.infumia.frame.slot.LayoutSlot;
import net.infumia.frame.state.State;
import net.infumia.frame.state.StateRich;
import net.infumia.frame.state.pagination.ElementConfigurer;
import net.infumia.frame.state.pagination.StatePagination;
import net.infumia.frame.util.Preconditions;
Expand Down Expand Up @@ -122,8 +121,11 @@ public void updatePageSize(@NotNull final ContextRender context) {

@NotNull
@Override
public CompletableFuture<?> loadCurrentPage(@NotNull final ContextRender context) {
return this.loadSourceForTheCurrentPage(context).thenAccept(pageContents -> {
public CompletableFuture<?> loadCurrentPage(
@NotNull final ContextRender context,
final boolean forced
) {
return this.loadSourceForTheCurrentPage(context, forced).thenAccept(pageContents -> {
if (pageContents.isEmpty()) {
return;
}
Expand Down Expand Up @@ -225,7 +227,7 @@ public void switchTo(final int pageIndex) {
this.onPageSwitch.accept(host, this);
}
CompletableFutureExtensions.logError(
this.pipelines.executeUpdate(host, false),
this.update(),
this.parent().frame().logger(),
"An error occurred while updating the pagination '%s'.",
this
Expand Down Expand Up @@ -333,6 +335,17 @@ public CompletableFuture<ConsumerService.State> update() {
return this.pipelines.executeUpdate((ContextRender) this.parent(), false);
}

@NotNull
@Override
public CompletableFuture<ConsumerService.State> forceUpdate() {
Preconditions.state(
this.parent() instanceof ContextRender,
"You cannot update the element '%s' when the parent is not a ContextRender!",
this
);
return this.pipelines.executeUpdate((ContextRender) this.parent(), true);
}

@NotNull
@Override
public PipelineExecutorElement pipelines() {
Expand Down Expand Up @@ -389,15 +402,15 @@ private void addComponentsForLayeredPagination(

@NotNull
private CompletableFuture<List<T>> loadSourceForTheCurrentPage(
@NotNull final ContextBase context
@NotNull final ContextBase context,
final boolean forced
) {
final boolean isLazy = this.sourceProvider.lazy();
final boolean reuseLazy = isLazy && this.initialized;
final boolean force = false; // TODO: portlek, Implement this.
if (
(this.sourceProvider.provided() || reuseLazy) &&
!this.sourceProvider.computed() &&
!force
!forced
) {
final List<T> currentSource = Preconditions.stateNotNull(
this.currentSource,
Expand All @@ -419,29 +432,25 @@ private CompletableFuture<List<T>> loadSourceForTheCurrentPage(
}

this.loading = true;
return ((StateRich<ElementPagination>) this.associated).manualUpdateWait(
context
).thenCompose(__ -> {
if (this.sourceFactory == null) {
return CompletableFuture.completedFuture(Collections.emptyList());
if (this.sourceFactory == null) {
return CompletableFuture.completedFuture(Collections.emptyList());
}
return this.sourceFactory.apply(context).thenApply(result -> {
this.currentSource = result;
this.pageCount = this.calculatePagesCount(result);
final int previousPage = Math.min(this.currentPageIndex, this.pageCount - 1);
this.loading = false;
if (previousPage != this.currentPageIndex) {
this.switchTo(previousPage);
}
return this.sourceFactory.apply(context).thenCompose(result -> {
this.currentSource = result;
this.pageCount = this.calculatePagesCount(result);
this.loading = false;
return ((StateRich<ElementPagination>) this.associated).manualUpdateWait(
context
).thenApply(value ->
!isLazy
? result
: ElementPaginationImpl.splitSourceForPage(
this.currentPageIndex,
this.pageSize(),
this.pageCount,
result
)
);
});
return isLazy
? ElementPaginationImpl.splitSourceForPage(
this.currentPageIndex,
this.pageSize(),
this.pageCount,
result
)
: result;
});
}

Expand Down Expand Up @@ -488,7 +497,7 @@ private static <T> List<T> splitSourceForPage(
if (src.size() <= pageSize) {
return new ArrayList<>(src);
}
if (index < 0 || (pagesCount > 0 && index > pagesCount)) {
if (index < 0 || (pagesCount > 0 && index >= pagesCount)) {
throw new IndexOutOfBoundsException(
String.format(
"Page index must be between the range of 0 and %d. Given: %d",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface ElementPaginationRich<T>
void updatePageSize(@NotNull ContextRender context);

@NotNull
CompletableFuture<?> loadCurrentPage(@NotNull ContextRender context);
CompletableFuture<?> loadCurrentPage(@NotNull ContextRender context, boolean forced);

@NotNull
Collection<Element> modifiableElements();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public PipelineExecutorElementImpl(@NotNull final ElementRich element) {
@NotNull
@Override
public CompletableFuture<ConsumerService.State> executeRender(
@NotNull final ContextRender context
@NotNull final ContextRender context,
final boolean forced
) {
return this.pipelines.render()
.completeWith(
new PipelineContextElements.Render(
new ContextElementRenderImpl(context, this.element)
new ContextElementRenderImpl(context, this.element, forced)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.concurrent.CompletableFuture;
import net.infumia.frame.context.element.ContextElementClick;
import net.infumia.frame.element.Element;
import net.infumia.frame.element.ElementRich;
import net.infumia.frame.pipeline.PipelineServiceConsumer;
import net.infumia.frame.pipeline.context.PipelineContextElement;
import org.jetbrains.annotations.NotNull;
Expand All @@ -28,7 +27,7 @@ public CompletableFuture<State> handle(@NotNull final PipelineContextElement.Cli
final ContextElementClick context = ctx.context();
final Element element = context.element();
if (element.updateOnClick()) {
return ((ElementRich) element).pipelines().executeUpdate(context, false);
return element.update();
}
return CompletableFuture.completedFuture(State.CONTINUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public CompletableFuture<State> handle(@NotNull final PipelineContextRender.Firs
final CompletableFuture<State>[] futures = new CompletableFuture[size];
for (int i = size; i > 0; i--) {
final ElementRich element = (ElementRich) elements.get(i - 1);
futures[size - i] = element.pipelines().executeRender(context);
futures[size - i] = element.pipelines().executeRender(context, false);
}
return CompletableFuture.allOf(futures).thenApply(unused -> State.CONTINUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static void updateOnStateAccess(
for (final net.infumia.frame.state.State<?> state : states) {
state.watchAccess(context, update ->
CompletableFutureExtensions.logError(
element.pipelines().executeUpdate(context, false),
element.update(),
context.frame().logger(),
"An error occurred while updating element '%s' due to state '%s' access!",
element.key(),
Expand All @@ -78,7 +78,7 @@ private static void updateOnStateChange(
for (final net.infumia.frame.state.State<?> state : states) {
state.watchUpdate(context, update ->
CompletableFutureExtensions.logError(
element.pipelines().executeUpdate(context, false),
element.update(),
context.frame().logger(),
"An error occurred while updating element '%s' due to state '%s' change!",
element.key(),
Expand Down

0 comments on commit aa4a4e0

Please sign in to comment.