DataFetcher .java

com.mojang.realmsclient.gui.task.DataFetcher (class): members: SuccessfulComputationResult, createSubscription, ComputationResult, SubscribedTask, Task.

com.mojang.realmsclient.gui.taskclass177 linescommon5.7 KB

API Surface (7)

<T> DataFetcher.Task<T> createTask(final String id, final Callable<T> updater, final Duration period, final RepeatedDelayStrategy repeatStrategy)
DataFetcher.Subscription createSubscription()
<T> void subscribe(final DataFetcher.Task<T> task, final Consumer<T> output)
void forceUpdate()
void tick()
void reset()
void reset()

Fields & Constants (4)

LOGGER
executor
resolution
timeSource

String literals (2)

"Period of "
" too short for selected resolution of "

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

DataFetcher.java177 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient.gui.task;
2
3
import com.mojang.datafixers.util.Either;
4
import com.mojang.logging.LogUtils;
5
import java.time.Duration;
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.concurrent.Callable;
9
import java.util.concurrent.CompletableFuture;
10
import java.util.concurrent.Executor;
11
import java.util.concurrent.TimeUnit;
12
import java.util.function.Consumer;
13
import net.fabricmc.api.EnvType;
14
import net.fabricmc.api.Environment;
15
import net.minecraft.util.TimeSource;
16
import org.jspecify.annotations.Nullable;
17
import org.slf4j.Logger;
18
19
@Environment(EnvType.CLIENT)
20
public class DataFetcher {
21
	private static final Logger LOGGER = LogUtils.getLogger();
22
	private final Executor executor;
23
	private final TimeUnit resolution;
24
	private final TimeSource timeSource;
25
26
	public DataFetcher(final Executor executor, final TimeUnit resolution, final TimeSource timeSource) {
27
		this.executor = executor;
28
		this.resolution = resolution;
29
		this.timeSource = timeSource;
30
	}
31
32
	public <T> DataFetcher.Task<T> createTask(final String id, final Callable<T> updater, final Duration period, final RepeatedDelayStrategy repeatStrategy) {
33
		long periodInUnit = this.resolution.convert(period);
34
		if (periodInUnit == 0L) {
35
			throw new IllegalArgumentException("Period of " + period + " too short for selected resolution of " + this.resolution);
36
		} else {
37
			return new DataFetcher.Task<>(id, updater, periodInUnit, repeatStrategy);
38
		}
39
	}
40
41
	public DataFetcher.Subscription createSubscription() {
42
		return new DataFetcher.Subscription();
43
	}
44
45
	@Environment(EnvType.CLIENT)
46
	private record ComputationResult<T>(Either<T, Exception> value, long time) {
47
	}
48
49
	@Environment(EnvType.CLIENT)
50
	private class SubscribedTask<T> {
51
		private final DataFetcher.Task<T> task;
52
		private final Consumer<T> output;
53
		private long lastCheckTime = -1L;
54
55
		private SubscribedTask(final DataFetcher.Task<T> task, final Consumer<T> output) {
56
			this.task = task;
57
			this.output = output;
58
		}
59
60
		private void update(final long currentTime) {
61
			this.task.updateIfNeeded(currentTime);
62
			this.runCallbackIfNeeded();
63
		}
64
65
		private void runCallbackIfNeeded() {
66
			DataFetcher.SuccessfulComputationResult<T> lastResult = this.task.lastResult;
67
			if (lastResult != null && this.lastCheckTime < lastResult.time) {
68
				this.output.accept(lastResult.value);
69
				this.lastCheckTime = lastResult.time;
70
			}
71
		}
72
73
		private void runCallback() {
74
			DataFetcher.SuccessfulComputationResult<T> lastResult = this.task.lastResult;
75
			if (lastResult != null) {
76
				this.output.accept(lastResult.value);
77
				this.lastCheckTime = lastResult.time;
78
			}
79
		}
80
81
		private void reset() {
82
			this.task.reset();
83
			this.lastCheckTime = -1L;
84
		}
85
	}
86
87
	@Environment(EnvType.CLIENT)
88
	public class Subscription {
89
		private final List<DataFetcher.SubscribedTask<?>> subscriptions = new ArrayList<>();
90
91
		public <T> void subscribe(final DataFetcher.Task<T> task, final Consumer<T> output) {
92
			DataFetcher.SubscribedTask<T> subscription = DataFetcher.this.new SubscribedTask<>(task, output);
93
			this.subscriptions.add(subscription);
94
			subscription.runCallbackIfNeeded();
95
		}
96
97
		public void forceUpdate() {
98
			for (DataFetcher.SubscribedTask<?> subscription : this.subscriptions) {
99
				subscription.runCallback();
100
			}
101
		}
102
103
		public void tick() {
104
			for (DataFetcher.SubscribedTask<?> subscription : this.subscriptions) {
105
				subscription.update(DataFetcher.this.timeSource.get(DataFetcher.this.resolution));
106
			}
107
		}
108
109
		public void reset() {
110
			for (DataFetcher.SubscribedTask<?> subscription : this.subscriptions) {
111
				subscription.reset();
112
			}
113
		}
114
	}
115
116
	@Environment(EnvType.CLIENT)
117
	private record SuccessfulComputationResult<T>(T value, long time) {
118
	}
119
120
	@Environment(EnvType.CLIENT)
121
	public class Task<T> {
122
		private final String id;
123
		private final Callable<T> updater;
124
		private final long period;
125
		private final RepeatedDelayStrategy repeatStrategy;
126
		private @Nullable CompletableFuture<DataFetcher.ComputationResult<T>> pendingTask;
127
		private DataFetcher.@Nullable SuccessfulComputationResult<T> lastResult;
128
		private long nextUpdate = -1L;
129
130
		private Task(final String id, final Callable<T> updater, final long period, final RepeatedDelayStrategy repeatStrategy) {
131
			this.id = id;
132
			this.updater = updater;
133
			this.period = period;
134
			this.repeatStrategy = repeatStrategy;
135
		}
136
137
		private void updateIfNeeded(final long currentTime) {
138
			if (this.pendingTask != null) {
139
				DataFetcher.ComputationResult<T> result = this.pendingTask.getNow(null);
140
				if (result == null) {
141
					return;
142
				}
143
144
				this.pendingTask = null;
145
				long completionTime = result.time;
146
				result.value().ifLeft(value -> {
147
					this.lastResult = new DataFetcher.SuccessfulComputationResult<>((T)value, completionTime);
148
					this.nextUpdate = completionTime + this.period * this.repeatStrategy.delayCyclesAfterSuccess();
149
				}).ifRight(e -> {
150
					long cycles = this.repeatStrategy.delayCyclesAfterFailure();
151
					DataFetcher.LOGGER.warn("Failed to process task {}, will repeat after {} cycles", this.id, cycles, e);
152
					this.nextUpdate = completionTime + this.period * cycles;
153
				});
154
			}
155
156
			if (this.nextUpdate <= currentTime) {
157
				this.pendingTask = CompletableFuture.supplyAsync(() -> {
158
					try {
159
						T resultx = this.updater.call();
160
						long completionTimex = DataFetcher.this.timeSource.get(DataFetcher.this.resolution);
161
						return new DataFetcher.ComputationResult<>(Either.left(resultx), completionTimex);
162
					} catch (Exception e) {
163
						long completionTimexx = DataFetcher.this.timeSource.get(DataFetcher.this.resolution);
164
						return new DataFetcher.ComputationResult<>(Either.right(e), completionTimexx);
165
					}
166
				}, DataFetcher.this.executor);
167
			}
168
		}
169
170
		public void reset() {
171
			this.pendingTask = null;
172
			this.lastResult = null;
173
			this.nextUpdate = -1L;
174
		}
175
	}
176
}
177

Decompiled Minecraft 26.3 source of DataFetcher (com/mojang/realmsclient/gui/task/DataFetcher.java) with Mojang official mappings, stored verbatim on the site.