Skip to content

Commit ce6e1c9

Browse files
authored
fix(firestore): not passing correctly the ListenSource when listening to as single DocumentReference (#13179)
* chore(firestore): updating source naming and requirement for platform channel * chore(firestore): updating source naming and requirement for platform channel * add tests
1 parent c0e0c99 commit ce6e1c9

File tree

10 files changed

+41
-14
lines changed

10 files changed

+41
-14
lines changed

packages/cloud_firestore/cloud_firestore/example/integration_test/document_reference_e2e.dart

+24
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,30 @@ void runDocumentReferenceTests() {
110110
});
111111
});
112112

113+
testWidgets('listens to a document from cache', (_) async {
114+
DocumentReference<Map<String, dynamic>> document =
115+
await initializeTest('document-snapshot-cache');
116+
await document.set({'foo': 'bar'});
117+
Stream<DocumentSnapshot<Map<String, dynamic>>> stream =
118+
document.snapshots(source: ListenSource.cache);
119+
StreamSubscription<DocumentSnapshot<Map<String, dynamic>>>?
120+
subscription;
121+
122+
subscription = stream.listen(
123+
expectAsync1(
124+
(DocumentSnapshot<Map<String, dynamic>> snapshot) {
125+
expect(snapshot.exists, isTrue);
126+
expect(snapshot.data(), equals({'foo': 'bar'}));
127+
},
128+
reason: 'Stream should only have been called once.',
129+
),
130+
);
131+
132+
addTearDown(() async {
133+
await subscription?.cancel();
134+
});
135+
});
136+
113137
testWidgets('listens to multiple documents', (_) async {
114138
DocumentReference<Map<String, dynamic>> doc1 =
115139
await initializeTest('document-snapshot-1');

packages/cloud_firestore/cloud_firestore/lib/src/document_reference.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ class _JsonDocumentReference
167167
}
168168

169169
return _delegate
170-
.snapshots(includeMetadataChanges: includeMetadataChanges)
170+
.snapshots(
171+
includeMetadataChanges: includeMetadataChanges,
172+
listenSource: source,
173+
)
171174
.map(
172175
(delegateSnapshot) =>
173176
_JsonDocumentSnapshot(firestore, delegateSnapshot),

packages/cloud_firestore/cloud_firestore/lib/src/query.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class _JsonQuery implements Query<Map<String, dynamic>> {
466466
return _delegate
467467
.snapshots(
468468
includeMetadataChanges: includeMetadataChanges,
469-
source: source,
469+
listenSource: source,
470470
)
471471
.map((item) => _JsonQuerySnapshot(firestore, item));
472472
}

packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/method_channel/method_channel_document_reference.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class MethodChannelDocumentReference extends DocumentReferencePlatform {
110110
bool includeMetadataChanges = false,
111111
ServerTimestampBehavior serverTimestampBehavior =
112112
ServerTimestampBehavior.none,
113-
ListenSource source = ListenSource.defaultSource,
113+
required ListenSource listenSource,
114114
}) {
115115
// It's fine to let the StreamController be garbage collected once all the
116116
// subscribers have cancelled; this analyzer warning is safe to ignore.
@@ -128,7 +128,7 @@ class MethodChannelDocumentReference extends DocumentReferencePlatform {
128128
serverTimestampBehavior: serverTimestampBehavior,
129129
),
130130
includeMetadataChanges,
131-
source,
131+
listenSource,
132132
);
133133
snapshotStreamSubscription =
134134
MethodChannelFirebaseFirestore.documentSnapshotChannel(observerId)

packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/method_channel/method_channel_query.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class MethodChannelQuery extends QueryPlatform {
153153
bool includeMetadataChanges = false,
154154
ServerTimestampBehavior serverTimestampBehavior =
155155
ServerTimestampBehavior.none,
156-
ListenSource source = ListenSource.defaultSource,
156+
required ListenSource listenSource,
157157
}) {
158158
// It's fine to let the StreamController be garbage collected once all the
159159
// subscribers have cancelled; this analyzer warning is safe to ignore.
@@ -175,7 +175,7 @@ class MethodChannelQuery extends QueryPlatform {
175175
serverTimestampBehavior: serverTimestampBehavior,
176176
),
177177
includeMetadataChanges,
178-
source,
178+
listenSource,
179179
);
180180

181181
snapshotStreamSubscription =

packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/platform_interface/platform_interface_document_reference.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ abstract class DocumentReferencePlatform extends PlatformInterface {
7373
/// Notifies of documents at this location
7474
Stream<DocumentSnapshotPlatform> snapshots({
7575
bool includeMetadataChanges = false,
76-
ListenSource source = ListenSource.defaultSource,
76+
required ListenSource listenSource,
7777
}) {
7878
throw UnimplementedError('snapshots() is not implemented');
7979
}

packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/platform_interface/platform_interface_query.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ abstract class QueryPlatform extends PlatformInterface {
135135
/// Notifies of query results at this location
136136
Stream<QuerySnapshotPlatform> snapshots({
137137
bool includeMetadataChanges = false,
138-
ListenSource source = ListenSource.defaultSource,
138+
required ListenSource listenSource,
139139
}) {
140140
throw UnimplementedError('snapshots() is not implemented');
141141
}

packages/cloud_firestore/cloud_firestore_web/lib/src/document_reference_web.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ class DocumentReferenceWeb extends DocumentReferencePlatform {
6767
@override
6868
Stream<DocumentSnapshotPlatform> snapshots({
6969
bool includeMetadataChanges = false,
70-
ListenSource source = ListenSource.defaultSource,
70+
required ListenSource listenSource,
7171
}) {
7272
Stream<firestore_interop.DocumentSnapshot> querySnapshots =
7373
_delegate.onSnapshot(
7474
includeMetadataChanges: includeMetadataChanges,
75-
source: source,
75+
source: listenSource,
7676
);
7777

7878
return convertWebExceptions(

packages/cloud_firestore/cloud_firestore_web/lib/src/interop/firestore.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,12 @@ class Query<T extends firestore_interop.QueryJsImpl>
553553

554554
Stream<QuerySnapshot> onSnapshot(
555555
{bool includeMetadataChanges = false,
556-
ListenSource source = ListenSource.defaultSource,
556+
required ListenSource listenSource,
557557
required int hashCode}) =>
558558
_createSnapshotStream(
559559
firestore_interop.DocumentListenOptions(
560560
includeMetadataChanges: includeMetadataChanges.toJS,
561-
source: convertListenSource(source),
561+
source: convertListenSource(listenSource),
562562
),
563563
hashCode,
564564
).stream;

packages/cloud_firestore/cloud_firestore_web/lib/src/query_web.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ class QueryWeb extends QueryPlatform {
182182
@override
183183
Stream<QuerySnapshotPlatform> snapshots({
184184
bool includeMetadataChanges = false,
185-
ListenSource source = ListenSource.defaultSource,
185+
required ListenSource listenSource,
186186
}) {
187187
Stream<firestore_interop.QuerySnapshot> querySnapshots =
188188
_buildWebQueryWithParameters().onSnapshot(
189189
includeMetadataChanges: includeMetadataChanges,
190-
source: source,
190+
listenSource: listenSource,
191191
hashCode: hashCode,
192192
);
193193

0 commit comments

Comments
 (0)