Skip to content

Commit 8ceb461

Browse files
fix(firestore, web): More explicit interop types (#17818)
* fix(firestore, web): More explicit interop types * fix: JSPromise types * fix: lint * fix: unneccesary cast * chore: comment out lint error
1 parent 311a57c commit 8ceb461

File tree

3 files changed

+36
-132
lines changed

3 files changed

+36
-132
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,9 @@ class CollectionReference<T extends firestore_interop.CollectionReferenceJsImpl>
719719
return _expando[jsObject] ??= CollectionReference._fromJsObject(jsObject);
720720
}
721721

722-
factory CollectionReference() => CollectionReference._fromJsObject(
723-
firestore_interop.CollectionReferenceJsImpl());
722+
factory CollectionReference(
723+
firestore_interop.CollectionReferenceJsImpl jsObject) =>
724+
CollectionReference._fromJsObject(jsObject);
724725

725726
CollectionReference._fromJsObject(
726727
firestore_interop.CollectionReferenceJsImpl jsObject)
@@ -730,8 +731,7 @@ class CollectionReference<T extends firestore_interop.CollectionReferenceJsImpl>
730731
final future =
731732
firestore_interop.addDoc(jsObject, jsify(data)! as JSObject).toDart;
732733
final result = await future;
733-
return DocumentReference.getInstance(
734-
(result)! as firestore_interop.DocumentReferenceJsImpl);
734+
return DocumentReference.getInstance(result);
735735
}
736736

737737
DocumentReference doc([String? documentPath]) {

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

Lines changed: 30 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ external FirestoreJsImpl initializeFirestore(
2727
@staticInterop
2828

2929
/// Type DocumentReferenceJsImpl
30-
external JSPromise addDoc(
30+
external JSPromise<DocumentReferenceJsImpl> addDoc(
3131
CollectionReferenceJsImpl reference,
3232
JSAny data,
3333
);
@@ -123,37 +123,37 @@ external JSPromise enableNetwork(FirestoreJsImpl firestore);
123123

124124
@JS()
125125
@staticInterop
126-
external JSPromise getDoc(
126+
external JSPromise<DocumentSnapshotJsImpl> getDoc(
127127
DocumentReferenceJsImpl reference,
128128
);
129129

130130
@JS()
131131
@staticInterop
132-
external JSPromise getDocFromCache(
132+
external JSPromise<DocumentSnapshotJsImpl> getDocFromCache(
133133
DocumentReferenceJsImpl reference,
134134
);
135135

136136
@JS()
137137
@staticInterop
138-
external JSPromise getDocFromServer(
138+
external JSPromise<DocumentSnapshotJsImpl> getDocFromServer(
139139
DocumentReferenceJsImpl reference,
140140
);
141141

142142
@JS()
143143
@staticInterop
144-
external JSPromise getDocs(
144+
external JSPromise<QuerySnapshotJsImpl> getDocs(
145145
QueryJsImpl query,
146146
);
147147

148148
@JS()
149149
@staticInterop
150-
external JSPromise getDocsFromCache(
150+
external JSPromise<QuerySnapshotJsImpl> getDocsFromCache(
151151
QueryJsImpl query,
152152
);
153153

154154
@JS()
155155
@staticInterop
156-
external JSPromise getDocsFromServer(
156+
external JSPromise<QuerySnapshotJsImpl> getDocsFromServer(
157157
QueryJsImpl query,
158158
);
159159

@@ -344,11 +344,7 @@ extension FirestoreJsImplExtension on FirestoreJsImpl {
344344
external JSString get type;
345345
}
346346

347-
@JS('WriteBatch')
348-
@staticInterop
349-
abstract class WriteBatchJsImpl {}
350-
351-
extension WriteBatchJsImplExtension on WriteBatchJsImpl {
347+
extension type WriteBatchJsImpl._(JSObject _) implements JSObject {
352348
external JSPromise commit();
353349

354350
external WriteBatchJsImpl delete(DocumentReferenceJsImpl documentRef);
@@ -363,13 +359,7 @@ extension WriteBatchJsImplExtension on WriteBatchJsImpl {
363359
);
364360
}
365361

366-
@JS('CollectionReference')
367-
@staticInterop
368-
class CollectionReferenceJsImpl extends QueryJsImpl {
369-
external factory CollectionReferenceJsImpl();
370-
}
371-
372-
extension CollectionReferenceJsImplExtension on CollectionReferenceJsImpl {
362+
extension type CollectionReferenceJsImpl._(JSObject _) implements QueryJsImpl {
373363
external JSString get id;
374364
external DocumentReferenceJsImpl get parent;
375365
external JSString get path;
@@ -430,11 +420,7 @@ extension GeoPointJsImplExtension on GeoPointJsImpl {
430420
@staticInterop
431421
external VectorValueJsImpl get VectorValueConstructor;
432422

433-
@JS('VectorValue')
434-
@staticInterop
435-
class VectorValueJsImpl {}
436-
437-
extension VectorValueJsImplExtension on VectorValueJsImpl {
423+
extension type VectorValueJsImpl._(JSObject _) implements JSObject {
438424
external JSArray toArray();
439425
}
440426

@@ -464,12 +450,7 @@ extension BytesJsImplExtension on BytesJsImpl {
464450
external JSBoolean isEqual(JSObject other);
465451
}
466452

467-
@anonymous
468-
@JS()
469-
@staticInterop
470-
abstract class DocumentChangeJsImpl {}
471-
472-
extension DocumentChangeJsImplExtension on DocumentChangeJsImpl {
453+
extension type DocumentChangeJsImpl._(JSObject _) implements JSObject {
473454
external JSString /*'added'|'removed'|'modified'*/ get type;
474455

475456
external set type(JSString /*'added'|'removed'|'modified'*/ v);
@@ -491,11 +472,7 @@ extension DocumentChangeJsImplExtension on DocumentChangeJsImpl {
491472
@staticInterop
492473
external DocumentReferenceJsImpl get DocumentReferenceJsConstructor;
493474

494-
@JS('DocumentReference')
495-
@staticInterop
496-
abstract class DocumentReferenceJsImpl {}
497-
498-
extension DocumentReferenceJsImplExtension on DocumentReferenceJsImpl {
475+
extension type DocumentReferenceJsImpl._(JSObject _) implements JSObject {
499476
external FirestoreJsImpl get firestore;
500477
external JSString get id;
501478
external CollectionReferenceJsImpl get parent;
@@ -511,11 +488,7 @@ extension QueryConstraintJsImplExtension on QueryConstraintJsImpl {
511488
external JSString get type;
512489
}
513490

514-
@JS('LoadBundleTask')
515-
@staticInterop
516-
abstract class LoadBundleTaskJsImpl {}
517-
518-
extension LoadBundleTaskJsImplExtension on LoadBundleTaskJsImpl {
491+
extension type LoadBundleTaskJsImpl._(JSObject _) implements JSObject {
519492
external void onProgress(
520493
JSFunction? next,
521494
);
@@ -526,13 +499,7 @@ extension LoadBundleTaskJsImplExtension on LoadBundleTaskJsImpl {
526499
]);
527500
}
528501

529-
@JS()
530-
@staticInterop
531-
@anonymous
532-
abstract class LoadBundleTaskProgressJsImpl {}
533-
534-
extension LoadBundleTaskProgressJsImplExtension
535-
on LoadBundleTaskProgressJsImpl {
502+
extension type LoadBundleTaskProgressJsImpl._(JSObject _) implements JSObject {
536503
// int or String?
537504
external JSAny get bytesLoaded;
538505

@@ -546,11 +513,7 @@ extension LoadBundleTaskProgressJsImplExtension
546513
external JSNumber get totalDocuments;
547514
}
548515

549-
@JS('DocumentSnapshot')
550-
@staticInterop
551-
abstract class DocumentSnapshotJsImpl {}
552-
553-
extension DocumentSnapshotJsImplExtension on DocumentSnapshotJsImpl {
516+
extension type DocumentSnapshotJsImpl._(JSObject _) implements JSObject {
554517
external JSString get id;
555518
external SnapshotMetadata get metadata;
556519
external DocumentReferenceJsImpl get ref;
@@ -564,12 +527,7 @@ extension DocumentSnapshotJsImplExtension on DocumentSnapshotJsImpl {
564527
/// [set()] or [update()].
565528
///
566529
/// See: <https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue>.
567-
@JS()
568-
@staticInterop
569-
@anonymous
570-
abstract class FieldValue {}
571-
572-
extension FieldValueExtension on FieldValue {
530+
extension type FieldValue._(JSObject _) implements JSObject {
573531
/// Returns `true` if this [FieldValue] is equal to the provided [other].
574532
external JSBoolean isEqual(FieldValue other);
575533
}
@@ -579,20 +537,12 @@ extension FieldValueExtension on FieldValue {
579537
@staticInterop
580538
external JSObject get fieldValues;
581539

582-
@JS('Query')
583-
@staticInterop
584-
abstract class QueryJsImpl {}
585-
586-
extension QueryJsImplExtension on QueryJsImpl {
540+
extension type QueryJsImpl._(JSObject _) implements JSObject {
587541
external FirestoreJsImpl get firestore;
588542
external JSString get type;
589543
}
590544

591-
@JS('QuerySnapshot')
592-
@staticInterop
593-
abstract class QuerySnapshotJsImpl {}
594-
595-
extension QuerySnapshotJsImplExtension on QuerySnapshotJsImpl {
545+
extension type QuerySnapshotJsImpl._(JSObject _) implements JSObject {
596546
external JSArray get docs;
597547
external JSBoolean get empty;
598548
external SnapshotMetadata get metadata;
@@ -607,11 +557,7 @@ extension QuerySnapshotJsImplExtension on QuerySnapshotJsImpl {
607557
]);
608558
}
609559

610-
@JS('Transaction')
611-
@staticInterop
612-
abstract class TransactionJsImpl {}
613-
614-
extension TransactionJsImplExtension on TransactionJsImpl {
560+
extension type TransactionJsImpl._(JSObject _) implements JSObject {
615561
external TransactionJsImpl delete(DocumentReferenceJsImpl documentRef);
616562

617563
external JSPromise get(DocumentReferenceJsImpl documentRef);
@@ -796,66 +742,36 @@ abstract class FirestoreLocalCache {}
796742
///
797743
/// To use, create an instance using the factory function , then set the instance to FirestoreSettings.cache
798744
/// and call initializeFirestore using the settings object.
799-
@anonymous
800-
@JS()
801-
@staticInterop
802-
abstract class MemoryLocalCache extends FirestoreLocalCache {}
803-
804-
extension MemoryLocalCacheExtension on MemoryLocalCache {
745+
extension type MemoryLocalCache._(JSObject _) implements JSObject {
805746
external JSString get kind;
806747
}
807748

808749
/// A tab manager supporting only one tab, no synchronization will be performed across tabs.
809-
@anonymous
810-
@JS()
811-
@staticInterop
812-
abstract class PersistentSingleTabManager {}
813-
814-
extension PersistentSingleTabManagerExtension on PersistentSingleTabManager {
750+
extension type PersistentSingleTabManager._(JSObject _) implements JSObject {
815751
external JSString get kind;
816752
}
817753

818754
/// A tab manager supporting multiple tabs. SDK will synchronize queries and mutations done across all tabs using the SDK.
819-
@anonymous
820-
@JS()
821-
@staticInterop
822-
abstract class PersistentMultipleTabManager {}
823-
824-
extension PersistentMultipleTabManagerExtension
825-
on PersistentMultipleTabManager {
755+
extension type PersistentMultipleTabManager._(JSObject _) implements JSObject {
826756
external JSString get kind;
827757
}
828758

829759
/// A garbage collector deletes documents whenever they are not part of any active queries, and have no local mutations attached to them.
830-
@anonymous
831-
@JS()
832-
@staticInterop
833-
abstract class MemoryEagerGarbageCollector {}
834-
835-
extension MemoryEagerGarbageCollectorExtension on MemoryEagerGarbageCollector {
760+
///
761+
extension type MemoryEagerGarbageCollector._(JSObject _) implements JSObject {
836762
external JSString get kind;
837763
}
838764

839765
/// A garbage collector deletes Least-Recently-Used documents in multiple batches.
840-
@anonymous
841-
@JS()
842-
@staticInterop
843-
abstract class MemoryLruGarbageCollector {}
844-
845-
extension MemoryLruGarbageCollectorExtension on MemoryLruGarbageCollector {
766+
extension type MemoryLruGarbageCollector._(JSObject _) implements JSObject {
846767
external JSString get kind;
847768
}
848769

849770
/// Provides an in-memory cache to the SDK. This is the default cache unless explicitly configured otherwise.
850771
///
851772
/// To use, create an instance using the factory function , then set the instance to FirestoreSettings.cache
852773
/// and call initializeFirestore using the settings object.
853-
@anonymous
854-
@JS()
855-
@staticInterop
856-
abstract class PersistentLocalCache extends FirestoreLocalCache {}
857-
858-
extension PersistentLocalCacheExtension on PersistentLocalCache {
774+
extension type PersistentLocalCache._(JSObject _) implements JSObject {
859775
external JSString get kind;
860776
}
861777

@@ -912,12 +828,8 @@ extension PersistentCacheSettingsExtension on PersistentCacheSettings {
912828
/// An settings object to configure an PersistentLocalCache instance.
913829
///
914830
/// See: <https://firebase.google.com/docs/reference/js/firestore_.persistentsingletabmanagersettings>.
915-
@JS()
916-
@staticInterop
917-
abstract class PersistentSingleTabManagerSettings {}
918-
919-
extension PersistentSingleTabManagerSettingsExtension
920-
on PersistentSingleTabManagerSettings {
831+
extension type PersistentSingleTabManagerSettings._(JSObject _)
832+
implements JSObject {
921833
/// Whether to force-enable persistent (IndexedDB) cache for the client.
922834
/// This cannot be used with multi-tab synchronization and is primarily
923835
/// intended for use with Web Workers.
@@ -931,11 +843,7 @@ extension PersistentSingleTabManagerSettingsExtension
931843
/// Metadata about a snapshot, describing the state of the snapshot.
932844
///
933845
/// See: <https://firebase.google.com/docs/reference/js/firebase.firestore.SnapshotMetadata>.
934-
@JS()
935-
@staticInterop
936-
abstract class SnapshotMetadata {}
937-
938-
extension SnapshotMetadataExtension on SnapshotMetadata {
846+
extension type SnapshotMetadata._(JSObject _) implements JSObject {
939847
/// [:true:] if the snapshot includes local writes (set() or update() calls)
940848
/// that haven't been committed to the backend yet. If your listener has opted
941849
/// into metadata updates via onDocumentMetadataSnapshot,
@@ -1091,12 +999,7 @@ external JSPromise getAggregateFromServer(
1091999
JSObject specs,
10921000
);
10931001

1094-
@JS('AggregateQuerySnapshot')
1095-
@staticInterop
1096-
abstract class AggregateQuerySnapshotJsImpl {}
1097-
1098-
extension AggregateQuerySnapshotJsImplExtension
1099-
on AggregateQuerySnapshotJsImpl {
1002+
extension type AggregateQuerySnapshotJsImpl._(JSObject _) implements JSObject {
11001003
external JSObject data();
11011004
}
11021005

packages/cloud_firestore/cloud_firestore_web/test/test_common.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const kCollectionId = 'test';
1414

1515
class MockWebDocumentSnapshot extends Mock implements web.DocumentSnapshot {}
1616

17-
class MockWebSnapshotMetaData extends Mock implements web.SnapshotMetadata {}
17+
// Lint error here but tests pass without this.
18+
// class MockWebSnapshotMetaData extends Mock implements web.SnapshotMetadata {}
1819

1920
class MockFirestoreWeb extends Mock implements web.Firestore {}
2021

0 commit comments

Comments
 (0)