Skip to content

Commit

Permalink
refactor(cloud_functions): remove deprecated Task APIs (#10076)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyokone committed Dec 8, 2022
1 parent d4c27da commit 951bc77
Showing 1 changed file with 38 additions and 20 deletions.
Expand Up @@ -8,6 +8,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.functions.FirebaseFunctions;
Expand Down Expand Up @@ -60,31 +61,40 @@ private FirebaseFunctions getFunctions(Map<String, Object> arguments) {
}

private Task<Object> httpsFunctionCall(Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
TaskCompletionSource<Object> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
FirebaseFunctions firebaseFunctions = getFunctions(arguments);
try {

String functionName = (String) Objects.requireNonNull(arguments.get("functionName"));
String origin = (String) arguments.get("origin");
Integer timeout = (Integer) arguments.get("timeout");
Object parameters = arguments.get("parameters");
FirebaseFunctions firebaseFunctions = getFunctions(arguments);

if (origin != null) {
Uri originUri = Uri.parse(origin);
firebaseFunctions.useEmulator(originUri.getHost(), originUri.getPort());
}
String functionName = (String) Objects.requireNonNull(arguments.get("functionName"));
String origin = (String) arguments.get("origin");
Integer timeout = (Integer) arguments.get("timeout");
Object parameters = arguments.get("parameters");

HttpsCallableReference httpsCallableReference =
firebaseFunctions.getHttpsCallable(functionName);
if (origin != null) {
Uri originUri = Uri.parse(origin);
firebaseFunctions.useEmulator(originUri.getHost(), originUri.getPort());
}

if (timeout != null) {
httpsCallableReference.setTimeout(timeout.longValue(), TimeUnit.MILLISECONDS);
}
HttpsCallableReference httpsCallableReference =
firebaseFunctions.getHttpsCallable(functionName);

HttpsCallableResult result = Tasks.await(httpsCallableReference.call(parameters));
return result.getData();
if (timeout != null) {
httpsCallableReference.setTimeout(timeout.longValue(), TimeUnit.MILLISECONDS);
}

HttpsCallableResult result = Tasks.await(httpsCallableReference.call(parameters));
taskCompletionSource.setResult(result.getData());

} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

@Override
Expand Down Expand Up @@ -156,11 +166,19 @@ private Map<String, Object> getExceptionDetails(@Nullable Exception exception) {

@Override
public Task<Map<String, Object>> getPluginConstantsForFirebaseApp(FirebaseApp firebaseApp) {
return Tasks.call(() -> null);
TaskCompletionSource<Map<String, Object>> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(() -> taskCompletionSource.setResult(null));

return taskCompletionSource.getTask();
}

@Override
public Task<Void> didReinitializeFirebaseCore() {
return Tasks.call(() -> null);
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(() -> taskCompletionSource.setResult(null));

return taskCompletionSource.getTask();
}
}

0 comments on commit 951bc77

Please sign in to comment.