Skip to content

Commit

Permalink
Replaced per-task executor with a global single-thread scheduled exec…
Browse files Browse the repository at this point in the history
…utor.
  • Loading branch information
ygimenez committed Jul 4, 2024
1 parent c418973 commit 61d4f64
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/main/java/com/github/ygimenez/method/Pages.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* to {@link #paginate}, {@link #categorize}, {@link #buttonize} and {@link #lazyPaginate}.
*/
public abstract class Pages {
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
private static final EventHandler handler = new EventHandler();
private static Paginator paginator;

Expand Down Expand Up @@ -570,11 +571,10 @@ public static ActionReference paginate(@NotNull Message msg, @NotNull PaginateHe
addReactions(msg, helper.getSkipAmount() > 1, helper.isFastForward());
}

Executor executor = CompletableFuture.delayedExecutor(helper.getTimeout(), TimeUnit.MILLISECONDS);
return handler.addEvent(msg, new ThrowingBiConsumer<>() {
private final int maxP = pgs.size() - 1;
private int p = 0;
private CompletableFuture<?> timeout;
private ScheduledFuture<?> timeout;
private final Consumer<Void> success = s -> {
if (timeout != null) {
timeout.cancel(true);
Expand All @@ -589,7 +589,7 @@ public static ActionReference paginate(@NotNull Message msg, @NotNull PaginateHe

{
if (helper.getTimeout() > 0) {
timeout = CompletableFuture.runAsync(() -> finalizeEvent(msg, success), executor);
timeout = worker.schedule(() -> finalizeEvent(msg, success), helper.getTimeout(), TimeUnit.MILLISECONDS);
}
}

Expand Down Expand Up @@ -678,7 +678,7 @@ public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrappe
timeout.cancel(true);
}
if (helper.getTimeout() > 0) {
timeout = CompletableFuture.runAsync(() -> finalizeEvent(msg, success), executor);
timeout = worker.schedule(() -> finalizeEvent(msg, success), helper.getTimeout(), TimeUnit.MILLISECONDS);
}

if (wrapper.isFromGuild() && wrapper.getSource() instanceof MessageReactionAddEvent && paginator.isRemoveOnReact()) {
Expand Down Expand Up @@ -845,10 +845,9 @@ public static ActionReference categorize(@NotNull Message msg, @NotNull Categori
msg.addReaction(paginator.getEmoji(CANCEL)).submit();
}

Executor executor = CompletableFuture.delayedExecutor(helper.getTimeout(), TimeUnit.MILLISECONDS);
return handler.addEvent(msg, new ThrowingBiConsumer<>() {
private Emoji currCat = null;
private CompletableFuture<?> timeout;
private ScheduledFuture<?> timeout;
private final Consumer<Void> success = s -> {
if (timeout != null) {
timeout.cancel(true);
Expand All @@ -860,7 +859,7 @@ public static ActionReference categorize(@NotNull Message msg, @NotNull Categori

{
if (helper.getTimeout() > 0) {
timeout = CompletableFuture.runAsync(() -> finalizeEvent(msg, success), executor);
timeout = worker.schedule(() -> finalizeEvent(msg, success), helper.getTimeout(), TimeUnit.MILLISECONDS);
}
}

Expand Down Expand Up @@ -909,7 +908,7 @@ public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrappe
timeout.cancel(true);
}
if (helper.getTimeout() > 0) {
timeout = CompletableFuture.runAsync(() -> finalizeEvent(msg, success), executor);
timeout = worker.schedule(() -> finalizeEvent(msg, success), helper.getTimeout(), TimeUnit.MILLISECONDS);
}

if (wrapper.isFromGuild() && wrapper.getSource() instanceof MessageReactionAddEvent && paginator.isRemoveOnReact()) {
Expand Down Expand Up @@ -1155,9 +1154,8 @@ public static ActionReference buttonize(@NotNull Message msg, @NotNull Buttonize
}
}

Executor executor = CompletableFuture.delayedExecutor(helper.getTimeout(), TimeUnit.MILLISECONDS);
return handler.addEvent(msg, new ThrowingBiConsumer<>() {
private CompletableFuture<?> timeout;
private ScheduledFuture<?> timeout;
private final Consumer<Void> success = s -> {
if (timeout != null) {
timeout.cancel(true);
Expand All @@ -1170,7 +1168,7 @@ public static ActionReference buttonize(@NotNull Message msg, @NotNull Buttonize

{
if (helper.getTimeout() > 0) {
timeout = CompletableFuture.runAsync(() -> finalizeEvent(msg, success), executor);
timeout = worker.schedule(() -> finalizeEvent(msg, success), helper.getTimeout(), TimeUnit.MILLISECONDS);
}
}

Expand Down Expand Up @@ -1229,7 +1227,7 @@ public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrappe
timeout.cancel(true);
}
if (helper.getTimeout() > 0) {
timeout = CompletableFuture.runAsync(() -> finalizeEvent(msg, success), executor);
timeout = worker.schedule(() -> finalizeEvent(msg, success), helper.getTimeout(), TimeUnit.MILLISECONDS);
}

if (wrapper.isFromGuild() && wrapper.getSource() instanceof MessageReactionAddEvent && paginator.isRemoveOnReact()) {
Expand Down Expand Up @@ -1493,10 +1491,9 @@ public static ActionReference lazyPaginate(@NotNull Message msg, @NotNull LazyPa
addReactions(msg, false, false);
}

Executor executor = CompletableFuture.delayedExecutor(helper.getTimeout(), TimeUnit.MILLISECONDS);
return handler.addEvent(msg, new ThrowingBiConsumer<>() {
private int p = 0;
private CompletableFuture<?> timeout;
private ScheduledFuture<?> timeout;
private final Consumer<Void> success = s -> {
if (timeout != null) {
timeout.cancel(true);
Expand All @@ -1510,7 +1507,7 @@ public static ActionReference lazyPaginate(@NotNull Message msg, @NotNull LazyPa

{
if (helper.getTimeout() > 0) {
timeout = CompletableFuture.runAsync(() -> finalizeEvent(msg, success), executor);
timeout = worker.schedule(() -> finalizeEvent(msg, success), helper.getTimeout(), TimeUnit.MILLISECONDS);
}
}

Expand Down Expand Up @@ -1588,7 +1585,7 @@ public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrappe
timeout.cancel(true);
}
if (helper.getTimeout() > 0) {
timeout = CompletableFuture.runAsync(() -> finalizeEvent(msg, success), executor);
timeout = worker.schedule(() -> finalizeEvent(msg, success), helper.getTimeout(), TimeUnit.MILLISECONDS);
}

if (wrapper.isFromGuild() && wrapper.getSource() instanceof MessageReactionAddEvent && paginator.isRemoveOnReact()) {
Expand Down

0 comments on commit 61d4f64

Please sign in to comment.