forked from compnerd/swift-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Implement runTransaction and batch support #28
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ad0439f
hacking
darinf f07e318
add missing files
darinf 79e3b65
something that compiles
darinf d18b850
make it compile
darinf dbce132
more
darinf b4e079e
hide this function by using a parameter type that Swift cannot see
darinf 0396027
works
darinf 7898839
clear out error before calling updateBlock
darinf bcf11eb
batch support
darinf 8fb2412
cleanup
darinf 34c6747
cleanup
darinf 4ab6cc2
more
darinf 44a505d
revert
darinf 23adf0b
add compile time assert
darinf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
@_exported | ||
import firebase | ||
|
||
import CxxShim | ||
import Foundation | ||
|
||
public typealias Transaction = swift_firebase.swift_cxx_shims.firebase.firestore.TransactionWeakReference | ||
|
||
extension Transaction { | ||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference) -> Transaction { | ||
setData(data, forDocument: document, merge: false) | ||
} | ||
|
||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference, merge: Bool) -> Transaction { | ||
assert(is_valid()) | ||
darinf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.Set(document, FirestoreDataConverter.firestoreValue(document: data), merge ? .Merge() : .init()) | ||
return self | ||
} | ||
|
||
/* TODO: implement | ||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference, mergeFields: [Any]) -> Transaction { | ||
} | ||
*/ | ||
|
||
public mutating func updateData(_ fields: [String : Any], forDocument document: DocumentReference) -> Transaction { | ||
assert(is_valid()) | ||
self.Update(document, FirestoreDataConverter.firestoreValue(document: fields)) | ||
return self | ||
} | ||
|
||
public mutating func deleteDocument(_ document: DocumentReference) -> Transaction { | ||
assert(is_valid()) | ||
Delete(document) | ||
return self | ||
} | ||
|
||
public mutating func getDocument(_ document: DocumentReference) throws -> DocumentSnapshot { | ||
assert(is_valid()) | ||
|
||
var error = firebase.firestore.kErrorNone | ||
var errorMessage = std.string() | ||
|
||
let snapshot = Get(document, &error, &errorMessage) | ||
|
||
if error != firebase.firestore.kErrorNone { | ||
throw NSError.firestore(error, errorMessage: String(errorMessage)) | ||
} | ||
|
||
return snapshot | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
@_exported | ||
import firebase | ||
|
||
public typealias TransactionOptions = firebase.firestore.TransactionOptions | ||
|
||
extension TransactionOptions { | ||
public var maxAttempts: Int { | ||
get { | ||
Int(max_attempts()) | ||
} | ||
set { | ||
set_max_attempts(Int32(newValue)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
@_exported | ||
import firebase | ||
@_spi(FirebaseInternal) | ||
import FirebaseCore | ||
|
||
import CxxShim | ||
import Foundation | ||
|
||
public typealias WriteBatch = firebase.firestore.WriteBatch | ||
|
||
extension WriteBatch { | ||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference) -> WriteBatch { | ||
setData(data, forDocument: document, merge: false) | ||
} | ||
|
||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference, merge: Bool) -> WriteBatch { | ||
_ = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_set( | ||
self, document, FirestoreDataConverter.firestoreValue(document: data), merge ? .Merge() : .init() | ||
) | ||
return self | ||
} | ||
|
||
/* TODO: implement | ||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference, mergeFields: [Any]) -> WriteBatch { | ||
} | ||
*/ | ||
|
||
public mutating func updateData(_ fields: [String : Any], forDocument document: DocumentReference) -> WriteBatch { | ||
_ = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_update( | ||
self, document, FirestoreDataConverter.firestoreValue(document: fields) | ||
) | ||
return self | ||
} | ||
|
||
public mutating func deleteDocument(_ document: DocumentReference) -> WriteBatch { | ||
_ = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_delete( | ||
self, document | ||
) | ||
return self | ||
} | ||
|
||
public mutating func commit(completion: @escaping ((Error?) -> Void) = { _ in }) { | ||
let future = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_commit(self) | ||
darinf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
future.setCompletion({ | ||
let (_, error) = future.resultAndError | ||
DispatchQueue.main.async { | ||
completion(error) | ||
} | ||
}) | ||
} | ||
|
||
public mutating func commit() async throws { | ||
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in | ||
let future = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_commit(self) | ||
future.setCompletion({ | ||
let (_, error) = future.resultAndError | ||
if let error { | ||
continuation.resume(throwing: error) | ||
} else { | ||
continuation.resume() | ||
} | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this need changes from https://github.com/thebrowsercompany/swift-firebase/pull/30/files for the error pointer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that PR builds on top of this one. I wrote that one once I was able to try building against Arc. Rather than complicate things by folding it into this PR, I just left them as separate. Hope that works!