Skip to content

Commit 1b3d633

Browse files
committed
Remove bundles from public interfaces take 2.
1 parent d4ab17f commit 1b3d633

File tree

8 files changed

+80
-129
lines changed

8 files changed

+80
-129
lines changed

.changeset/lemon-steaks-draw.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"@firebase/firestore": feature
2+
"@firebase/firestore": internal
33
---
44

5-
Add support for loading Firestore Bundle Files.
5+
Merge bundle loading implementation without exposing public API

packages/firebase/index.d.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8285,43 +8285,6 @@ declare namespace firebase.firestore {
82858285
INTERNAL: { delete: () => Promise<void> };
82868286
}
82878287

8288-
export function loadBundle(
8289-
db: Firestore,
8290-
bundleData: ArrayBuffer | ReadableStream<ArrayBuffer> | string
8291-
): LoadBundleTask;
8292-
8293-
export function namedQuery(
8294-
db: Firestore,
8295-
name: string
8296-
): Promise<Query<DocumentData> | null>;
8297-
8298-
export interface LoadBundleTask {
8299-
onProgress(
8300-
next?: (progress: LoadBundleTaskProgress) => any,
8301-
error?: (error: Error) => any,
8302-
complete?: () => void
8303-
): void;
8304-
8305-
then<T, R>(
8306-
onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike<T>,
8307-
onRejected?: (a: Error) => R | PromiseLike<R>
8308-
): Promise<T | R>;
8309-
8310-
catch<R>(
8311-
onRejected: (a: Error) => R | PromiseLike<R>
8312-
): Promise<R | LoadBundleTaskProgress>;
8313-
}
8314-
8315-
export interface LoadBundleTaskProgress {
8316-
documentsLoaded: number;
8317-
totalDocuments: number;
8318-
bytesLoaded: number;
8319-
totalBytes: number;
8320-
taskState: TaskState;
8321-
}
8322-
8323-
export type TaskState = 'Error' | 'Running' | 'Success';
8324-
83258288
/**
83268289
* An immutable object representing a geo point in Firestore. The geo point
83278290
* is represented as latitude/longitude pair.

packages/firestore-types/index.d.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,33 +99,6 @@ export class FirebaseFirestore {
9999
INTERNAL: { delete: () => Promise<void> };
100100
}
101101

102-
export interface LoadBundleTask {
103-
onProgress(
104-
next?: (progress: LoadBundleTaskProgress) => any,
105-
error?: (error: Error) => any,
106-
complete?: () => void
107-
): void;
108-
109-
then<T, R>(
110-
onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike<T>,
111-
onRejected?: (a: Error) => R | PromiseLike<R>
112-
): Promise<T | R>;
113-
114-
catch<R>(
115-
onRejected: (a: Error) => R | PromiseLike<R>
116-
): Promise<R | LoadBundleTaskProgress>;
117-
}
118-
119-
export interface LoadBundleTaskProgress {
120-
documentsLoaded: number;
121-
totalDocuments: number;
122-
bytesLoaded: number;
123-
totalBytes: number;
124-
taskState: TaskState;
125-
}
126-
127-
export type TaskState = 'Error' | 'Running' | 'Success';
128-
129102
export class GeoPoint {
130103
constructor(latitude: number, longitude: number);
131104

packages/firestore/exp-types/index.d.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -516,43 +516,6 @@ export function snapshotEqual<T>(
516516
right: DocumentSnapshot<T> | QuerySnapshot<T>
517517
): boolean;
518518

519-
export interface LoadBundleTask {
520-
onProgress(
521-
next?: (progress: LoadBundleTaskProgress) => any,
522-
error?: (error: Error) => any,
523-
complete?: () => void
524-
): void;
525-
526-
then<T, R>(
527-
onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike<T>,
528-
onRejected?: (a: Error) => R | PromiseLike<R>
529-
): Promise<T | R>;
530-
531-
catch<R>(
532-
onRejected: (a: Error) => R | PromiseLike<R>
533-
): Promise<R | LoadBundleTaskProgress>;
534-
}
535-
536-
export interface LoadBundleTaskProgress {
537-
documentsLoaded: number;
538-
totalDocuments: number;
539-
bytesLoaded: number;
540-
totalBytes: number;
541-
taskState: TaskState;
542-
}
543-
544-
export type TaskState = 'Error' | 'Running' | 'Success';
545-
546-
export function loadBundle(
547-
firestore: FirebaseFirestore,
548-
bundleData: ArrayBuffer | ReadableStream<Uint8Array> | string
549-
): LoadBundleTask;
550-
551-
export function namedQuery(
552-
firestore: FirebaseFirestore,
553-
name: string
554-
): Promise<Query<DocumentData> | null>;
555-
556519
export type FirestoreErrorCode =
557520
| 'cancelled'
558521
| 'unknown'

packages/firestore/src/api/bundle.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,46 @@
1515
* limitations under the License.
1616
*/
1717

18-
import * as firestore from '@firebase/firestore-types';
1918
import { Deferred } from '../util/promise';
2019
import { PartialObserver } from './observer';
2120
import { debugAssert } from '../util/assert';
2221
import { FirestoreError } from '../util/error';
2322

23+
export interface ApiLoadBundleTask {
24+
onProgress(
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
next?: (progress: ApiLoadBundleTaskProgress) => any,
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28+
error?: (error: Error) => any,
29+
complete?: () => void
30+
): void;
31+
32+
then<T, R>(
33+
onFulfilled?: (a: ApiLoadBundleTaskProgress) => T | PromiseLike<T>,
34+
onRejected?: (a: Error) => R | PromiseLike<R>
35+
): Promise<T | R>;
36+
37+
catch<R>(
38+
onRejected: (a: Error) => R | PromiseLike<R>
39+
): Promise<R | ApiLoadBundleTaskProgress>;
40+
}
41+
42+
export interface ApiLoadBundleTaskProgress {
43+
documentsLoaded: number;
44+
totalDocuments: number;
45+
bytesLoaded: number;
46+
totalBytes: number;
47+
taskState: TaskState;
48+
}
49+
50+
export type TaskState = 'Error' | 'Running' | 'Success';
51+
2452
export class LoadBundleTask
25-
implements
26-
firestore.LoadBundleTask,
27-
PromiseLike<firestore.LoadBundleTaskProgress> {
28-
private _progressObserver: PartialObserver<
29-
firestore.LoadBundleTaskProgress
30-
> = {};
31-
private _taskCompletionResolver = new Deferred<
32-
firestore.LoadBundleTaskProgress
33-
>();
34-
35-
private _lastProgress: firestore.LoadBundleTaskProgress = {
53+
implements ApiLoadBundleTask, PromiseLike<ApiLoadBundleTaskProgress> {
54+
private _progressObserver: PartialObserver<ApiLoadBundleTaskProgress> = {};
55+
private _taskCompletionResolver = new Deferred<ApiLoadBundleTaskProgress>();
56+
57+
private _lastProgress: ApiLoadBundleTaskProgress = {
3658
taskState: 'Running',
3759
totalBytes: 0,
3860
totalDocuments: 0,
@@ -41,7 +63,7 @@ export class LoadBundleTask
4163
};
4264

4365
onProgress(
44-
next?: (progress: firestore.LoadBundleTaskProgress) => unknown,
66+
next?: (progress: ApiLoadBundleTaskProgress) => unknown,
4567
error?: (err: Error) => unknown,
4668
complete?: () => void
4769
): void {
@@ -54,12 +76,12 @@ export class LoadBundleTask
5476

5577
catch<R>(
5678
onRejected: (a: Error) => R | PromiseLike<R>
57-
): Promise<R | firestore.LoadBundleTaskProgress> {
79+
): Promise<R | ApiLoadBundleTaskProgress> {
5880
return this._taskCompletionResolver.promise.catch(onRejected);
5981
}
6082

6183
then<T, R>(
62-
onFulfilled?: (a: firestore.LoadBundleTaskProgress) => T | PromiseLike<T>,
84+
onFulfilled?: (a: ApiLoadBundleTaskProgress) => T | PromiseLike<T>,
6385
onRejected?: (a: Error) => R | PromiseLike<R>
6486
): Promise<T | R> {
6587
return this._taskCompletionResolver.promise.then(onFulfilled, onRejected);
@@ -69,7 +91,7 @@ export class LoadBundleTask
6991
* Notifies all observers that bundle loading has completed, with a provided
7092
* `LoadBundleTaskProgress` object.
7193
*/
72-
_completeWith(progress: firestore.LoadBundleTaskProgress): void {
94+
_completeWith(progress: ApiLoadBundleTaskProgress): void {
7395
debugAssert(
7496
progress.taskState === 'Success',
7597
'Task is not completed with Success.'
@@ -104,7 +126,7 @@ export class LoadBundleTask
104126
* Notifies a progress update of loading a bundle.
105127
* @param progress The new progress.
106128
*/
107-
_updateProgress(progress: firestore.LoadBundleTaskProgress): void {
129+
_updateProgress(progress: ApiLoadBundleTaskProgress): void {
108130
debugAssert(
109131
this._lastProgress.taskState === 'Running',
110132
'Cannot update progress on a completed or failed task'

packages/firestore/src/api/database.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import {
2828
firestoreClientGetDocumentsFromLocalCache,
2929
firestoreClientGetDocumentsViaSnapshotListener,
3030
firestoreClientGetDocumentViaSnapshotListener,
31+
firestoreClientGetNamedQuery,
3132
firestoreClientListen,
33+
firestoreClientLoadBundle,
3234
firestoreClientTransaction,
3335
firestoreClientWrite
3436
} from '../core/firestore_client';
@@ -141,6 +143,7 @@ import {
141143
import { newUserDataReader } from '../../lite/src/api/reference';
142144
import { makeDatabaseInfo } from '../../lite/src/api/database';
143145
import { DEFAULT_HOST } from '../../lite/src/api/components';
146+
import { ApiLoadBundleTask, LoadBundleTask } from './bundle';
144147

145148
/**
146149
* Constant used to indicate the LRU garbage collection should be disabled.
@@ -424,6 +427,36 @@ export function setLogLevel(level: PublicLogLevel): void {
424427
setClientLogLevel(level);
425428
}
426429

430+
export function loadBundle(
431+
db: Firestore,
432+
bundleData: ArrayBuffer | ReadableStream<Uint8Array> | string
433+
): ApiLoadBundleTask {
434+
const resultTask = new LoadBundleTask();
435+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
436+
firestoreClientLoadBundle(
437+
ensureFirestoreConfigured(db._delegate),
438+
bundleData,
439+
resultTask
440+
);
441+
return resultTask;
442+
}
443+
444+
export function namedQuery(
445+
db: Firestore,
446+
name: string
447+
): Promise<PublicQuery | null> {
448+
return firestoreClientGetNamedQuery(
449+
ensureFirestoreConfigured(db._delegate),
450+
name
451+
).then(namedQuery => {
452+
if (!namedQuery) {
453+
return null;
454+
}
455+
456+
return new Query(namedQuery.query, db, null);
457+
});
458+
}
459+
427460
/**
428461
* A reference to a transaction.
429462
*/

packages/firestore/src/core/bundle.ts

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

18-
import * as firestore from '@firebase/firestore-types';
1918
import { Query } from './query';
2019
import { SnapshotVersion } from './snapshot_version';
2120
import {
@@ -41,6 +40,7 @@ import {
4140
DocumentKeySet,
4241
MaybeDocumentMap
4342
} from '../model/collections';
43+
import { ApiLoadBundleTaskProgress } from '../api/bundle';
4444

4545
/**
4646
* Represents a Firestore bundle saved by the SDK in its local storage.
@@ -118,7 +118,7 @@ export class BundleConverter {
118118
*/
119119
export function bundleInitialProgress(
120120
metadata: BundleMetadata
121-
): firestore.LoadBundleTaskProgress {
121+
): ApiLoadBundleTaskProgress {
122122
return {
123123
taskState: 'Running',
124124
documentsLoaded: 0,
@@ -134,7 +134,7 @@ export function bundleInitialProgress(
134134
*/
135135
export function bundleSuccessProgress(
136136
metadata: BundleMetadata
137-
): firestore.LoadBundleTaskProgress {
137+
): ApiLoadBundleTaskProgress {
138138
return {
139139
taskState: 'Success',
140140
documentsLoaded: metadata.totalDocuments!,
@@ -146,7 +146,7 @@ export function bundleSuccessProgress(
146146

147147
export class BundleLoadResult {
148148
constructor(
149-
readonly progress: firestore.LoadBundleTaskProgress,
149+
readonly progress: ApiLoadBundleTaskProgress,
150150
readonly changedDocs: MaybeDocumentMap
151151
) {}
152152
}
@@ -157,7 +157,7 @@ export class BundleLoadResult {
157157
*/
158158
export class BundleLoader {
159159
/** The current progress of loading */
160-
private progress: firestore.LoadBundleTaskProgress;
160+
private progress: ApiLoadBundleTaskProgress;
161161
/** Batched queries to be saved into storage */
162162
private queries: bundleProto.NamedQuery[] = [];
163163
/** Batched documents to be saved into storage */
@@ -179,7 +179,7 @@ export class BundleLoader {
179179
*/
180180
addSizedElement(
181181
element: SizedBundleElement
182-
): firestore.LoadBundleTaskProgress | null {
182+
): ApiLoadBundleTaskProgress | null {
183183
debugAssert(!element.isBundleMetadata(), 'Unexpected bundle metadata.');
184184

185185
this.progress.bytesLoaded += element.byteLength;

packages/firestore/src/core/firestore_client.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,6 @@ export async function firestoreClientLoadBundle(
662662
data: ReadableStream<Uint8Array> | ArrayBuffer | string,
663663
resultTask: LoadBundleTask
664664
): Promise<void> {
665-
client.verifyNotTerminated();
666-
667665
const reader = createBundleReader(
668666
data,
669667
newSerializer((await client.getConfiguration()).databaseInfo.databaseId)
@@ -677,7 +675,6 @@ export function firestoreClientGetNamedQuery(
677675
client: FirestoreClient,
678676
queryName: string
679677
): Promise<NamedQuery | undefined> {
680-
client.verifyNotTerminated();
681678
return client.asyncQueue.enqueue(async () =>
682679
getNamedQuery(await getLocalStore(client), queryName)
683680
);

0 commit comments

Comments
 (0)