Skip to content

Commit 5462e69

Browse files
committed
move callback wrapper
1 parent 9d9b7c2 commit 5462e69

File tree

12 files changed

+134
-94
lines changed

12 files changed

+134
-94
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "FIRCallbackWrapper.h"
18+
19+
#include <memory>
20+
#include <utility>
21+
#include <vector>
22+
23+
#include "Firestore/core/interfaceForSwift/api/pipeline.h"
24+
#include "Firestore/core/interfaceForSwift/api/pipeline_result.h"
25+
#include "Firestore/core/src/core/event_listener.h"
26+
#include "Firestore/core/src/util/error_apple.h"
27+
#include "Firestore/core/src/util/statusor.h"
28+
29+
using firebase::firestore::api::PipelineResult;
30+
using firebase::firestore::api::PipelineSnapshotListener;
31+
using firebase::firestore::core::EventListener;
32+
using firebase::firestore::util::MakeNSError;
33+
using firebase::firestore::util::StatusOr;
34+
35+
@implementation FIRCallbackWrapper
36+
37+
+ (PipelineSnapshotListener)
38+
wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore
39+
completion:(void (^)(std::shared_ptr<std::vector<PipelineResult>> result,
40+
NSError *_Nullable error))completion {
41+
class Converter : public EventListener<std::vector<PipelineResult>> {
42+
public:
43+
explicit Converter(std::shared_ptr<api::Firestore> firestore, PipelineBlock completion)
44+
: firestore_(firestore), completion_(completion) {
45+
}
46+
47+
void OnEvent(StatusOr<std::vector<PipelineResult>> maybe_snapshot) override {
48+
if (maybe_snapshot.ok()) {
49+
completion_(
50+
std::make_shared<std::vector<PipelineResult>>(
51+
std::initializer_list<PipelineResult>{PipelineResult::GetTestResult(firestore_)}),
52+
nullptr);
53+
} else {
54+
completion_(nullptr, MakeNSError(maybe_snapshot.status()));
55+
}
56+
}
57+
58+
private:
59+
std::shared_ptr<api::Firestore> firestore_;
60+
PipelineBlock completion_;
61+
};
62+
63+
return absl::make_unique<Converter>(firestore, completion);
64+
}
65+
66+
@end

Firestore/Source/API/FIRDocumentSnapshot.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ - (instancetype)initWithFirestore:(FIRFirestore *)firestore
8686
metadata:(SnapshotMetadata)metadata {
8787
DocumentSnapshot wrapped;
8888
if (document.has_value()) {
89-
wrapped =
90-
DocumentSnapshot::FromDocument(firestore.cppFirestorePtr, document.value(), std::move(metadata));
89+
wrapped = DocumentSnapshot::FromDocument(firestore.cppFirestorePtr, document.value(),
90+
std::move(metadata));
9191
} else {
9292
wrapped = DocumentSnapshot::FromNoDocument(firestore.cppFirestorePtr, std::move(documentKey),
9393
std::move(metadata));

Firestore/Source/API/FIRQuery.mm

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@
6363
#include "Firestore/core/src/util/exception.h"
6464
#include "Firestore/core/src/util/statusor.h"
6565
#include "Firestore/core/src/util/string_apple.h"
66-
#include "Firestore/core/interfaceForSwift/api/pipeline.h"
67-
#include "Firestore/core/interfaceForSwift/api/pipeline_result.h"
6866
#include "absl/strings/match.h"
6967

7068
namespace nanopb = firebase::firestore::nanopb;
@@ -73,8 +71,6 @@
7371
using firebase::firestore::google_firestore_v1_Value_fields;
7472
using firebase::firestore::api::Firestore;
7573
using firebase::firestore::api::MakeListenSource;
76-
using firebase::firestore::api::PipelineResult;
77-
using firebase::firestore::api::PipelineSnapshotListener;
7874
using firebase::firestore::api::Query;
7975
using firebase::firestore::api::QueryListenerRegistration;
8076
using firebase::firestore::api::QuerySnapshot;
@@ -179,35 +175,6 @@ - (void)getDocumentsWithCompletion:(void (^)(FIRQuerySnapshot *_Nullable snapsho
179175
_query.GetDocuments(Source::Default, [self wrapQuerySnapshotBlock:completion]);
180176
}
181177

182-
+ (PipelineSnapshotListener)
183-
wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore
184-
completion:(void (^)(std::shared_ptr<std::vector<PipelineResult>> result,
185-
NSError *_Nullable error))completion {
186-
class Converter : public EventListener<std::vector<PipelineResult>> {
187-
public:
188-
explicit Converter(std::shared_ptr<api::Firestore> firestore, PipelineBlock completion)
189-
: firestore_(firestore), completion_(completion) {
190-
}
191-
192-
void OnEvent(StatusOr<std::vector<PipelineResult>> maybe_snapshot) override {
193-
if (maybe_snapshot.ok()) {
194-
completion_(
195-
std::make_shared<std::vector<PipelineResult>>(
196-
std::initializer_list<PipelineResult>{PipelineResult::GetTestResult(firestore_)}),
197-
nullptr);
198-
} else {
199-
completion_(nullptr, MakeNSError(maybe_snapshot.status()));
200-
}
201-
}
202-
203-
private:
204-
std::shared_ptr<api::Firestore> firestore_;
205-
PipelineBlock completion_;
206-
};
207-
208-
return absl::make_unique<Converter>(firestore, completion);
209-
}
210-
211178
- (void)getDocumentsWithSource:(FIRFirestoreSource)publicSource
212179
completion:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
213180
NSError *_Nullable error))completion {
@@ -567,29 +534,6 @@ void OnEvent(StatusOr<QuerySnapshot> maybe_snapshot) override {
567534
return absl::make_unique<Converter>(block);
568535
}
569536

570-
+ (QuerySnapshotListener)wrapPipelineCallbackBlock:(FIRQuerySnapshotBlock)block {
571-
class Converter : public EventListener<QuerySnapshot> {
572-
public:
573-
explicit Converter(FIRQuerySnapshotBlock block) : block_(block) {
574-
}
575-
576-
void OnEvent(StatusOr<QuerySnapshot> maybe_snapshot) override {
577-
if (maybe_snapshot.ok()) {
578-
FIRQuerySnapshot *result =
579-
[[FIRQuerySnapshot alloc] initWithSnapshot:std::move(maybe_snapshot).ValueOrDie()];
580-
block_(result, nil);
581-
} else {
582-
block_(nil, MakeNSError(maybe_snapshot.status()));
583-
}
584-
}
585-
586-
private:
587-
FIRQuerySnapshotBlock block_;
588-
};
589-
590-
return absl::make_unique<Converter>(block);
591-
}
592-
593537
- (Filter)parseFieldFilter:(FSTUnaryFilter *)unaryFilter {
594538
auto describer = [&unaryFilter] {
595539
return MakeString(NSStringFromClass([unaryFilter.value class]));
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
#import <memory>
19+
20+
namespace firebase {
21+
namespace firestore {
22+
namespace api {
23+
class Firestore;
24+
class PipelineResult;
25+
} // namespace api
26+
27+
namespace core {
28+
template <typename T>
29+
class EventListener;
30+
} // namespace core
31+
32+
} // namespace firestore
33+
} // namespace firebase
34+
35+
namespace api = firebase::firestore::api;
36+
namespace core = firebase::firestore::core;
37+
38+
NS_ASSUME_NONNULL_BEGIN
39+
40+
typedef void (^PipelineBlock)(std::shared_ptr<std::vector<api::PipelineResult>> result,
41+
NSError *_Nullable error)
42+
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
43+
44+
typedef std::shared_ptr<std::vector<api::PipelineResult>> PipelineResultVectorPtr;
45+
46+
NS_SWIFT_SENDABLE
47+
NS_SWIFT_NAME(CallbackWrapper)
48+
@interface FIRCallbackWrapper : NSObject
49+
50+
+ (std::unique_ptr<core::EventListener<std::vector<api::PipelineResult>>>)
51+
wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore
52+
completion:(void (^)(std::shared_ptr<std::vector<api::PipelineResult>> result,
53+
NSError *_Nullable error))completion
54+
NS_SWIFT_NAME(wrapPipelineCallback(firestore:completion:));
55+
56+
@end
57+
58+
NS_ASSUME_NONNULL_END

Firestore/Source/Public/FirebaseFirestore/FIRQuery.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,6 @@
2929
@class FIRQuerySnapshot;
3030
@class FIRDocumentSnapshot;
3131

32-
namespace firebase {
33-
namespace firestore {
34-
namespace api {
35-
class Firestore;
36-
class PipelineResult;
37-
} // namespace api
38-
39-
namespace core {
40-
template <typename T>
41-
class EventListener;
42-
} // namespace core
43-
44-
} // namespace firestore
45-
} // namespace firebase
46-
47-
namespace api = firebase::firestore::api;
48-
namespace core = firebase::firestore::core;
49-
5032
NS_ASSUME_NONNULL_BEGIN
5133

5234
/**
@@ -56,12 +38,6 @@ typedef void (^FIRQuerySnapshotBlock)(FIRQuerySnapshot *_Nullable snapshot,
5638
NSError *_Nullable error)
5739
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
5840

59-
typedef void (^PipelineBlock)(std::shared_ptr<std::vector<api::PipelineResult>> result,
60-
NSError *_Nullable error)
61-
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
62-
63-
typedef std::shared_ptr<std::vector<api::PipelineResult>> PipelineResultVectorPtr;
64-
6541
/**
6642
* A `Query` refers to a query which you can read or listen to. You can also construct
6743
* refined `Query` objects by adding filters and ordering.
@@ -91,11 +67,6 @@ NS_SWIFT_NAME(Query)
9167
(void (^)(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error))completion
9268
NS_SWIFT_NAME(getDocuments(completion:));
9369

94-
+ (std::unique_ptr<core::EventListener<std::vector<api::PipelineResult>>>)
95-
wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore
96-
completion:(void (^)(std::shared_ptr<std::vector<api::PipelineResult>> result,
97-
NSError *_Nullable error))completion
98-
NS_SWIFT_NAME(wrapPipelineCallback(firestore:completion:));
9970
/**
10071
* Reads the documents matching this query.
10172
*

Firestore/Source/Public/FirebaseFirestore/FirebaseFirestore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#import "FIRAggregateQuery.h"
1919
#import "FIRAggregateQuerySnapshot.h"
2020
#import "FIRAggregateSource.h"
21+
#import "FIRCallbackWrapper.h"
2122
#import "FIRCollectionReference.h"
2223
#import "FIRDocumentChange.h"
2324
#import "FIRDocumentReference.h"

Firestore/Swift/Source/AsyncAwait/Firestore+AsyncAwait.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616

1717
#if SWIFT_PACKAGE
18-
@_exported import FirebaseFirestoreInternalWrapper
1918
import FirebaseFirestoreCpp
19+
@_exported import FirebaseFirestoreInternalWrapper
2020
#else
2121
@_exported import FirebaseFirestoreInternal
2222
#endif // SWIFT_PACKAGE

Firestore/Swift/Source/SwiftAPI/Pipeline.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
#if SWIFT_PACKAGE
9-
import FirebaseFirestoreCpp
9+
import FirebaseFirestoreCpp
1010
#endif
1111

1212
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
@@ -20,7 +20,7 @@ public class Pipeline {
2020
@discardableResult
2121
public func GetPipelineResult() async throws -> [PipelineResult] {
2222
return try await withCheckedThrowingContinuation { continuation in
23-
let listener = Query.wrapPipelineCallback(firestore: cppObj.GetFirestore()) {
23+
let listener = CallbackWrapper.wrapPipelineCallback(firestore: cppObj.GetFirestore()) {
2424
result, error in
2525
if let error {
2626
continuation.resume(throwing: error)

Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Cheryl Lin on 2024-12-18.
66
//
77
#if SWIFT_PACKAGE
8-
import FirebaseFirestoreCpp
8+
import FirebaseFirestoreCpp
99
#endif
1010

1111
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)

Firestore/core/interfaceForSwift/api/pipeline.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#include <memory>
55

66
#include "Firestore/core/include/firebase/firestore/timestamp.h"
7+
#include "Firestore/core/interfaceForSwift/api/pipeline_result.h"
78
#include "Firestore/core/src/api/firestore.h"
89
#include "Firestore/core/src/api/listener_registration.h"
910
#include "Firestore/core/src/api/source.h"
1011
#include "Firestore/core/src/core/event_listener.h"
1112
#include "Firestore/core/src/core/listen_options.h"
1213
#include "Firestore/core/src/core/view_snapshot.h"
13-
#include "Firestore/core/interfaceForSwift/api/pipeline_result.h"
1414

1515
namespace firebase {
1616
namespace firestore {

0 commit comments

Comments
 (0)