Skip to content

Commit d127fa6

Browse files
committed
implement Compat in firestore-compat
1 parent 1659505 commit d127fa6

18 files changed

+98
-170
lines changed

common/api-review/firestore-exp.api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export class CollectionReference<T = DocumentData> extends Query<T> {
5353
get path(): string;
5454
// (undocumented)
5555
readonly type = "collection";
56+
withConverter(converter: null): CollectionReference<DocumentData>;
57+
// (undocumented)
5658
withConverter<U>(converter: FirestoreDataConverter<U>): CollectionReference<U>;
5759
}
5860

@@ -101,6 +103,8 @@ export class DocumentReference<T = DocumentData> {
101103
get parent(): CollectionReference<T>;
102104
get path(): string;
103105
readonly type = "document";
106+
withConverter(converter: null): DocumentReference<DocumentData>;
107+
// (undocumented)
104108
withConverter<U>(converter: FirestoreDataConverter<U>): DocumentReference<U>;
105109
}
106110

@@ -318,6 +322,8 @@ export class Query<T = DocumentData> {
318322
protected constructor();
319323
readonly firestore: FirebaseFirestore;
320324
readonly type: 'query' | 'collection';
325+
withConverter(converter: null): Query<DocumentData>;
326+
// (undocumented)
321327
withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
322328
}
323329

common/api-review/firestore-lite.api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export class CollectionReference<T = DocumentData> extends Query<T> {
4747
get path(): string;
4848
// (undocumented)
4949
readonly type = "collection";
50+
withConverter(converter: null): CollectionReference<DocumentData>;
51+
// (undocumented)
5052
withConverter<U>(converter: FirestoreDataConverter<U>): CollectionReference<U>;
5153
}
5254

@@ -81,6 +83,8 @@ export class DocumentReference<T = DocumentData> {
8183
get parent(): CollectionReference<T>;
8284
get path(): string;
8385
readonly type = "document";
86+
withConverter(converter: null): DocumentReference<DocumentData>;
87+
// (undocumented)
8488
withConverter<U>(converter: FirestoreDataConverter<U>): DocumentReference<U>;
8589
}
8690

@@ -195,6 +199,8 @@ export class Query<T = DocumentData> {
195199
protected constructor();
196200
readonly firestore: FirebaseFirestore;
197201
readonly type: 'query' | 'collection';
202+
withConverter(converter: null): Query<DocumentData>;
203+
// (undocumented)
198204
withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
199205
}
200206

packages/firestore/exp/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,3 @@ export { CACHE_SIZE_UNLIMITED } from '../src/exp/database';
133133
export { FirestoreErrorCode, FirestoreError } from '../src/util/error';
134134

135135
export { AbstractUserDataWriter } from '../src/lite/user_data_writer';
136-
137-
export { Compat } from '../src/lite/compat';

packages/firestore/externs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"packages/webchannel-wrapper/src/index.d.ts",
2828
"packages/util/dist/src/crypt.d.ts",
2929
"packages/util/dist/src/environment.d.ts",
30+
"packages/util/dist/src/compat.d.ts",
3031
"packages/firestore/export.ts",
3132
"packages/firestore/src/protos/firestore_bundle_proto.ts",
3233
"packages/firestore/src/protos/firestore_proto_api.ts",

packages/firestore/src/api/blob.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { Bytes, Compat } from '../../exp/index';
18+
import { Compat } from '@firebase/util';
19+
20+
import { Bytes } from '../../exp/index';
1921
import { isBase64Available } from '../platform/base64';
2022
import { Code, FirestoreError } from '../util/error';
2123

@@ -40,7 +42,8 @@ function assertBase64Available(): void {
4042
}
4143

4244
/** Immutable class holding a blob (binary data) */
43-
export class Blob extends Compat<Bytes> {
45+
export class Blob implements Compat<Bytes> {
46+
constructor(readonly _delegate: Bytes) {}
4447
static fromBase64String(base64: string): Blob {
4548
assertBase64Available();
4649
return new Blob(Bytes.fromBase64String(base64));

packages/firestore/src/api/database.ts

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
WhereFilterOp as PublicWhereFilterOp,
4545
WriteBatch as PublicWriteBatch
4646
} from '@firebase/firestore-types';
47+
import { Compat, getModularInstance } from '@firebase/util';
4748

4849
import {
4950
LoadBundleTask,
@@ -98,8 +99,7 @@ import {
9899
runTransaction,
99100
Transaction as ExpTransaction,
100101
WriteBatch as ExpWriteBatch,
101-
AbstractUserDataWriter,
102-
Compat
102+
AbstractUserDataWriter
103103
} from '../../exp/index'; // import from the exp public API
104104
import { DatabaseId } from '../core/database_info';
105105
import { UntypedFirestoreDataConverter } from '../lite/user_data_reader';
@@ -195,16 +195,13 @@ export class IndexedDbPersistenceProvider implements PersistenceProvider {
195195
* to the functional API of firestore-exp.
196196
*/
197197
export class Firestore
198-
extends Compat<ExpFirebaseFirestore>
199-
implements PublicFirestore, FirebaseService {
198+
implements PublicFirestore, FirebaseService, Compat<ExpFirebaseFirestore> {
200199
_appCompat?: FirebaseApp;
201200
constructor(
202201
databaseIdOrApp: DatabaseId | FirebaseApp,
203-
delegate: ExpFirebaseFirestore,
202+
readonly _delegate: ExpFirebaseFirestore,
204203
private _persistenceProvider: PersistenceProvider
205204
) {
206-
super(delegate);
207-
208205
if (!(databaseIdOrApp instanceof DatabaseId)) {
209206
this._appCompat = databaseIdOrApp as FirebaseApp;
210207
}
@@ -386,16 +383,13 @@ export function setLogLevel(level: PublicLogLevel): void {
386383
/**
387384
* A reference to a transaction.
388385
*/
389-
export class Transaction
390-
extends Compat<ExpTransaction>
391-
implements PublicTransaction {
386+
export class Transaction implements PublicTransaction, Compat<ExpTransaction> {
392387
private _userDataWriter: UserDataWriter;
393388

394389
constructor(
395390
private readonly _firestore: Firestore,
396-
delegate: ExpTransaction
391+
readonly _delegate: ExpTransaction
397392
) {
398-
super(delegate);
399393
this._userDataWriter = new UserDataWriter(_firestore);
400394
}
401395

@@ -480,9 +474,8 @@ export class Transaction
480474
}
481475
}
482476

483-
export class WriteBatch
484-
extends Compat<ExpWriteBatch>
485-
implements PublicWriteBatch {
477+
export class WriteBatch implements PublicWriteBatch, Compat<ExpWriteBatch> {
478+
constructor(readonly _delegate: ExpWriteBatch) {}
486479
set<T>(
487480
documentRef: DocumentReference<T>,
488481
data: Partial<T>,
@@ -551,17 +544,16 @@ export class WriteBatch
551544
* them to the wrapped converter.
552545
*/
553546
class FirestoreDataConverter<U>
554-
extends Compat<PublicFirestoreDataConverter<U>>
555-
implements UntypedFirestoreDataConverter<U> {
547+
implements
548+
UntypedFirestoreDataConverter<U>,
549+
Compat<PublicFirestoreDataConverter<U>> {
556550
private static readonly INSTANCES = new WeakMap();
557551

558552
private constructor(
559553
private readonly _firestore: Firestore,
560554
private readonly _userDataWriter: UserDataWriter,
561-
delegate: PublicFirestoreDataConverter<U>
562-
) {
563-
super(delegate);
564-
}
555+
readonly _delegate: PublicFirestoreDataConverter<U>
556+
) {}
565557

566558
fromFirestore(
567559
snapshot: ExpQueryDocumentSnapshot,
@@ -629,15 +621,13 @@ class FirestoreDataConverter<U>
629621
* A reference to a particular document in a collection in the database.
630622
*/
631623
export class DocumentReference<T = PublicDocumentData>
632-
extends Compat<ExpDocumentReference<T>>
633-
implements PublicDocumentReference<T> {
624+
implements PublicDocumentReference<T>, Compat<ExpDocumentReference<T>> {
634625
private _userDataWriter: UserDataWriter;
635626

636627
constructor(
637628
readonly firestore: Firestore,
638-
delegate: ExpDocumentReference<T>
629+
readonly _delegate: ExpDocumentReference<T>
639630
) {
640-
super(delegate);
641631
this._userDataWriter = new UserDataWriter(firestore);
642632
}
643633

@@ -705,9 +695,7 @@ export class DocumentReference<T = PublicDocumentData>
705695
}
706696

707697
isEqual(other: PublicDocumentReference<T>): boolean {
708-
if (other instanceof Compat) {
709-
other = other._delegate;
710-
}
698+
other = getModularInstance<PublicDocumentReference<T>>(other);
711699

712700
if (!(other instanceof ExpDocumentReference)) {
713701
return false;
@@ -917,14 +905,11 @@ export function wrapObserver<CompatType, ExpType>(
917905
export interface SnapshotOptions extends PublicSnapshotOptions {}
918906

919907
export class DocumentSnapshot<T = PublicDocumentData>
920-
extends Compat<ExpDocumentSnapshot<T>>
921-
implements PublicDocumentSnapshot<T> {
908+
implements PublicDocumentSnapshot<T>, Compat<ExpDocumentSnapshot<T>> {
922909
constructor(
923910
private readonly _firestore: Firestore,
924-
delegate: ExpDocumentSnapshot<T>
925-
) {
926-
super(delegate);
927-
}
911+
readonly _delegate: ExpDocumentSnapshot<T>
912+
) {}
928913

929914
get ref(): DocumentReference<T> {
930915
return new DocumentReference<T>(this._firestore, this._delegate.ref);
@@ -974,12 +959,10 @@ export class QueryDocumentSnapshot<T = PublicDocumentData>
974959
}
975960

976961
export class Query<T = PublicDocumentData>
977-
extends Compat<ExpQuery<T>>
978-
implements PublicQuery<T> {
962+
implements PublicQuery<T>, Compat<ExpQuery<T>> {
979963
private readonly _userDataWriter: UserDataWriter;
980964

981-
constructor(readonly firestore: Firestore, delegate: ExpQuery<T>) {
982-
super(delegate);
965+
constructor(readonly firestore: Firestore, readonly _delegate: ExpQuery<T>) {
983966
this._userDataWriter = new UserDataWriter(firestore);
984967
}
985968

@@ -1154,14 +1137,11 @@ export class Query<T = PublicDocumentData>
11541137
}
11551138

11561139
export class DocumentChange<T = PublicDocumentData>
1157-
extends Compat<ExpDocumentChange<T>>
1158-
implements PublicDocumentChange<T> {
1140+
implements PublicDocumentChange<T>, Compat<ExpDocumentChange<T>> {
11591141
constructor(
11601142
private readonly _firestore: Firestore,
1161-
delegate: ExpDocumentChange<T>
1162-
) {
1163-
super(delegate);
1164-
}
1143+
readonly _delegate: ExpDocumentChange<T>
1144+
) {}
11651145

11661146
get type(): PublicDocumentChangeType {
11671147
return this._delegate.type;
@@ -1181,11 +1161,11 @@ export class DocumentChange<T = PublicDocumentData>
11811161
}
11821162

11831163
export class QuerySnapshot<T = PublicDocumentData>
1184-
extends Compat<ExpQuerySnapshot<T>>
1185-
implements PublicQuerySnapshot<T> {
1186-
constructor(readonly _firestore: Firestore, delegate: ExpQuerySnapshot<T>) {
1187-
super(delegate);
1188-
}
1164+
implements PublicQuerySnapshot<T>, Compat<ExpQuerySnapshot<T>> {
1165+
constructor(
1166+
readonly _firestore: Firestore,
1167+
readonly _delegate: ExpQuerySnapshot<T>
1168+
) {}
11891169

11901170
get query(): Query<T> {
11911171
return new Query(this._firestore, this._delegate.query);
@@ -1306,8 +1286,5 @@ export class CollectionReference<T = PublicDocumentData>
13061286
function castReference<T>(
13071287
documentRef: PublicDocumentReference<T>
13081288
): ExpDocumentReference<T> {
1309-
if (documentRef instanceof Compat) {
1310-
documentRef = documentRef._delegate;
1311-
}
13121289
return cast<ExpDocumentReference<T>>(documentRef, ExpDocumentReference);
13131290
}

packages/firestore/src/api/field_path.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
*/
1717

1818
import { FieldPath as PublicFieldPath } from '@firebase/firestore-types';
19+
import { Compat, getModularInstance } from '@firebase/util';
1920

20-
import { FieldPath as ExpFieldPath, Compat } from '../../exp/index';
21+
import { FieldPath as ExpFieldPath } from '../../exp/index';
2122
import { FieldPath as InternalFieldPath } from '../model/path';
2223

2324
// The objects that are a part of this API are exposed to third-parties as
@@ -29,15 +30,16 @@ import { FieldPath as InternalFieldPath } from '../model/path';
2930
* single field name (referring to a top-level field in the document), or a list
3031
* of field names (referring to a nested field in the document).
3132
*/
32-
export class FieldPath extends Compat<ExpFieldPath> implements PublicFieldPath {
33+
export class FieldPath implements PublicFieldPath, Compat<ExpFieldPath> {
34+
readonly _delegate: ExpFieldPath;
3335
/**
3436
* Creates a FieldPath from the provided field names. If more than one field
3537
* name is provided, the path will point to a nested field in a document.
3638
*
3739
* @param fieldNames - A list of field names.
3840
*/
3941
constructor(...fieldNames: string[]) {
40-
super(new ExpFieldPath(...fieldNames));
42+
this._delegate = new ExpFieldPath(...fieldNames);
4143
}
4244

4345
static documentId(): FieldPath {
@@ -51,9 +53,7 @@ export class FieldPath extends Compat<ExpFieldPath> implements PublicFieldPath {
5153
}
5254

5355
isEqual(other: PublicFieldPath): boolean {
54-
if (other instanceof Compat) {
55-
other = other._delegate;
56-
}
56+
other = getModularInstance(other);
5757

5858
if (!(other instanceof ExpFieldPath)) {
5959
return false;

packages/firestore/src/api/field_value.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@
1616
*/
1717

1818
import { FieldValue as PublicFieldValue } from '@firebase/firestore-types';
19+
import { Compat } from '@firebase/util';
1920

2021
import {
2122
arrayRemove,
2223
arrayUnion,
2324
deleteField,
2425
FieldValue as FieldValue1,
2526
increment,
26-
serverTimestamp,
27-
Compat
27+
serverTimestamp
2828
} from '../../exp/index';
2929

30-
export class FieldValue
31-
extends Compat<FieldValue1>
32-
implements PublicFieldValue {
30+
export class FieldValue implements PublicFieldValue, Compat<FieldValue1> {
3331
static serverTimestamp(): FieldValue {
3432
const delegate = serverTimestamp();
3533
delegate._methodName = 'FieldValue.serverTimestamp';
@@ -60,6 +58,8 @@ export class FieldValue
6058
return new FieldValue(delegate);
6159
}
6260

61+
constructor(readonly _delegate: FieldValue1) {}
62+
6363
isEqual(other: FieldValue): boolean {
6464
return this._delegate.isEqual(other._delegate);
6565
}

packages/firestore/src/exp/reference_impl.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { getModularInstance } from '@firebase/util';
19+
1820
import {
1921
CompleteFn,
2022
ErrorFn,
@@ -34,7 +36,6 @@ import {
3436
import { newQueryForPath, Query as InternalQuery } from '../core/query';
3537
import { ViewSnapshot } from '../core/view_snapshot';
3638
import { Bytes } from '../lite/bytes';
37-
import { Compat } from '../lite/compat';
3839
import { FieldPath } from '../lite/field_path';
3940
import { validateHasExplicitOrderByForLimitToLast } from '../lite/query';
4041
import {
@@ -338,9 +339,7 @@ export function updateDoc(
338339

339340
// For Compat types, we have to "extract" the underlying types before
340341
// performing validation.
341-
if (fieldOrUpdateData instanceof Compat) {
342-
fieldOrUpdateData = fieldOrUpdateData._delegate;
343-
}
342+
fieldOrUpdateData = getModularInstance(fieldOrUpdateData);
344343

345344
let parsed: ParsedUpdateData;
346345
if (
@@ -622,9 +621,7 @@ export function onSnapshot<T>(
622621
reference: Query<T> | DocumentReference<T>,
623622
...args: unknown[]
624623
): Unsubscribe {
625-
if (reference instanceof Compat) {
626-
reference = reference._delegate;
627-
}
624+
reference = getModularInstance(reference);
628625

629626
let options: SnapshotListenOptions = {
630627
includeMetadataChanges: false

0 commit comments

Comments
 (0)