package com.mojang.realmsclient.gui.task; import com.mojang.datafixers.util.Either; import com.mojang.logging.LogUtils; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import net.minecraft.util.TimeSource; import org.jspecify.annotations.Nullable; import org.slf4j.Logger; public class DataFetcher { static final Logger LOGGER = LogUtils.getLogger(); final Executor executor; final TimeUnit resolution; final TimeSource timeSource; public DataFetcher(Executor executor, TimeUnit timeUnit, TimeSource timeSource) { this.executor = executorx; this.resolution = timeUnit; this.timeSource = timeSourcex; } public DataFetcher.Task createTask(String string, Callable callable, Duration duration, RepeatedDelayStrategy repeatedDelayStrategy) { long l = this.resolution.convert(duration); if (l == 0L) { throw new IllegalArgumentException("Period of " + duration + " too short for selected resolution of " + this.resolution); } else { return new DataFetcher.Task<>(string, callable, l, repeatedDelayStrategy); } } public DataFetcher.Subscription createSubscription() { return new DataFetcher.Subscription(); } static record ComputationResult(Either value, long time) { } class SubscribedTask { private final DataFetcher.Task task; private final Consumer output; private long lastCheckTime = -1L; SubscribedTask(final DataFetcher.Task task, final Consumer consumer) { this.task = taskx; this.output = consumer; } void update(long l) { this.task.updateIfNeeded(l); this.runCallbackIfNeeded(); } void runCallbackIfNeeded() { DataFetcher.SuccessfulComputationResult successfulComputationResult = this.task.lastResult; if (successfulComputationResult != null && this.lastCheckTime < successfulComputationResult.time) { this.output.accept(successfulComputationResult.value); this.lastCheckTime = successfulComputationResult.time; } } void runCallback() { DataFetcher.SuccessfulComputationResult successfulComputationResult = this.task.lastResult; if (successfulComputationResult != null) { this.output.accept(successfulComputationResult.value); this.lastCheckTime = successfulComputationResult.time; } } void reset() { this.task.reset(); this.lastCheckTime = -1L; } } public class Subscription { private final List> subscriptions = new ArrayList<>(); public void subscribe(DataFetcher.Task task, Consumer consumer) { DataFetcher.SubscribedTask subscribedTask = DataFetcher.this.new SubscribedTask(task, consumer); this.subscriptions.add(subscribedTask); subscribedTask.runCallbackIfNeeded(); } public void forceUpdate() { for (DataFetcher.SubscribedTask subscribedTask : this.subscriptions) { subscribedTask.runCallback(); } } public void tick() { for (DataFetcher.SubscribedTask subscribedTask : this.subscriptions) { subscribedTask.update(DataFetcher.this.timeSource.get(DataFetcher.this.resolution)); } } public void reset() { for (DataFetcher.SubscribedTask subscribedTask : this.subscriptions) { subscribedTask.reset(); } } } static record SuccessfulComputationResult(T value, long time) { } public class Task { private final String id; private final Callable updater; private final long period; private final RepeatedDelayStrategy repeatStrategy; @Nullable private CompletableFuture> pendingTask; @Nullable DataFetcher.SuccessfulComputationResult lastResult; private long nextUpdate = -1L; Task(final String string, final Callable callable, final long l, final RepeatedDelayStrategy repeatedDelayStrategy) { this.id = string; this.updater = callable; this.period = l; this.repeatStrategy = repeatedDelayStrategy; } void updateIfNeeded(long l) { if (this.pendingTask != null) { DataFetcher.ComputationResult computationResult = this.pendingTask.getNow(null); if (computationResult == null) { return; } this.pendingTask = null; long m = computationResult.time; computationResult.value().ifLeft(object -> { this.lastResult = new DataFetcher.SuccessfulComputationResult<>((T)object, m); this.nextUpdate = m + this.period * this.repeatStrategy.delayCyclesAfterSuccess(); }).ifRight(exception -> { long o = this.repeatStrategy.delayCyclesAfterFailure(); DataFetcher.LOGGER.warn("Failed to process task {}, will repeat after {} cycles", new Object[]{this.id, o, exception}); this.nextUpdate = m + this.period * o; }); } if (this.nextUpdate <= l) { this.pendingTask = CompletableFuture.supplyAsync(() -> { try { Object object = this.updater.call(); long o = DataFetcher.this.timeSource.get(DataFetcher.this.resolution); return new DataFetcher.ComputationResult<>(Either.left(object), o); } catch (Exception exception) { long n = DataFetcher.this.timeSource.get(DataFetcher.this.resolution); return new DataFetcher.ComputationResult<>(Either.right(exception), n); } }, DataFetcher.this.executor); } } public void reset() { this.pendingTask = null; this.lastResult = null; this.nextUpdate = -1L; } } }