From aa4a4e0e539d44e119fb193c312ef368b8d3be22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Thu, 17 Oct 2024 21:49:51 +0300 Subject: [PATCH] force update. (#15) --- .../context/element/ContextElementRender.java | 2 + .../net/infumia/frame/element/Element.java | 3 + .../executor/PipelineExecutorElement.java | 5 +- .../element/ContextElementRenderImpl.java | 14 +++- .../element/ElementEventHandlerItem.java | 8 +-- .../infumia/frame/element/ElementImpl.java | 6 ++ .../frame/element/ElementItemImpl.java | 11 +++ .../ElementEventHandlerPagination.java | 18 +++-- .../pagination/ElementPaginationImpl.java | 69 +++++++++++-------- .../pagination/ElementPaginationRich.java | 2 +- .../executor/PipelineExecutorElementImpl.java | 5 +- .../element/ServiceClickUpdateOnClick.java | 3 +- .../service/render/ServiceFirstRender.java | 2 +- .../render/ServiceFirstRenderWatchState.java | 4 +- 14 files changed, 99 insertions(+), 53 deletions(-) diff --git a/common/src/main/java/net/infumia/frame/context/element/ContextElementRender.java b/common/src/main/java/net/infumia/frame/context/element/ContextElementRender.java index c45e840..63d7198 100644 --- a/common/src/main/java/net/infumia/frame/context/element/ContextElementRender.java +++ b/common/src/main/java/net/infumia/frame/context/element/ContextElementRender.java @@ -7,4 +7,6 @@ public interface ContextElementRender extends ContextRender { @NotNull Element element(); + + boolean forced(); } diff --git a/common/src/main/java/net/infumia/frame/element/Element.java b/common/src/main/java/net/infumia/frame/element/Element.java index 5d22de0..23e7602 100644 --- a/common/src/main/java/net/infumia/frame/element/Element.java +++ b/common/src/main/java/net/infumia/frame/element/Element.java @@ -27,4 +27,7 @@ public interface Element { @NotNull CompletableFuture update(); + + @NotNull + CompletableFuture forceUpdate(); } diff --git a/common/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorElement.java b/common/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorElement.java index 07555b8..81761ad 100644 --- a/common/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorElement.java +++ b/common/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorElement.java @@ -10,7 +10,10 @@ public interface PipelineExecutorElement { @NotNull - CompletableFuture executeRender(@NotNull ContextRender context); + CompletableFuture executeRender( + @NotNull ContextRender context, + boolean forced + ); @NotNull CompletableFuture executeUpdate( diff --git a/core/src/main/java/net/infumia/frame/context/element/ContextElementRenderImpl.java b/core/src/main/java/net/infumia/frame/context/element/ContextElementRenderImpl.java index a2892f1..23a6ee2 100644 --- a/core/src/main/java/net/infumia/frame/context/element/ContextElementRenderImpl.java +++ b/core/src/main/java/net/infumia/frame/context/element/ContextElementRenderImpl.java @@ -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 @@ -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; } } diff --git a/core/src/main/java/net/infumia/frame/element/ElementEventHandlerItem.java b/core/src/main/java/net/infumia/frame/element/ElementEventHandlerItem.java index 2d41054..ad41a2b 100644 --- a/core/src/main/java/net/infumia/frame/element/ElementEventHandlerItem.java +++ b/core/src/main/java/net/infumia/frame/element/ElementEventHandlerItem.java @@ -33,7 +33,7 @@ public CompletableFuture 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); @@ -80,7 +80,7 @@ public CompletableFuture handleUpdate( if (context.cancelled()) { return CompletableFuture.completedFuture(ConsumerService.State.CONTINUE); } - return element.pipelines().executeRender(context); + return element.pipelines().executeRender(context, context.forced()); } @NotNull @@ -96,7 +96,7 @@ private CompletableFuture checkOverlapping( final ElementRich overlapping = overlappingOptional.get(); return overlapping .pipelines() - .executeRender(context) + .executeRender(context, false) .thenCompose(__ -> { if (overlapping.visible()) { return CompletableFuture.completedFuture(ConsumerService.State.CONTINUE); @@ -150,7 +150,7 @@ private Optional findOverlappingElement( return Optional.empty(); } - private void forceRender( + private void renderInternally( @NotNull final ElementItemRich element, @NotNull final ContextElementRender delegate ) { diff --git a/core/src/main/java/net/infumia/frame/element/ElementImpl.java b/core/src/main/java/net/infumia/frame/element/ElementImpl.java index 1b0e02c..af01c8b 100644 --- a/core/src/main/java/net/infumia/frame/element/ElementImpl.java +++ b/core/src/main/java/net/infumia/frame/element/ElementImpl.java @@ -122,6 +122,12 @@ public CompletableFuture update() { throw new UnsupportedOperationException("This element cannot be updated!"); } + @NotNull + @Override + public CompletableFuture forceUpdate() { + throw new UnsupportedOperationException("This element cannot be updated!"); + } + @Override public String key() { return this.key; diff --git a/core/src/main/java/net/infumia/frame/element/ElementItemImpl.java b/core/src/main/java/net/infumia/frame/element/ElementItemImpl.java index 1dd6a87..8017ce4 100644 --- a/core/src/main/java/net/infumia/frame/element/ElementItemImpl.java +++ b/core/src/main/java/net/infumia/frame/element/ElementItemImpl.java @@ -102,6 +102,17 @@ public CompletableFuture update() { return this.pipelines.executeUpdate((ContextRender) this.parent, false); } + @NotNull + @Override + public CompletableFuture 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() { diff --git a/core/src/main/java/net/infumia/frame/element/pagination/ElementEventHandlerPagination.java b/core/src/main/java/net/infumia/frame/element/pagination/ElementEventHandlerPagination.java index da9f0ef..399710f 100644 --- a/core/src/main/java/net/infumia/frame/element/pagination/ElementEventHandlerPagination.java +++ b/core/src/main/java/net/infumia/frame/element/pagination/ElementEventHandlerPagination.java @@ -30,7 +30,7 @@ public CompletableFuture 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); } @@ -38,7 +38,7 @@ public CompletableFuture handleRender( pagination.updatePageSize(context); } return pagination - .loadCurrentPage(context) + .loadCurrentPage(context, context.forced()) .thenCompose(__ -> { pagination.visible(true); pagination.initialized(true); @@ -109,13 +109,13 @@ public CompletableFuture 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); @@ -128,8 +128,12 @@ public CompletableFuture handleUpdate( final List 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); } @@ -143,7 +147,7 @@ private CompletableFuture 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); } diff --git a/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationImpl.java b/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationImpl.java index 16c83b0..e14bd95 100644 --- a/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationImpl.java +++ b/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationImpl.java @@ -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; @@ -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; } @@ -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 @@ -333,6 +335,17 @@ public CompletableFuture update() { return this.pipelines.executeUpdate((ContextRender) this.parent(), false); } + @NotNull + @Override + public CompletableFuture 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() { @@ -389,15 +402,15 @@ private void addComponentsForLayeredPagination( @NotNull private CompletableFuture> 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 currentSource = Preconditions.stateNotNull( this.currentSource, @@ -419,29 +432,25 @@ private CompletableFuture> loadSourceForTheCurrentPage( } this.loading = true; - return ((StateRich) 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) 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; }); } @@ -488,7 +497,7 @@ private static List 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", diff --git a/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationRich.java b/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationRich.java index 28876f2..c95e9ca 100644 --- a/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationRich.java +++ b/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationRich.java @@ -25,7 +25,7 @@ public interface ElementPaginationRich void updatePageSize(@NotNull ContextRender context); @NotNull - CompletableFuture loadCurrentPage(@NotNull ContextRender context); + CompletableFuture loadCurrentPage(@NotNull ContextRender context, boolean forced); @NotNull Collection modifiableElements(); diff --git a/core/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorElementImpl.java b/core/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorElementImpl.java index e12d229..4bdfefc 100644 --- a/core/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorElementImpl.java +++ b/core/src/main/java/net/infumia/frame/pipeline/executor/PipelineExecutorElementImpl.java @@ -27,12 +27,13 @@ public PipelineExecutorElementImpl(@NotNull final ElementRich element) { @NotNull @Override public CompletableFuture 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) ) ); } diff --git a/core/src/main/java/net/infumia/frame/pipeline/service/element/ServiceClickUpdateOnClick.java b/core/src/main/java/net/infumia/frame/pipeline/service/element/ServiceClickUpdateOnClick.java index 776b6f0..9c92a42 100644 --- a/core/src/main/java/net/infumia/frame/pipeline/service/element/ServiceClickUpdateOnClick.java +++ b/core/src/main/java/net/infumia/frame/pipeline/service/element/ServiceClickUpdateOnClick.java @@ -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; @@ -28,7 +27,7 @@ public CompletableFuture 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); } diff --git a/core/src/main/java/net/infumia/frame/pipeline/service/render/ServiceFirstRender.java b/core/src/main/java/net/infumia/frame/pipeline/service/render/ServiceFirstRender.java index e9a2cea..d5c1e29 100644 --- a/core/src/main/java/net/infumia/frame/pipeline/service/render/ServiceFirstRender.java +++ b/core/src/main/java/net/infumia/frame/pipeline/service/render/ServiceFirstRender.java @@ -36,7 +36,7 @@ public CompletableFuture handle(@NotNull final PipelineContextRender.Firs final CompletableFuture[] 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); } diff --git a/core/src/main/java/net/infumia/frame/pipeline/service/render/ServiceFirstRenderWatchState.java b/core/src/main/java/net/infumia/frame/pipeline/service/render/ServiceFirstRenderWatchState.java index d74069e..49a89fe 100644 --- a/core/src/main/java/net/infumia/frame/pipeline/service/render/ServiceFirstRenderWatchState.java +++ b/core/src/main/java/net/infumia/frame/pipeline/service/render/ServiceFirstRenderWatchState.java @@ -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(), @@ -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(),