Skip to content

Commit 8af55c9

Browse files
committed
Merge remote-tracking branch 'origin/main' into FirebaseDataConnectImplState
2 parents e19950e + 51b4a1c commit 8af55c9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/FirebaseDataConnectImpl.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,29 @@ internal class FirebaseDataConnectImpl(
484484
is State.Closing -> newState.closeJob.apply { start() }
485485
is State.Closed -> null
486486
}
487+
488+
// Register the new "close job". Do not overwrite a close job that is already in progress (to
489+
// avoid having more than one close job in progress at a time) or a close job that completed
490+
// successfully (since there is nothing to do if a previous close job was successful).
491+
val updatedCloseJobRef =
492+
closeJob.updateAndGet { currentCloseJobRef: NullableReference<Deferred<Unit>> ->
493+
if (currentCloseJobRef.ref !== null && !currentCloseJobRef.ref.isCancelled) {
494+
currentCloseJobRef
495+
} else {
496+
NullableReference(newCloseJob)
497+
}
498+
}
499+
500+
// Start the updated "close job" (if it was already started then start() is a no-op).
501+
val updatedCloseJob =
502+
checkNotNull(updatedCloseJobRef.ref) {
503+
"internal error: closeJob.updateAndGet() returned a NullableReference whose 'ref' " +
504+
"property was null; however it should NOT have been null (error code y5fk4ntdnd)"
505+
}
506+
updatedCloseJob.start()
507+
508+
// Return the "close job", which _may_ already be completed, so the caller can await it.
509+
return updatedCloseJob
487510
}
488511

489512
// The generated SDK relies on equals() and hashCode() using object identity.

0 commit comments

Comments
 (0)