Skip to content
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

Snapshot listener source from cache #12370

Merged
merged 20 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
change api interface
  • Loading branch information
milaGGL committed Feb 5, 2024
commit 37d49ac37d3f50eaf07f6bc760788694a9977226
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 <FirebaseFirestoreInternal/FIRSnapshotListenOptions.h>
7 changes: 7 additions & 0 deletions Firestore/Source/API/FIRDocumentReference.mm
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ - (void)getDocumentWithSource:(FIRFirestoreSource)source
return [self addSnapshotListenerInternalWithOptions:options listener:listener];
}

- (id<FIRListenerRegistration>)addSnapshotListenerWithOptions:(FIRSnapshotListenOptions *)options
listener:(FIRDocumentSnapshotBlock)listener {
// Mila
ListenOptions listenOptions = ListenOptions::FromIncludeMetadataChanges(false);
return [self addSnapshotListenerInternalWithOptions:listenOptions listener:listener];
}

- (id<FIRListenerRegistration>)addSnapshotListenerInternalWithOptions:(ListenOptions)internalOptions
listener:(FIRDocumentSnapshotBlock)
listener {
Expand Down
6 changes: 6 additions & 0 deletions Firestore/Source/API/FIRQuery.mm
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ - (void)getDocumentsWithSource:(FIRFirestoreSource)publicSource
return [self addSnapshotListenerInternalWithOptions:options listener:listener];
}

- (id<FIRListenerRegistration>)addSnapshotListenerWithOptions:(FIRSnapshotListenOptions *)options
listener:(FIRQuerySnapshotBlock)listener {
auto listenOptions = ListenOptions::FromIncludeMetadataChanges(false);
return [self addSnapshotListenerInternalWithOptions:listenOptions listener:listener];
}

- (id<FIRListenerRegistration>)addSnapshotListenerInternalWithOptions:(ListenOptions)internalOptions
listener:
(FIRQuerySnapshotBlock)listener {
Expand Down
64 changes: 64 additions & 0 deletions Firestore/Source/API/FIRSnapshotListenOptions.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2023 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 "FIRSnapshotListenOptions.h"

#import <Foundation/Foundation.h>

#include <cstdint>
#include <string>

NS_ASSUME_NONNULL_BEGIN

@implementation FIRSnapshotListenOptions

// private method
- (instancetype)initPrivateWithSource:(FIRListenSource)source
includeMetadataChanges:(BOOL)includeMetadataChanges {
self = [self init];
if (self) {
_source = source;
_includeMetadataChanges = includeMetadataChanges;
}
return self;
}

- (instancetype)init {
self = [super init];
if (self) {
_source = FIRListenSourceDefault;
_includeMetadataChanges = NO;
}
return self;
}

- (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges {
FIRSnapshotListenOptions *newOptions =
[[FIRSnapshotListenOptions alloc] initPrivateWithSource:self.source
includeMetadataChanges:includeMetadataChanges];
return newOptions;
}

- (FIRSnapshotListenOptions *)optionsWithSource:(FIRListenSource)source {
FIRSnapshotListenOptions *newOptions =
[[FIRSnapshotListenOptions alloc] initPrivateWithSource:source
includeMetadataChanges:self.includeMetadataChanges];
return newOptions;
}

@end

NS_ASSUME_NONNULL_END
18 changes: 18 additions & 0 deletions Firestore/Source/Public/FirebaseFirestore/FIRDocumentReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#import "FIRFirestoreSource.h"
#import "FIRListenerRegistration.h"
#import "FIRSnapshotListenOptions.h"

@class FIRCollectionReference;
@class FIRDocumentSnapshot;
Expand Down Expand Up @@ -270,6 +271,23 @@ addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:));
// clang-format on

/**
* Attaches a listener for `DocumentSnapshot` events.
*
* @param options Sets snapshot listen options, including whether metadata-only changes should
* trigger snapshot events, the source that listens to, the executor to use to call the
* listener, or the activity to scope the listener to.
* @param listener The listener to attach.
*
* @return A `ListenerRegistration` that can be used to remove this listener.
*/
// clang-format off
- (id<FIRListenerRegistration>)addSnapshotListenerWithOptions:
(FIRSnapshotListenOptions *)options
listener:(void (^)(FIRDocumentSnapshot *_Nullable snapshot,NSError *_Nullable error))listener
NS_SWIFT_NAME(addSnapshotListener(options:listener:));
// clang-format on

@end

NS_ASSUME_NONNULL_END
16 changes: 16 additions & 0 deletions Firestore/Source/Public/FirebaseFirestore/FIRQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#import "FIRFirestoreSource.h"
#import "FIRListenerRegistration.h"
#import "FIRSnapshotListenOptions.h"

@class FIRAggregateQuery;
@class FIRAggregateField;
Expand Down Expand Up @@ -104,6 +105,21 @@ NS_SWIFT_NAME(Query)
NSError *_Nullable error))listener
NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:));

/**
* Attaches a listener for `QuerySnapshot` events.
* @param options Sets snapshot listen options, including whether metadata-only changes should
* trigger snapshot events, the source that listens to, the executor to use to call the
* listener, or the activity to scope the listener to.
* @param listener The listener to attach.
*
* @return A `ListenerRegistration` that can be used to remove this listener.
*/
- (id<FIRListenerRegistration>)
addSnapshotListenerWithOptions:(FIRSnapshotListenOptions *)options
listener:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
NSError *_Nullable error))listener
NS_SWIFT_NAME(addSnapshotListener(options:listener:));

#pragma mark - Filtering Data
/**
* Creates and returns a new Query with the additional filter.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, FIRListenSource) {
FIRListenSourceDefault,
FIRListenSourceCache
} NS_SWIFT_NAME(ListenSource);

NS_SWIFT_NAME(SnapshotListenOptions)
@interface FIRSnapshotListenOptions : NSObject

@property(nonatomic, readonly) FIRListenSource source;
@property(nonatomic, readonly) BOOL includeMetadataChanges;

- (instancetype)init NS_DESIGNATED_INITIALIZER;

- (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges;
- (FIRSnapshotListenOptions *)optionsWithSource:(FIRListenSource)source;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#import "FIRLocalCacheSettings.h"
#import "FIRQuery.h"
#import "FIRQuerySnapshot.h"
#import "FIRSnapshotListenOptions.h"
#import "FIRSnapshotMetadata.h"
#import "FIRTimestamp.h"
#import "FIRTransaction.h"
Expand Down