Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloud_firestore): fix returning of transaction result #3747

Merged
merged 1 commit into from Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/cloud_firestore/cloud_firestore/lib/src/firestore.dart
Expand Up @@ -168,11 +168,15 @@ class FirebaseFirestore extends FirebasePluginPlatform {
/// By default transactions are limited to 5 seconds of execution time. This
/// timeout can be adjusted by setting the timeout parameter.
Future<T> runTransaction<T>(TransactionHandler<T> transactionHandler,
{Duration timeout = const Duration(seconds: 30)}) {
{Duration timeout = const Duration(seconds: 30)}) async {
assert(transactionHandler != null, "transactionHandler cannot be null");
return _delegate.runTransaction<T>((transaction) {
return transactionHandler(Transaction._(this, transaction));

T output;
await _delegate.runTransaction((transaction) async {
output = await transactionHandler(Transaction._(this, transaction));
}, timeout: timeout);

return output;
}

/// Specifies custom settings to be used to configure this [FirebaseFirestore] instance.
Expand Down
Expand Up @@ -341,6 +341,8 @@ class MethodChannelFirebaseFirestore extends FirebaseFirestorePlatform {
exception = e;
});

// The #create call only resolves once all transaction attempts have succeeded
// or something failed.
await channel.invokeMethod<T>('Transaction#create', <String, dynamic>{
'firestore': this,
'transactionId': transactionId,
Expand All @@ -349,7 +351,7 @@ class MethodChannelFirebaseFirestore extends FirebaseFirestorePlatform {
exception = e;
});

// The transaction is successful, cleanup the stream
// The transaction has completed (may have errored), cleanup the stream
await subscription.cancel();
_transactionStreamControllerHandlers.remove(transactionId);

Expand Down
Expand Up @@ -86,16 +86,15 @@ class FirebaseFirestoreWeb extends FirebaseFirestorePlatform {
// }

@override
Future<T> runTransaction<T>(TransactionHandler transactionHandler,
Future<T> runTransaction<T>(TransactionHandler<T> transactionHandler,
{Duration timeout = const Duration(seconds: 30)}) async {
try {
dynamic result = await _webFirestore.runTransaction((transaction) async {
await _webFirestore.runTransaction((transaction) async {
return transactionHandler(
TransactionWeb(this, _webFirestore, transaction));
}).timeout(timeout);
// Workaround for 'Runtime type information not available for type_variable_local'
// See: https://github.com/dart-lang/sdk/issues/29722
return result as T;

return null;
} catch (e) {
throw getFirebaseException(e);
}
Expand Down