Skip to content

Commit

Permalink
move callback wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
cherylEnkidu committed Jan 7, 2025
1 parent 9d9b7c2 commit 5462e69
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 94 deletions.
66 changes: 66 additions & 0 deletions Firestore/Source/API/FIRCallbackWrapper.mm
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
4 changes: 2 additions & 2 deletions Firestore/Source/API/FIRDocumentSnapshot.mm
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ - (instancetype)initWithFirestore:(FIRFirestore *)firestore
metadata:(SnapshotMetadata)metadata {
DocumentSnapshot wrapped;
if (document.has_value()) {
wrapped =
DocumentSnapshot::FromDocument(firestore.cppFirestorePtr, document.value(), std::move(metadata));
wrapped = DocumentSnapshot::FromDocument(firestore.cppFirestorePtr, document.value(),
std::move(metadata));
} else {
wrapped = DocumentSnapshot::FromNoDocument(firestore.cppFirestorePtr, std::move(documentKey),
std::move(metadata));
Expand Down
56 changes: 0 additions & 56 deletions Firestore/Source/API/FIRQuery.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
#include "Firestore/core/src/util/exception.h"
#include "Firestore/core/src/util/statusor.h"
#include "Firestore/core/src/util/string_apple.h"
#include "Firestore/core/interfaceForSwift/api/pipeline.h"
#include "Firestore/core/interfaceForSwift/api/pipeline_result.h"
#include "absl/strings/match.h"

namespace nanopb = firebase::firestore::nanopb;
Expand All @@ -73,8 +71,6 @@
using firebase::firestore::google_firestore_v1_Value_fields;
using firebase::firestore::api::Firestore;
using firebase::firestore::api::MakeListenSource;
using firebase::firestore::api::PipelineResult;
using firebase::firestore::api::PipelineSnapshotListener;
using firebase::firestore::api::Query;
using firebase::firestore::api::QueryListenerRegistration;
using firebase::firestore::api::QuerySnapshot;
Expand Down Expand Up @@ -179,35 +175,6 @@ - (void)getDocumentsWithCompletion:(void (^)(FIRQuerySnapshot *_Nullable snapsho
_query.GetDocuments(Source::Default, [self wrapQuerySnapshotBlock:completion]);
}

+ (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);
}

- (void)getDocumentsWithSource:(FIRFirestoreSource)publicSource
completion:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
NSError *_Nullable error))completion {
Expand Down Expand Up @@ -567,29 +534,6 @@ void OnEvent(StatusOr<QuerySnapshot> maybe_snapshot) override {
return absl::make_unique<Converter>(block);
}

+ (QuerySnapshotListener)wrapPipelineCallbackBlock:(FIRQuerySnapshotBlock)block {
class Converter : public EventListener<QuerySnapshot> {
public:
explicit Converter(FIRQuerySnapshotBlock block) : block_(block) {
}

void OnEvent(StatusOr<QuerySnapshot> maybe_snapshot) override {
if (maybe_snapshot.ok()) {
FIRQuerySnapshot *result =
[[FIRQuerySnapshot alloc] initWithSnapshot:std::move(maybe_snapshot).ValueOrDie()];
block_(result, nil);
} else {
block_(nil, MakeNSError(maybe_snapshot.status()));
}
}

private:
FIRQuerySnapshotBlock block_;
};

return absl::make_unique<Converter>(block);
}

- (Filter)parseFieldFilter:(FSTUnaryFilter *)unaryFilter {
auto describer = [&unaryFilter] {
return MakeString(NSStringFromClass([unaryFilter.value class]));
Expand Down
58 changes: 58 additions & 0 deletions Firestore/Source/Public/FirebaseFirestore/FIRCallbackWrapper.h
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
NS_SWIFT_NAME(wrapPipelineCallback(firestore:completion:));

@end

NS_ASSUME_NONNULL_END
29 changes: 0 additions & 29 deletions Firestore/Source/Public/FirebaseFirestore/FIRQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,6 @@
@class FIRQuerySnapshot;
@class FIRDocumentSnapshot;

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

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

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;

/**
* A `Query` refers to a query which you can read or listen to. You can also construct
* refined `Query` objects by adding filters and ordering.
Expand Down Expand Up @@ -91,11 +67,6 @@ NS_SWIFT_NAME(Query)
(void (^)(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error))completion
NS_SWIFT_NAME(getDocuments(completion:));

+ (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
NS_SWIFT_NAME(wrapPipelineCallback(firestore:completion:));
/**
* Reads the documents matching this query.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "FIRAggregateQuery.h"
#import "FIRAggregateQuerySnapshot.h"
#import "FIRAggregateSource.h"
#import "FIRCallbackWrapper.h"
#import "FIRCollectionReference.h"
#import "FIRDocumentChange.h"
#import "FIRDocumentReference.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

#if SWIFT_PACKAGE
@_exported import FirebaseFirestoreInternalWrapper
import FirebaseFirestoreCpp
@_exported import FirebaseFirestoreInternalWrapper
#else
@_exported import FirebaseFirestoreInternal
#endif // SWIFT_PACKAGE
Expand Down
4 changes: 2 additions & 2 deletions Firestore/Swift/Source/SwiftAPI/Pipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

#if SWIFT_PACKAGE
import FirebaseFirestoreCpp
import FirebaseFirestoreCpp
#endif

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
Expand All @@ -20,7 +20,7 @@ public class Pipeline {
@discardableResult
public func GetPipelineResult() async throws -> [PipelineResult] {
return try await withCheckedThrowingContinuation { continuation in
let listener = Query.wrapPipelineCallback(firestore: cppObj.GetFirestore()) {
let listener = CallbackWrapper.wrapPipelineCallback(firestore: cppObj.GetFirestore()) {

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / platforms (catalyst)

cannot find 'CallbackWrapper' in scope

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / iOS-Device (macos-14, Xcode_15.3)

cannot find 'CallbackWrapper' in scope

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-14, Xcode_15.3, spm)

cannot find 'CallbackWrapper' in scope

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / client-app-cocoapods (ClientApp-CocoaPods)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (iOS)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (iOS)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (iOS)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (iOS)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (iOS)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (iOS)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (iOS)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (iOS)

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / storage-combine-integration

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / storage-combine-integration

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / storage-combine-integration

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / storage-combine-integration

type 'CallbackWrapper' has no member 'wrapPipelineCallback'

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / installation-test

type 'CallbackWrapper' has no member 'wrapPipelineCallback'
result, error in
if let error {
continuation.resume(throwing: error)
Expand Down
2 changes: 1 addition & 1 deletion Firestore/Swift/Source/SwiftAPI/PipelineResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Cheryl Lin on 2024-12-18.
//
#if SWIFT_PACKAGE
import FirebaseFirestoreCpp
import FirebaseFirestoreCpp
#endif

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/interfaceForSwift/api/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include <memory>

#include "Firestore/core/include/firebase/firestore/timestamp.h"
#include "Firestore/core/interfaceForSwift/api/pipeline_result.h"
#include "Firestore/core/src/api/firestore.h"
#include "Firestore/core/src/api/listener_registration.h"
#include "Firestore/core/src/api/source.h"
#include "Firestore/core/src/core/event_listener.h"
#include "Firestore/core/src/core/listen_options.h"
#include "Firestore/core/src/core/view_snapshot.h"
#include "Firestore/core/interfaceForSwift/api/pipeline_result.h"

namespace firebase {
namespace firestore {
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/interfaceForSwift/api/pipeline_source.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Firestore/core/interfaceForSwift/api/pipeline_source.h"
#include "Firestore/core/interfaceForSwift/api/collection_stage.h"
#include "Firestore/core/src/api/document_reference.h"
#include "Firestore/core/src/api/firestore.h"
#include "Firestore/core/interfaceForSwift/api/collection_stage.h"

namespace firebase {
namespace firestore {
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/api/firestore.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <mutex> // NOLINT(build/c++11)
#include <string>

#include "Firestore/core/interfaceForSwift/api/pipeline_source.h"
#include "Firestore/core/src/api/api_fwd.h"
#include "Firestore/core/src/api/load_bundle_task.h"
#include "Firestore/core/src/api/settings.h"
Expand All @@ -29,7 +30,6 @@
#include "Firestore/core/src/model/database_id.h"
#include "Firestore/core/src/util/byte_stream.h"
#include "Firestore/core/src/util/status_fwd.h"
#include "Firestore/core/interfaceForSwift/api/pipeline_source.h"

namespace firebase {
namespace firestore {
Expand Down

0 comments on commit 5462e69

Please sign in to comment.