Skip to content

Commit 64162a2

Browse files
Simplify
1 parent fe88bbc commit 64162a2

File tree

2 files changed

+32
-61
lines changed

2 files changed

+32
-61
lines changed

packages/firestore/exp/src/api/reference.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { DocumentKeyReference } from '../../../src/api/user_data_reader';
2424
import { debugAssert } from '../../../src/util/assert';
2525
import { cast } from '../../../lite/src/api/util';
2626
import { DocumentSnapshot } from './snapshot';
27-
import { Rejecter, Resolver } from '../../../src/util/promise';
2827
import {
2928
getDocViaSnapshotListener,
3029
SnapshotMetadata
@@ -38,23 +37,8 @@ export function getDoc<T>(
3837
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
3938
const firestore = cast<Firestore>(ref.firestore, Firestore);
4039
return firestore._getFirestoreClient().then(async firestoreClient => {
41-
return new Promise(
42-
(resolve: Resolver<firestore.DocumentSnapshot<T>>, reject: Rejecter) => {
43-
getDocViaSnapshotListener(
44-
firestoreClient,
45-
ref,
46-
async snapshot => {
47-
const viewSnapshot = await snapshot;
48-
resolve(
49-
viewSnapshot
50-
? convertToDocSnapshot(firestore, ref, viewSnapshot)
51-
: undefined
52-
);
53-
},
54-
reject
55-
);
56-
}
57-
);
40+
const viewSnapshot = await getDocViaSnapshotListener(firestoreClient, ref);
41+
return convertToDocSnapshot(firestore, ref, viewSnapshot);
5842
});
5943
}
6044

packages/firestore/src/api/database.ts

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,42 +1208,29 @@ export class DocumentReference<T = firestore.DocumentData>
12081208
get(options?: firestore.GetOptions): Promise<firestore.DocumentSnapshot<T>> {
12091209
validateBetweenNumberOfArgs('DocumentReference.get', arguments, 0, 1);
12101210
validateGetOptions('DocumentReference.get', options);
1211-
return new Promise(
1212-
(resolve: Resolver<firestore.DocumentSnapshot<T>>, reject: Rejecter) => {
1213-
if (options && options.source === 'cache') {
1214-
this.firestore
1215-
.ensureClientConfigured()
1216-
.getDocumentFromLocalCache(this._key)
1217-
.then(doc => {
1218-
resolve(
1219-
new DocumentSnapshot(
1220-
this.firestore,
1221-
this._key,
1222-
doc,
1223-
/*fromCache=*/ true,
1224-
doc instanceof Document ? doc.hasLocalMutations : false,
1225-
this._converter
1226-
)
1227-
);
1228-
}, reject);
1229-
} else {
1230-
getDocViaSnapshotListener(
1231-
this._firestoreClient,
1232-
this,
1233-
async snapshot => {
1234-
const viewSnapshot = await snapshot;
1235-
resolve(
1236-
viewSnapshot
1237-
? this._convertToDocSnapshot(viewSnapshot)
1238-
: undefined
1239-
);
1240-
},
1241-
reject,
1242-
options
1243-
);
1244-
}
1245-
}
1246-
);
1211+
1212+
if (options && options.source === 'cache') {
1213+
return this.firestore
1214+
.ensureClientConfigured()
1215+
.getDocumentFromLocalCache(this._key)
1216+
.then(
1217+
doc =>
1218+
new DocumentSnapshot(
1219+
this.firestore,
1220+
this._key,
1221+
doc,
1222+
/*fromCache=*/ true,
1223+
doc instanceof Document ? doc.hasLocalMutations : false,
1224+
this._converter
1225+
)
1226+
);
1227+
} else {
1228+
return getDocViaSnapshotListener(
1229+
this._firestoreClient,
1230+
this,
1231+
options
1232+
).then(snapshot => this._convertToDocSnapshot(snapshot));
1233+
}
12471234
}
12481235

12491236
withConverter<U>(
@@ -1315,10 +1302,9 @@ function addDocSnapshotListener<T>(
13151302
export function getDocViaSnapshotListener<T>(
13161303
firestoreClient: FirestoreClient,
13171304
ref: DocumentKeyReference<T>,
1318-
resolve: Resolver<ViewSnapshot>,
1319-
reject: Rejecter,
13201305
options?: firestore.GetOptions
1321-
): void {
1306+
): Promise<ViewSnapshot> {
1307+
const result = new Deferred<ViewSnapshot>();
13221308
const unlisten = addDocSnapshotListener(
13231309
firestoreClient,
13241310
ref,
@@ -1341,7 +1327,7 @@ export function getDocViaSnapshotListener<T>(
13411327
// the server so we can deliver that even when you're
13421328
// offline 2) Actually reject the Promise in the online case
13431329
// if the document doesn't exist.
1344-
reject(
1330+
result.reject(
13451331
new FirestoreError(
13461332
Code.UNAVAILABLE,
13471333
'Failed to get document because the client is ' + 'offline.'
@@ -1353,7 +1339,7 @@ export function getDocViaSnapshotListener<T>(
13531339
options &&
13541340
options.source === 'server'
13551341
) {
1356-
reject(
1342+
result.reject(
13571343
new FirestoreError(
13581344
Code.UNAVAILABLE,
13591345
'Failed to get document from server. (However, this ' +
@@ -1363,12 +1349,13 @@ export function getDocViaSnapshotListener<T>(
13631349
)
13641350
);
13651351
} else {
1366-
resolve(snap);
1352+
result.resolve(snap);
13671353
}
13681354
},
1369-
error: reject
1355+
error: e => result.reject(e)
13701356
}
13711357
);
1358+
return result.promise;
13721359
}
13731360

13741361
export class SnapshotMetadata implements firestore.SnapshotMetadata {

0 commit comments

Comments
 (0)