@@ -40,7 +40,8 @@ import { Transaction as InternalTransaction } from '../core/transaction';
40
40
import { ChangeType , ViewSnapshot } from '../core/view_snapshot' ;
41
41
import { IndexedDbPersistence } from '../local/indexeddb_persistence' ;
42
42
import { LruParams } from '../local/lru_garbage_collector' ;
43
- import { Document , MaybeDocument , NoDocument } from '../model/document' ;
43
+ import { getDocument } from '../model/collections' ;
44
+ import { Document } from '../model/document' ;
44
45
import { DocumentKey } from '../model/document_key' ;
45
46
import {
46
47
ArrayValue ,
@@ -79,7 +80,7 @@ import { LogLevel } from '../util/log';
79
80
import { AutoId } from '../util/misc' ;
80
81
import * as objUtils from '../util/obj' ;
81
82
import { Rejecter , Resolver } from '../util/promise' ;
82
- import { Deferred } from './. ./util/promise' ;
83
+ import { Deferred } from '../util/promise' ;
83
84
import { FieldPath as ExternalFieldPath } from './field_path' ;
84
85
85
86
import {
@@ -661,37 +662,18 @@ export class Transaction implements firestore.Transaction {
661
662
documentRef ,
662
663
this . _firestore
663
664
) ;
664
- return this . _transaction
665
- . lookup ( [ ref . _key ] )
666
- . then ( ( docs : MaybeDocument [ ] ) => {
667
- if ( ! docs || docs . length !== 1 ) {
668
- return fail ( 'Mismatch in docs returned from document lookup.' ) ;
669
- }
670
- const doc = docs [ 0 ] ;
671
- if ( doc instanceof NoDocument ) {
672
- return new DocumentSnapshot (
673
- this . _firestore ,
674
- ref . _key ,
675
- null ,
676
- /* fromCache= */ false ,
677
- /* hasPendingWrites= */ false
678
- ) ;
679
- } else if ( doc instanceof Document ) {
680
- return new DocumentSnapshot (
681
- this . _firestore ,
682
- ref . _key ,
683
- doc ,
684
- /* fromCache= */ false ,
685
- /* hasPendingWrites= */ false
686
- ) ;
687
- } else {
688
- throw fail (
689
- `BatchGetDocumentsRequest returned unexpected document type: ${
690
- doc . constructor . name
691
- } `
692
- ) ;
693
- }
694
- } ) ;
665
+ return this . _transaction . lookup ( [ ref . _key ] ) . then ( ( docs : Document [ ] ) => {
666
+ if ( ! docs || docs . length !== 1 ) {
667
+ return fail ( 'Mismatch in docs returned from document lookup.' ) ;
668
+ }
669
+ const doc = docs [ 0 ] ;
670
+ return new DocumentSnapshot (
671
+ this . _firestore ,
672
+ doc ,
673
+ /* fromCache= */ false ,
674
+ /* hasPendingWrites= */ false
675
+ ) ;
676
+ } ) ;
695
677
}
696
678
697
679
set (
@@ -1130,12 +1112,11 @@ export class DocumentReference implements firestore.DocumentReference {
1130
1112
snapshot . docs . size <= 1 ,
1131
1113
'Too many documents returned on a document query'
1132
1114
) ;
1133
- const doc = snapshot . docs . get ( this . _key ) ;
1115
+ const doc = getDocument ( this . _key , snapshot . docs ) ;
1134
1116
1135
1117
observer . next (
1136
1118
new DocumentSnapshot (
1137
1119
this . firestore ,
1138
- this . _key ,
1139
1120
doc ,
1140
1121
snapshot . fromCache ,
1141
1122
snapshot . hasPendingWrites
@@ -1170,10 +1151,9 @@ export class DocumentReference implements firestore.DocumentReference {
1170
1151
resolve (
1171
1152
new DocumentSnapshot (
1172
1153
this . firestore ,
1173
- this . _key ,
1174
1154
doc ,
1175
1155
/*fromCache=*/ true ,
1176
- doc instanceof Document ? doc . hasLocalMutations : false
1156
+ doc . hasLocalMutations
1177
1157
)
1178
1158
) ;
1179
1159
} , reject ) ;
@@ -1262,8 +1242,7 @@ export interface SnapshotOptions extends firestore.SnapshotOptions {}
1262
1242
export class DocumentSnapshot implements firestore . DocumentSnapshot {
1263
1243
constructor (
1264
1244
private _firestore : Firestore ,
1265
- private _key : DocumentKey ,
1266
- public _document : Document | null ,
1245
+ public _document : Document ,
1267
1246
private _fromCache : boolean ,
1268
1247
private _hasPendingWrites : boolean
1269
1248
) { }
@@ -1273,7 +1252,7 @@ export class DocumentSnapshot implements firestore.DocumentSnapshot {
1273
1252
) : firestore . DocumentData | undefined {
1274
1253
validateBetweenNumberOfArgs ( 'DocumentSnapshot.data' , arguments , 0 , 1 ) ;
1275
1254
options = validateSnapshotOptions ( 'DocumentSnapshot.data' , options ) ;
1276
- return ! this . _document
1255
+ return ! this . _document . exists
1277
1256
? undefined
1278
1257
: this . convertObject (
1279
1258
this . _document . data ,
@@ -1290,33 +1269,31 @@ export class DocumentSnapshot implements firestore.DocumentSnapshot {
1290
1269
) : unknown {
1291
1270
validateBetweenNumberOfArgs ( 'DocumentSnapshot.get' , arguments , 1 , 2 ) ;
1292
1271
options = validateSnapshotOptions ( 'DocumentSnapshot.get' , options ) ;
1293
- if ( this . _document ) {
1294
- const value = this . _document . data . field (
1295
- fieldPathFromArgument ( 'DocumentSnapshot.get' , fieldPath )
1272
+ const value = this . _document . data . field (
1273
+ fieldPathFromArgument ( 'DocumentSnapshot.get' , fieldPath )
1274
+ ) ;
1275
+ if ( value !== undefined ) {
1276
+ return this . convertValue (
1277
+ value ,
1278
+ FieldValueOptions . fromSnapshotOptions (
1279
+ options ,
1280
+ this . _firestore . _areTimestampsInSnapshotsEnabled ( )
1281
+ )
1296
1282
) ;
1297
- if ( value !== undefined ) {
1298
- return this . convertValue (
1299
- value ,
1300
- FieldValueOptions . fromSnapshotOptions (
1301
- options ,
1302
- this . _firestore . _areTimestampsInSnapshotsEnabled ( )
1303
- )
1304
- ) ;
1305
- }
1306
1283
}
1307
1284
return undefined ;
1308
1285
}
1309
1286
1310
1287
get id ( ) : string {
1311
- return this . _key . path . lastSegment ( ) ;
1288
+ return this . _document . key . path . lastSegment ( ) ;
1312
1289
}
1313
1290
1314
1291
get ref ( ) : firestore . DocumentReference {
1315
- return new DocumentReference ( this . _key , this . _firestore ) ;
1292
+ return new DocumentReference ( this . _document . key , this . _firestore ) ;
1316
1293
}
1317
1294
1318
1295
get exists ( ) : boolean {
1319
- return this . _document !== null ;
1296
+ return this . _document . exists ;
1320
1297
}
1321
1298
1322
1299
get metadata ( ) : firestore . SnapshotMetadata {
@@ -1330,10 +1307,7 @@ export class DocumentSnapshot implements firestore.DocumentSnapshot {
1330
1307
return (
1331
1308
this . _firestore === other . _firestore &&
1332
1309
this . _fromCache === other . _fromCache &&
1333
- this . _key . isEqual ( other . _key ) &&
1334
- ( this . _document === null
1335
- ? other . _document === null
1336
- : this . _document . isEqual ( other . _document ) )
1310
+ this . _document . isEqual ( other . _document )
1337
1311
) ;
1338
1312
}
1339
1313
@@ -1359,7 +1333,7 @@ export class DocumentSnapshot implements firestore.DocumentSnapshot {
1359
1333
if ( ! value . databaseId . isEqual ( database ) ) {
1360
1334
// TODO(b/64130202): Somehow support foreign references.
1361
1335
log . error (
1362
- `Document ${ this . _key . path } contains a document ` +
1336
+ `Document ${ this . _document . key . path } contains a document ` +
1363
1337
`reference within a different database (` +
1364
1338
`${ value . databaseId . projectId } /${
1365
1339
value . databaseId . database
@@ -1389,12 +1363,11 @@ export class QueryDocumentSnapshot extends DocumentSnapshot
1389
1363
implements firestore . QueryDocumentSnapshot {
1390
1364
constructor (
1391
1365
firestore : Firestore ,
1392
- key : DocumentKey ,
1393
1366
document : Document ,
1394
1367
fromCache : boolean ,
1395
1368
hasPendingWrites : boolean
1396
1369
) {
1397
- super ( firestore , key , document , fromCache , hasPendingWrites ) ;
1370
+ super ( firestore , document , fromCache , hasPendingWrites ) ;
1398
1371
}
1399
1372
1400
1373
data ( options ?: SnapshotOptions ) : firestore . DocumentData {
@@ -2136,7 +2109,6 @@ export class QuerySnapshot implements firestore.QuerySnapshot {
2136
2109
private convertToDocumentImpl ( doc : Document ) : QueryDocumentSnapshot {
2137
2110
return new QueryDocumentSnapshot (
2138
2111
this . _firestore ,
2139
- doc . key ,
2140
2112
doc ,
2141
2113
this . metadata . fromCache ,
2142
2114
this . _snapshot . mutatedKeys . has ( doc . key )
@@ -2348,7 +2320,6 @@ export function changesFromSnapshot(
2348
2320
return snapshot . docChanges . map ( change => {
2349
2321
const doc = new QueryDocumentSnapshot (
2350
2322
firestore ,
2351
- change . doc . key ,
2352
2323
change . doc ,
2353
2324
snapshot . fromCache ,
2354
2325
snapshot . mutatedKeys . has ( change . doc . key )
@@ -2380,7 +2351,6 @@ export function changesFromSnapshot(
2380
2351
. map ( change => {
2381
2352
const doc = new QueryDocumentSnapshot (
2382
2353
firestore ,
2383
- change . doc . key ,
2384
2354
change . doc ,
2385
2355
snapshot . fromCache ,
2386
2356
snapshot . mutatedKeys . has ( change . doc . key )
0 commit comments