Skip to content

Commit

Permalink
style: move the function closer to the non-async implementation
Browse files Browse the repository at this point in the history
Asked by paulb777 [here](firebase#13407 (comment))
  • Loading branch information
MojtabaHs committed Jul 27, 2024
1 parent 63a80d5 commit 5bd75f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,4 @@ public extension CollectionReference {
}
}
}

/// Encodes an instance of `Encodable` and adds a new document to this collection
/// with the encoded data, assigning it a document ID automatically.
///
/// See `Firestore.Encoder` for more details about the encoding process.
///
/// - Parameters:
/// - value: An instance of `Encodable` to be encoded to a document.
/// - encoder: An encoder instance to use to run the encoding.
/// - Throws: `Error` if the backend rejected the write.
/// - Returns: A `DocumentReference` pointing to the newly created document.
@discardableResult
func addDocument<T: Encodable>(from value: T,
encoder: Firestore.Encoder = Firestore.Encoder()) async throws
-> DocumentReference {
return try await withCheckedThrowingContinuation { continuation in
var document: DocumentReference?
document = self.addDocument(from: value, encoder: encoder) { error in
if let error {
continuation.resume(throwing: error)
} else {
// Our callbacks guarantee that we either return an error or a document.
continuation.resume(returning: document!)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,31 @@ public extension CollectionReference {
let encoded = try encoder.encode(value)
return addDocument(data: encoded, completion: completion)
}

/// Encodes an instance of `Encodable` and adds a new document to this collection
/// with the encoded data, assigning it a document ID automatically.
///
/// See `Firestore.Encoder` for more details about the encoding process.
///
/// - Parameters:
/// - value: An instance of `Encodable` to be encoded to a document.
/// - encoder: An encoder instance to use to run the encoding.
/// - Throws: `Error` if the backend rejected the write.
/// - Returns: A `DocumentReference` pointing to the newly created document.
@discardableResult
func addDocument<T: Encodable>(from value: T,
encoder: Firestore.Encoder = Firestore.Encoder()) async throws
-> DocumentReference {
return try await withCheckedThrowingContinuation { continuation in
var document: DocumentReference?
document = self.addDocument(from: value, encoder: encoder) { error in
if let error {
continuation.resume(throwing: error)
} else {
// Our callbacks guarantee that we either return an error or a document.
continuation.resume(returning: document!)
}
}
}
}
}

0 comments on commit 5bd75f2

Please sign in to comment.