-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Firestore Swift Cpp Experiment #13956
base: main
Are you sure you want to change the base?
Changes from 22 commits
163cbdd
6350734
19cdc82
be5efcf
d89a70b
f2326f7
a980df3
2dfbe50
7887fe5
2e504e5
50e52e0
72ae07d
53b0471
2b647b8
6608bb8
9ced414
e6785f5
e880583
9d9b7c2
5462e69
7d2362b
5dc9783
da80692
dbf10d8
0e652a7
5bde275
3fc8a15
182b237
7a9aa72
499bcb4
757d524
ef10b6f
f171bf4
fe4ec87
eb6620b
72430e8
e6379ca
5a009b8
6334091
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import "FIRCallbackWrapper.h" | ||
|
||
#include <memory> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "Firestore/core/interfaceForSwift/api/pipeline.h" | ||
#include "Firestore/core/interfaceForSwift/api/pipeline_result.h" | ||
#include "Firestore/core/src/core/event_listener.h" | ||
#include "Firestore/core/src/util/error_apple.h" | ||
#include "Firestore/core/src/util/statusor.h" | ||
|
||
using firebase::firestore::api::PipelineResult; | ||
using firebase::firestore::api::PipelineSnapshotListener; | ||
using firebase::firestore::core::EventListener; | ||
using firebase::firestore::util::MakeNSError; | ||
using firebase::firestore::util::StatusOr; | ||
|
||
@implementation FIRCallbackWrapper | ||
|
||
+ (PipelineSnapshotListener) | ||
wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore | ||
completion:(void (^)(std::shared_ptr<std::vector<PipelineResult>> result, | ||
NSError *_Nullable error))completion { | ||
class Converter : public EventListener<std::vector<PipelineResult>> { | ||
public: | ||
explicit Converter(std::shared_ptr<api::Firestore> firestore, PipelineBlock completion) | ||
: firestore_(firestore), completion_(completion) { | ||
} | ||
|
||
void OnEvent(StatusOr<std::vector<PipelineResult>> maybe_snapshot) override { | ||
if (maybe_snapshot.ok()) { | ||
completion_( | ||
std::make_shared<std::vector<PipelineResult>>( | ||
std::initializer_list<PipelineResult>{PipelineResult::GetTestResult(firestore_)}), | ||
nullptr); | ||
} else { | ||
completion_(nullptr, MakeNSError(maybe_snapshot.status())); | ||
} | ||
} | ||
|
||
private: | ||
std::shared_ptr<api::Firestore> firestore_; | ||
PipelineBlock completion_; | ||
}; | ||
|
||
return absl::make_unique<Converter>(firestore, completion); | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <memory> | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace api { | ||
class Firestore; | ||
class PipelineResult; | ||
} // namespace api | ||
|
||
namespace core { | ||
template <typename T> | ||
class EventListener; | ||
} // namespace core | ||
|
||
} // namespace firestore | ||
} // namespace firebase | ||
|
||
namespace api = firebase::firestore::api; | ||
namespace core = firebase::firestore::core; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
typedef void (^PipelineBlock)(std::shared_ptr<std::vector<api::PipelineResult>> result, | ||
NSError *_Nullable error) | ||
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead."); | ||
|
||
typedef std::shared_ptr<std::vector<api::PipelineResult>> PipelineResultVectorPtr; | ||
|
||
NS_SWIFT_SENDABLE | ||
NS_SWIFT_NAME(CallbackWrapper) | ||
@interface FIRCallbackWrapper : NSObject | ||
|
||
+ (std::unique_ptr<core::EventListener<std::vector<api::PipelineResult>>>) | ||
wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore | ||
completion:(void (^)(std::shared_ptr<std::vector<api::PipelineResult>> result, | ||
NSError *_Nullable error))completion | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this intended to be exposed to customers? And should it use the ObjC types for it's parameters instead of C++ ones? e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason for using C++ types instead of Objective-C is that this class is intended to be used exclusively internally by the Swift layer and not by developers. The goal is to minimize the use of Objective-C wherever possible. |
||
NS_SWIFT_NAME(wrapPipelineCallback(firestore:completion:)); | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
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.
This is a newer learning, but it seems like making the callbacks in callback-based APIs Sendable so they can be invoked on any thread can prevent crashes when they do get called on a different thread. If this callback may be called on a different thread than the one it was defined in, then we should mark it as Sendable with.