Skip to content

Commit 90af53d

Browse files
committed
Update TDLib
- Introduced `sendAsync` in `TelegramClient` to support asynchronous queries with `CompletableFuture`. - Updated TDLib native and API versions in BOM. - Improved input handling in `ScannerUtils`. - Minor adjustment in `SimpleTelegramClient.logOutAsync`.
1 parent 3af7649 commit 90af53d

4 files changed

Lines changed: 39 additions & 6 deletions

File tree

bom/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<name>TDLight Java BOM</name>
99
<properties>
1010
<revision>3.0.0.0-SNAPSHOT</revision>
11-
<tdlight.natives.version>4.0.558</tdlight.natives.version>
12-
<tdlight.api.version>4.0.528</tdlight.api.version>
11+
<tdlight.natives.version>4.0.585</tdlight.natives.version>
12+
<tdlight.api.version>4.0.555</tdlight.api.version>
1313
<maven.compiler.source>1.8</maven.compiler.source>
1414
<maven.compiler.target>1.8</maven.compiler.target>
1515
</properties>

tdlight-java/src/main/java/it/tdlight/TelegramClient.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package it.tdlight;
22

3+
import it.tdlight.client.TelegramError;
34
import it.tdlight.jni.TdApi;
5+
import java.util.concurrent.CompletableFuture;
46

57
public interface TelegramClient {
68

@@ -65,4 +67,31 @@ default <R extends TdApi.Object> void send(TdApi.Function<R> query, ResultHandle
6567
* @throws NullPointerException if query is null.
6668
*/
6769
<R extends TdApi.Object> TdApi.Object execute(TdApi.Function<R> query);
70+
71+
/**
72+
* Sends a request to the TDLib and returns a {@link CompletableFuture} that will be completed
73+
* with the result or completed exceptionally with a {@link TelegramError} if TDLib returns an error.
74+
*
75+
* <p>The returned future has no timeout by default. To avoid hanging indefinitely on a
76+
* non-responsive request, use {@link CompletableFuture#orTimeout(long, java.util.concurrent.TimeUnit)}
77+
* on the returned future.</p>
78+
*
79+
* @param query Object representing a query to the TDLib.
80+
* @param <R> The expected result type.
81+
* @return a {@link CompletableFuture} that completes with the result or fails with {@link TelegramError}.
82+
* @throws NullPointerException if query is null.
83+
*/
84+
default <R extends TdApi.Object> CompletableFuture<R> sendAsync(TdApi.Function<R> query) {
85+
CompletableFuture<R> future = new CompletableFuture<>();
86+
send(query, result -> {
87+
if (result.getConstructor() == TdApi.Error.CONSTRUCTOR) {
88+
future.completeExceptionally(new TelegramError((TdApi.Error) result));
89+
} else {
90+
@SuppressWarnings("unchecked")
91+
R r = (R) result;
92+
future.complete(r);
93+
}
94+
});
95+
return future;
96+
}
6897
}

tdlight-java/src/main/java/it/tdlight/client/SimpleTelegramClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ private <R extends TdApi.Object> boolean shouldWaitForReadiness(Function<R> func
270270
case TdApi.PingProxy.CONSTRUCTOR:
271271
case TdApi.TestProxy.CONSTRUCTOR:
272272
case TdApi.EnableProxy.CONSTRUCTOR:
273-
case TdApi.GetProxyLink.CONSTRUCTOR:
274273
case TdApi.GetProxies.CONSTRUCTOR:
275274
case TdApi.LogOut.CONSTRUCTOR:
276275
case TdApi.SetTdlibParameters.CONSTRUCTOR:
@@ -519,7 +518,7 @@ public CompletableFuture<Void> loadChatListMainAsync() {
519518
*
520519
**/
521520
public CompletableFuture<Void> logOutAsync() {
522-
return send(new LogOut()).thenAccept(ok -> {});
521+
return send(new LogOut()).thenApply(ok -> null);
523522
}
524523

525524
public boolean isMainChatsListLoaded() {

tdlight-java/src/main/java/it/tdlight/util/ScannerUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.Console;
44
import java.io.IOException;
5+
import java.io.InputStream;
56
import java.io.InputStreamReader;
67
import java.io.Reader;
78
import java.util.regex.Matcher;
@@ -13,13 +14,17 @@ public final class ScannerUtils {
1314
private static InputStreamReader scanner = null;
1415

1516
public static String askParameter(String displayName, String question) {
17+
return askParameter(displayName, question, System.in);
18+
}
19+
20+
public static String askParameter(String displayName, String question, InputStream inputStream) {
1621
synchronized (LOCK) {
1722
Console console = System.console();
1823
if (console != null) {
1924
return console.readLine("[%s] %s: ", displayName, question);
2025
} else {
21-
if (scanner == null) {
22-
scanner = new InputStreamReader(System.in);
26+
if (scanner == null || inputStream != System.in) {
27+
scanner = new InputStreamReader(inputStream);
2328
}
2429
System.out.printf("[%s] %s: ", displayName, question);
2530
try {

0 commit comments

Comments
 (0)