Skip to content

Commit 8698b11

Browse files
committed
Refactoring to fix bundling. Remove extraneous exports. Add missing exports. Fix some circular deps. Remove circular dep assert. Update missing externs.
1 parent d98b0d1 commit 8698b11

29 files changed

+502
-375
lines changed

packages/firestore/lite/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import { registerFirestore } from './register';
2828
registerFirestore();
2929

30+
// TODO this should not be part of lite
31+
export { SnapshotMetadata } from '../src/api/snapshot';
32+
3033
export {
3134
aggregateQuerySnapshotEqual,
3235
getCount,

packages/firestore/lite/pipelines/pipelines.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export type {
4545
QueryDocumentSnapshot,
4646
Primitive,
4747
FieldValue,
48-
Bytes
48+
Bytes,
49+
// TODO this should not be part of lite
50+
SnapshotMetadata
4951
} from '../index';
5052

5153
export { PipelineSource } from '../../src/lite-api/pipeline-source';

packages/firestore/pipelines/pipelines.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export type {
4545
Primitive,
4646
FieldValue,
4747
SnapshotMetadata,
48-
Bytes
48+
Bytes,
49+
SnapshotListenOptions,
50+
Unsubscribe
4951
} from '../src/api';
5052

5153
export * from '../src/api_pipelines';

packages/firestore/src/api/pipeline_impl.ts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { Pipeline } from '../api/pipeline';
1919
import {
2020
firestoreClientExecutePipeline,
21+
firestoreClientGetDocumentsViaSnapshotListener,
2122
firestoreClientListen
2223
} from '../core/firestore_client';
2324
import { toCorePipeline } from '../core/pipeline-util';
@@ -78,35 +79,52 @@ declare module './database' {
7879
* @param pipeline The pipeline to execute.
7980
* @return A Promise representing the asynchronous pipeline execution.
8081
*/
81-
export function execute(pipeline: LitePipeline): Promise<PipelineSnapshot> {
82+
export function execute(pipeline: LitePipeline): Promise<PipelineSnapshot>;
83+
export function execute(
84+
pipeline: RealtimePipeline
85+
): Promise<RealtimePipelineSnapshot>;
86+
export function execute(
87+
pipeline: LitePipeline | RealtimePipeline
88+
): Promise<PipelineSnapshot | RealtimePipelineSnapshot> {
8289
const firestore = cast(pipeline._db, Firestore);
8390
const client = ensureFirestoreConfigured(firestore);
84-
return firestoreClientExecutePipeline(client, pipeline).then(result => {
85-
// Get the execution time from the first result.
86-
// firestoreClientExecutePipeline returns at least one PipelineStreamElement
87-
// even if the returned document set is empty.
88-
const executionTime =
89-
result.length > 0 ? result[0].executionTime?.toTimestamp() : undefined;
9091

91-
const docs = result
92-
// Currently ignore any response from ExecutePipeline that does
93-
// not contain any document data in the `fields` property.
94-
.filter(element => !!element.fields)
95-
.map(
96-
element =>
97-
new PipelineResult(
98-
pipeline._userDataWriter,
99-
element.key?.path
100-
? new DocumentReference(firestore, null, element.key)
101-
: undefined,
102-
element.fields,
103-
element.createTime?.toTimestamp(),
104-
element.updateTime?.toTimestamp()
105-
)
106-
);
92+
if (pipeline instanceof RealtimePipeline) {
93+
return firestoreClientGetDocumentsViaSnapshotListener(
94+
client,
95+
pipeline
96+
).then(
97+
snapshot =>
98+
new RealtimePipelineSnapshot(pipeline as RealtimePipeline, snapshot)
99+
);
100+
} else {
101+
return firestoreClientExecutePipeline(client, pipeline).then(result => {
102+
// Get the execution time from the first result.
103+
// firestoreClientExecutePipeline returns at least one PipelineStreamElement
104+
// even if the returned document set is empty.
105+
const executionTime =
106+
result.length > 0 ? result[0].executionTime?.toTimestamp() : undefined;
107107

108-
return new PipelineSnapshot(pipeline, docs, executionTime);
109-
});
108+
const docs = result
109+
// Currently ignore any response from ExecutePipeline that does
110+
// not contain any document data in the `fields` property.
111+
.filter(element => !!element.fields)
112+
.map(
113+
element =>
114+
new PipelineResult(
115+
pipeline._userDataWriter,
116+
element.key?.path
117+
? new DocumentReference(firestore, null, element.key)
118+
: undefined,
119+
element.fields,
120+
element.createTime?.toTimestamp(),
121+
element.updateTime?.toTimestamp()
122+
)
123+
);
124+
125+
return new PipelineSnapshot(pipeline, docs, executionTime);
126+
});
127+
}
110128
}
111129

112130
// Augment the Firestore class with the pipeline() factory method

packages/firestore/src/api/realtime_pipeline.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export class RealtimePipeline {
3030
* @private
3131
*/
3232
public _db: Firestore,
33+
/**
34+
* @internal
35+
* @private
36+
*/
3337
readonly userDataReader: UserDataReader,
3438
/**
3539
* @internal
@@ -46,6 +50,7 @@ export class RealtimePipeline {
4650
* @param expressionMap
4751
* @return the expressionMap argument.
4852
* @private
53+
* @internal
4954
*/
5055
protected readUserData<
5156
T extends
@@ -73,9 +78,8 @@ export class RealtimePipeline {
7378
* @param userDataWriter
7479
* @param stages
7580
* @param converter
76-
* @protected
7781
*/
78-
protected newPipeline(
82+
newPipeline(
7983
db: Firestore,
8084
userDataReader: UserDataReader,
8185
userDataWriter: AbstractUserDataWriter,

packages/firestore/src/api/reference_impl.ts

Lines changed: 123 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -200,43 +200,28 @@ export function getDocFromServer<
200200
export function getDocs<AppModelType, DbModelType extends DocumentData>(
201201
query: Query<AppModelType, DbModelType>
202202
): Promise<QuerySnapshot<AppModelType, DbModelType>>;
203-
export function getDocs(
204-
pipeline: RealtimePipeline
205-
): Promise<RealtimePipelineSnapshot>;
206-
export function getDocs<AppModelType, DbModelType extends DocumentData>(
207-
query: Query<AppModelType, DbModelType> | RealtimePipeline
208-
): Promise<
209-
QuerySnapshot<AppModelType, DbModelType> | RealtimePipelineSnapshot
210-
> {
211-
if (query instanceof RealtimePipeline) {
212-
const firestore = cast(query._db, Firestore);
213-
const client = ensureFirestoreConfigured(firestore);
214-
const userDataWriter = new ExpUserDataWriter(firestore);
215203

216-
return firestoreClientGetDocumentsViaSnapshotListener(client, query).then(
217-
snapshot =>
218-
new RealtimePipelineSnapshot(query as RealtimePipeline, snapshot)
219-
);
220-
} else {
221-
query = cast<Query<AppModelType, DbModelType>>(query, Query);
222-
const firestore = cast(query.firestore, Firestore);
223-
const client = ensureFirestoreConfigured(firestore);
224-
const userDataWriter = new ExpUserDataWriter(firestore);
204+
export function getDocs<AppModelType, DbModelType extends DocumentData>(
205+
query: Query<AppModelType, DbModelType>
206+
): Promise<QuerySnapshot<AppModelType, DbModelType>> {
207+
query = cast<Query<AppModelType, DbModelType>>(query, Query);
208+
const firestore = cast(query.firestore, Firestore);
209+
const client = ensureFirestoreConfigured(firestore);
210+
const userDataWriter = new ExpUserDataWriter(firestore);
225211

226-
validateHasExplicitOrderByForLimitToLast(query._query);
227-
return firestoreClientGetDocumentsViaSnapshotListener(
228-
client,
229-
query._query
230-
).then(
231-
snapshot =>
232-
new QuerySnapshot<AppModelType, DbModelType>(
233-
firestore,
234-
userDataWriter,
235-
query as Query<AppModelType, DbModelType>,
236-
snapshot
237-
)
238-
);
239-
}
212+
validateHasExplicitOrderByForLimitToLast(query._query);
213+
return firestoreClientGetDocumentsViaSnapshotListener(
214+
client,
215+
query._query
216+
).then(
217+
snapshot =>
218+
new QuerySnapshot<AppModelType, DbModelType>(
219+
firestore,
220+
userDataWriter,
221+
query as Query<AppModelType, DbModelType>,
222+
snapshot
223+
)
224+
);
240225
}
241226

242227
/**
@@ -683,41 +668,11 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
683668
onError?: (error: FirestoreError) => void,
684669
onCompletion?: () => void
685670
): Unsubscribe;
686-
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
687-
query: RealtimePipeline,
688-
observer: {
689-
next?: (snapshot: RealtimePipelineSnapshot) => void;
690-
error?: (error: FirestoreError) => void;
691-
complete?: () => void;
692-
}
693-
): Unsubscribe;
694-
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
695-
query: RealtimePipeline,
696-
options: SnapshotListenOptions,
697-
observer: {
698-
next?: (snapshot: RealtimePipelineSnapshot) => void;
699-
error?: (error: FirestoreError) => void;
700-
complete?: () => void;
701-
}
702-
): Unsubscribe;
703-
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
704-
query: RealtimePipeline,
705-
onNext: (snapshot: RealtimePipelineSnapshot) => void,
706-
onError?: (error: FirestoreError) => void,
707-
onCompletion?: () => void
708-
): Unsubscribe;
709-
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
710-
query: RealtimePipeline,
711-
options: SnapshotListenOptions,
712-
onNext: (snapshot: RealtimePipelineSnapshot) => void,
713-
onError?: (error: FirestoreError) => void,
714-
onCompletion?: () => void
715-
): Unsubscribe;
671+
716672
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
717673
reference:
718674
| Query<AppModelType, DbModelType>
719-
| DocumentReference<AppModelType, DbModelType>
720-
| RealtimePipeline,
675+
| DocumentReference<AppModelType, DbModelType>,
721676
...args: unknown[]
722677
): Unsubscribe {
723678
reference = getModularInstance(reference);
@@ -771,7 +726,7 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
771726
error: args[currArg + 1] as ErrorFn,
772727
complete: args[currArg + 2] as CompleteFn
773728
};
774-
} else if (reference instanceof Query) {
729+
} else {
775730
const query = cast<Query<AppModelType, DbModelType>>(reference, Query);
776731
firestore = cast(query.firestore, Firestore);
777732
internalQuery = query._query;
@@ -790,24 +745,6 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
790745
};
791746

792747
validateHasExplicitOrderByForLimitToLast(reference._query);
793-
} else {
794-
// RealtimePipeline
795-
firestore = cast(reference._db, Firestore);
796-
internalQuery = toCorePipeline(reference);
797-
observer = {
798-
next: snapshot => {
799-
if (args[currArg]) {
800-
(args[currArg] as NextFn<RealtimePipelineSnapshot>)(
801-
new RealtimePipelineSnapshot(
802-
reference as RealtimePipeline,
803-
snapshot
804-
)
805-
);
806-
}
807-
},
808-
error: args[currArg + 1] as ErrorFn,
809-
complete: args[currArg + 2] as CompleteFn
810-
};
811748
}
812749

813750
const client = ensureFirestoreConfigured(firestore);
@@ -819,6 +756,106 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
819756
);
820757
}
821758

759+
export function onPipelineSnapshot<
760+
AppModelType,
761+
DbModelType extends DocumentData
762+
>(
763+
query: RealtimePipeline,
764+
observer: {
765+
next?: (snapshot: RealtimePipelineSnapshot) => void;
766+
error?: (error: FirestoreError) => void;
767+
complete?: () => void;
768+
}
769+
): Unsubscribe;
770+
export function onPipelineSnapshot<
771+
AppModelType,
772+
DbModelType extends DocumentData
773+
>(
774+
query: RealtimePipeline,
775+
options: SnapshotListenOptions,
776+
observer: {
777+
next?: (snapshot: RealtimePipelineSnapshot) => void;
778+
error?: (error: FirestoreError) => void;
779+
complete?: () => void;
780+
}
781+
): Unsubscribe;
782+
export function onPipelineSnapshot<
783+
AppModelType,
784+
DbModelType extends DocumentData
785+
>(
786+
query: RealtimePipeline,
787+
onNext: (snapshot: RealtimePipelineSnapshot) => void,
788+
onError?: (error: FirestoreError) => void,
789+
onCompletion?: () => void
790+
): Unsubscribe;
791+
export function onPipelineSnapshot<
792+
AppModelType,
793+
DbModelType extends DocumentData
794+
>(
795+
query: RealtimePipeline,
796+
options: SnapshotListenOptions,
797+
onNext: (snapshot: RealtimePipelineSnapshot) => void,
798+
onError?: (error: FirestoreError) => void,
799+
onCompletion?: () => void
800+
): Unsubscribe;
801+
export function onPipelineSnapshot<
802+
AppModelType,
803+
DbModelType extends DocumentData
804+
>(reference: RealtimePipeline, ...args: unknown[]): Unsubscribe {
805+
reference = getModularInstance(reference);
806+
807+
let options: SnapshotListenOptions = {
808+
includeMetadataChanges: false,
809+
source: 'default'
810+
};
811+
let currArg = 0;
812+
if (typeof args[currArg] === 'object' && !isPartialObserver(args[currArg])) {
813+
options = args[currArg] as SnapshotListenOptions;
814+
currArg++;
815+
}
816+
817+
const internalOptions = {
818+
includeMetadataChanges: options.includeMetadataChanges,
819+
source: options.source as ListenerDataSource
820+
};
821+
822+
if (isPartialObserver(args[currArg])) {
823+
const userObserver = args[currArg] as PartialObserver<
824+
QuerySnapshot<AppModelType, DbModelType>
825+
>;
826+
args[currArg] = userObserver.next?.bind(userObserver);
827+
args[currArg + 1] = userObserver.error?.bind(userObserver);
828+
args[currArg + 2] = userObserver.complete?.bind(userObserver);
829+
}
830+
831+
let observer: PartialObserver<ViewSnapshot>;
832+
let firestore: Firestore;
833+
let internalQuery: QueryOrPipeline;
834+
835+
// RealtimePipeline
836+
firestore = cast(reference._db, Firestore);
837+
internalQuery = toCorePipeline(reference);
838+
observer = {
839+
next: snapshot => {
840+
if (args[currArg]) {
841+
(args[currArg] as NextFn<RealtimePipelineSnapshot>)(
842+
new RealtimePipelineSnapshot(reference as RealtimePipeline, snapshot)
843+
);
844+
}
845+
},
846+
error: args[currArg + 1] as ErrorFn,
847+
complete: args[currArg + 2] as CompleteFn
848+
};
849+
850+
const client = ensureFirestoreConfigured(firestore);
851+
return firestoreClientListen(
852+
client,
853+
internalQuery,
854+
internalOptions,
855+
observer
856+
);
857+
}
858+
822859
// TODO(firestorexp): Make sure these overloads are tested via the Firestore
823860
// integration tests
824861

0 commit comments

Comments
 (0)