Skip to content

Commit f7ff495

Browse files
committed
Add comments and move types to d.ts
1 parent 979ffd9 commit f7ff495

File tree

8 files changed

+113
-77
lines changed

8 files changed

+113
-77
lines changed

packages/firestore-types/index.d.ts

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

1818
import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types';
19-
import { LoadBundleTask } from '../firestore/src/core/bundle';
2019

2120
export type DocumentData = { [field: string]: any };
2221

@@ -87,12 +86,37 @@ export class FirebaseFirestore {
8786
terminate(): Promise<void>;
8887

8988
loadBundle(
90-
bundleData: ArrayBuffer | ReadableStream /*| string */
89+
bundleData: ArrayBuffer | ReadableStream | string
9190
): LoadBundleTask;
9291

9392
INTERNAL: { delete: () => Promise<void> };
9493
}
9594

95+
export interface LoadBundleTask {
96+
onProgress(
97+
next?: (progress: LoadBundleTaskProgress) => any,
98+
error?: (error: Error) => any,
99+
complete?: (progress?: LoadBundleTaskProgress) => any
100+
): Promise<any>;
101+
102+
then(
103+
onFulfilled?: (a: LoadBundleTaskProgress) => any,
104+
onRejected?: (a: Error) => any
105+
): Promise<any>;
106+
107+
catch(onRejected: (a: Error) => any): Promise<any>;
108+
}
109+
110+
export interface LoadBundleTaskProgress {
111+
documentsLoaded: number;
112+
totalDocuments: number;
113+
bytesLoaded: number;
114+
totalBytes: number;
115+
taskState: TaskState;
116+
}
117+
118+
export type TaskState = 'Error' | 'Running' | 'Success';
119+
96120
export class GeoPoint {
97121
constructor(latitude: number, longitude: number);
98122

packages/firestore/src/api/database.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ import { fieldPathFromArgument, UserDataReader } from './user_data_reader';
9292
import { UserDataWriter } from './user_data_writer';
9393
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
9494
import { Provider } from '@firebase/component';
95-
import { LoadBundleTask } from '../core/bundle';
9695

9796
// settings() defaults:
9897
const DEFAULT_HOST = 'firestore.googleapis.com';
@@ -479,7 +478,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
479478

480479
loadBundle(
481480
bundleData: ArrayBuffer | ReadableStream /*| string */
482-
): LoadBundleTask {
481+
): firestore.LoadBundleTask {
483482
this.ensureClientConfigured();
484483
return this._firestoreClient!.loadBundle(bundleData);
485484
}

packages/firestore/src/core/bundle.ts

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

18+
import * as firestore from '@firebase/firestore-types';
19+
1820
import { Query } from './query';
1921
import { SnapshotVersion } from './snapshot_version';
2022
import { JsonProtoSerializer } from '../remote/serializer';
@@ -80,19 +82,14 @@ export class BundleConverter {
8082
}
8183
}
8284

83-
export interface LoadBundleTaskProgress {
84-
documentsLoaded: number;
85-
totalDocuments: number;
86-
bytesLoaded: number;
87-
totalBytes: number;
88-
taskState: TaskState;
89-
}
90-
export type TaskState = 'Error' | 'Running' | 'Success';
91-
85+
/**
86+
* Returns a `LoadBundleTaskProgress` representing the first progress of
87+
* loading a bundle.
88+
*/
9289
export function initialProgress(
93-
state: TaskState,
90+
state: firestore.TaskState,
9491
metadata: BundleMetadata
95-
): LoadBundleTaskProgress {
92+
): firestore.LoadBundleTaskProgress {
9693
return {
9794
taskState: state,
9895
documentsLoaded: state === 'Success' ? metadata.totalDocuments! : 0,
@@ -103,32 +100,21 @@ export function initialProgress(
103100
}
104101

105102
/* eslint-disable @typescript-eslint/no-explicit-any */
106-
export interface LoadBundleTask {
107-
onProgress(
108-
next?: (progress: LoadBundleTaskProgress) => any,
109-
error?: (error: Error) => any,
110-
complete?: (progress?: LoadBundleTaskProgress) => any
111-
): Promise<any>;
112-
113-
then(
114-
onFulfilled?: (a: LoadBundleTaskProgress) => any,
115-
onRejected?: (a: Error) => any
116-
): Promise<any>;
117-
118-
catch(onRejected: (a: Error) => any): Promise<any>;
119-
}
120-
121-
export class LoadBundleTaskImpl implements LoadBundleTask {
103+
export class LoadBundleTaskImpl implements firestore.LoadBundleTask {
122104
private progressResolver = new Deferred<any>();
123-
private progressNext?: (progress: LoadBundleTaskProgress) => any;
105+
private progressNext?: (progress: firestore.LoadBundleTaskProgress) => any;
124106
private progressError?: (err: Error) => any;
125-
private progressComplete?: (progress?: LoadBundleTaskProgress) => any;
107+
private progressComplete?: (
108+
progress?: firestore.LoadBundleTaskProgress
109+
) => any;
126110

127111
private promiseResolver = new Deferred<any>();
128-
private promiseFulfilled?: (a: LoadBundleTaskProgress) => any;
129-
private promiseRejected?: (a: Error) => any;
112+
private promiseFulfilled?: (
113+
progress: firestore.LoadBundleTaskProgress
114+
) => any;
115+
private promiseRejected?: (err: Error) => any;
130116

131-
private lastProgress: LoadBundleTaskProgress = {
117+
private lastProgress: firestore.LoadBundleTaskProgress = {
132118
taskState: 'Running',
133119
totalBytes: 0,
134120
totalDocuments: 0,
@@ -137,9 +123,9 @@ export class LoadBundleTaskImpl implements LoadBundleTask {
137123
};
138124

139125
onProgress(
140-
next?: (progress: LoadBundleTaskProgress) => any,
126+
next?: (progress: firestore.LoadBundleTaskProgress) => any,
141127
error?: (err: Error) => any,
142-
complete?: (progress?: LoadBundleTaskProgress) => void
128+
complete?: (progress?: firestore.LoadBundleTaskProgress) => void
143129
): Promise<any> {
144130
this.progressNext = next;
145131
this.progressError = error;
@@ -153,15 +139,20 @@ export class LoadBundleTaskImpl implements LoadBundleTask {
153139
}
154140

155141
then(
156-
onFulfilled?: (a: LoadBundleTaskProgress) => any,
142+
onFulfilled?: (a: firestore.LoadBundleTaskProgress) => any,
157143
onRejected?: (a: Error) => any
158144
): Promise<any> {
159145
this.promiseFulfilled = onFulfilled;
160146
this.promiseRejected = onRejected;
161147
return this.promiseResolver.promise;
162148
}
149+
/* eslint-enable @typescript-eslint/no-explicit-any */
163150

164-
completeWith(progress: LoadBundleTaskProgress): void {
151+
/**
152+
* Notifies the completion of loading a bundle, with a provided
153+
* `LoadBundleTaskProgress` object.
154+
*/
155+
completeWith(progress: firestore.LoadBundleTaskProgress): void {
165156
let result;
166157
if (this.progressComplete) {
167158
result = this.progressComplete(progress);
@@ -175,9 +166,13 @@ export class LoadBundleTaskImpl implements LoadBundleTask {
175166
this.promiseResolver.resolve(result);
176167
}
177168

169+
/**
170+
* Notifies a failure of loading a bundle, with a provided `Error`
171+
* as the reason.
172+
*/
178173
failedWith(error: Error): void {
179174
if (this.progressNext) {
180-
this.lastProgress!.taskState = 'Error';
175+
this.lastProgress.taskState = 'Error';
181176
this.progressNext(this.lastProgress);
182177
}
183178

@@ -194,44 +189,65 @@ export class LoadBundleTaskImpl implements LoadBundleTask {
194189
this.promiseResolver.reject(result);
195190
}
196191

197-
updateProgress(progress: LoadBundleTaskProgress): void {
192+
/**
193+
* Notifies a progress update of loading a bundle.
194+
* @param progress The new progress.
195+
*/
196+
updateProgress(progress: firestore.LoadBundleTaskProgress): void {
198197
this.lastProgress = progress;
199198
if (this.progressNext) {
200199
this.progressNext(progress);
201200
}
202201
}
203202
}
204-
/* eslint-enable @typescript-eslint/no-explicit-any */
205203

206204
export class LoadResult {
207205
constructor(
208-
readonly progress: LoadBundleTaskProgress,
206+
readonly progress: firestore.LoadBundleTaskProgress,
209207
readonly changedDocs?: MaybeDocumentMap
210208
) {}
211209
}
212210

211+
/**
212+
* A class to process the elements from a bundle, load them into local
213+
* storage and provide progress update while loading.
214+
*/
213215
export class BundleLoader {
214-
private progress: LoadBundleTaskProgress;
216+
/** The current progress of loading */
217+
private progress: firestore.LoadBundleTaskProgress;
218+
/**
219+
* The threshold multiplier used to determine whether enough elements are
220+
* batched to be loaded, and a progress update is needed.
221+
*/
215222
private step = 0.01;
223+
/** Batched queries to be saved into storage */
216224
private queries: bundleProto.NamedQuery[] = [];
225+
/** Batched documents to be saved into storage */
217226
private documents: BundledDocuments = [];
227+
/** How many bytes in the bundle are being batched. */
218228
private bytesIncrement = 0;
229+
/** How many documents in the bundle are being batched. */
219230
private documentsIncrement = 0;
231+
/**
232+
* A BundleDocumentMetadata is added to the loader, it is saved here while
233+
* we wait for the actual document.
234+
*/
220235
private unpairedDocumentMetadata: bundleProto.BundledDocumentMetadata | null = null;
221236

222237
constructor(
223238
private metadata: bundleProto.BundleMetadata,
224239
private localStore: LocalStore
225240
) {
226-
this.progress = {
227-
documentsLoaded: 0,
228-
totalDocuments: metadata.totalDocuments!,
229-
bytesLoaded: 0,
230-
totalBytes: metadata.totalBytes!,
231-
taskState: 'Running'
232-
};
241+
this.progress = initialProgress('Running', metadata);
233242
}
234243

244+
/**
245+
* Adds an element from the bundle to the loader.
246+
*
247+
* If adding this element leads to actually saving the batched elements into
248+
* storage, the returned promise will resolve to a `LoadResult`, otherwise
249+
* it will resolve to null.
250+
*/
235251
addSizedElement(element: SizedBundleElement): Promise<LoadResult | null> {
236252
debugAssert(!element.isBundleMetadata(), 'Unexpected bundle metadata.');
237253

@@ -293,7 +309,10 @@ export class BundleLoader {
293309
return new LoadResult({ ...this.progress }, changedDocs);
294310
}
295311

296-
async complete(): Promise<LoadBundleTaskProgress> {
312+
/**
313+
* Update the progress to 'Success' and return the updated progress.
314+
*/
315+
complete(): firestore.LoadBundleTaskProgress {
297316
debugAssert(
298317
this.queries.length === 0 && this.documents.length === 0,
299318
'There are more items needs to be saved but complete() is called.'

packages/firestore/src/core/firestore_client.ts

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

18+
import { LoadBundleTask } from '@firebase/firestore-types';
1819
import { CredentialsProvider } from '../api/credentials';
1920
import { User } from '../auth/user';
2021
import { LocalStore } from '../local/local_store';
@@ -49,7 +50,7 @@ import {
4950
MemoryComponentProvider
5051
} from './component_provider';
5152
import { BundleReader } from '../util/bundle_reader';
52-
import { LoadBundleTask, LoadBundleTaskImpl } from './bundle';
53+
import { LoadBundleTaskImpl } from './bundle';
5354

5455
const LOG_TAG = 'FirestoreClient';
5556
const MAX_CONCURRENT_LIMBO_RESOLUTIONS = 100;

packages/firestore/src/core/sync_engine.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -482,18 +482,19 @@ export class SyncEngine implements RemoteSyncer {
482482
const result = await loader.addSizedElement(element);
483483
if (result) {
484484
task.updateProgress(result.progress);
485-
}
486-
if (result && result.changedDocs) {
487-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
488-
this.emitNewSnapsAndNotifyLocalStore(result.changedDocs);
485+
486+
if (result.changedDocs) {
487+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
488+
this.emitNewSnapsAndNotifyLocalStore(result.changedDocs);
489+
}
489490
}
490491

491492
element = await reader.nextElement();
492493
}
493494

494495
await this.localStore.saveBundle(metadata);
495496

496-
const completeProgress = await loader.complete();
497+
const completeProgress = loader.complete();
497498
task.completeWith(completeProgress);
498499
}
499500

@@ -1329,11 +1330,4 @@ export class MultiTabSyncEngine extends SyncEngine
13291330
.catch(ignoreIfPrimaryLeaseLoss);
13301331
}
13311332
}
1332-
1333-
loadBundle(
1334-
bundleReader: BundleReader,
1335-
task: LoadBundleTaskImpl
1336-
): Promise<void> {
1337-
return super.loadBundle(bundleReader, task);
1338-
}
13391333
}

packages/firestore/test/integration/api/bundle.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@ import {
2626
import { TestBundleBuilder } from '../../util/bundle_data';
2727
import { DatabaseId } from '../../../src/core/database_info';
2828
import { key } from '../../util/helpers';
29-
import { LoadBundleTaskProgress } from '../../../src/core/bundle';
3029
import { EventsAccumulator } from '../util/events_accumulator';
3130

32-
function verifySuccessProgress(p: LoadBundleTaskProgress): void {
31+
function verifySuccessProgress(p: firestore.LoadBundleTaskProgress): void {
3332
expect(p.taskState).to.equal('Success');
3433
expect(p.bytesLoaded).to.be.equal(p.totalBytes);
3534
expect(p.documentsLoaded).to.equal(p.totalDocuments);
3635
}
3736

3837
function verifyInProgress(
39-
p: LoadBundleTaskProgress,
38+
p: firestore.LoadBundleTaskProgress,
4039
expectedDocuments: number
4140
): void {
4241
expect(p.taskState).to.equal('Running');
@@ -83,9 +82,9 @@ apiDescribe('Bundles', (persistence: boolean) => {
8382
return withTestDb(persistence, async db => {
8483
const builder = bundleWithTestDocs(db);
8584

86-
const progresses: LoadBundleTaskProgress[] = [];
87-
let completeProgress: LoadBundleTaskProgress,
88-
fulfillProgress: LoadBundleTaskProgress;
85+
const progresses: firestore.LoadBundleTaskProgress[] = [];
86+
let completeProgress: firestore.LoadBundleTaskProgress,
87+
fulfillProgress: firestore.LoadBundleTaskProgress;
8988
await db
9089
.loadBundle(
9190
encoder.encode(
@@ -132,7 +131,7 @@ apiDescribe('Bundles', (persistence: boolean) => {
132131
return withTestDb(persistence, async db => {
133132
const builder = bundleWithTestDocs(db);
134133

135-
let fulfillProgress: LoadBundleTaskProgress;
134+
let fulfillProgress: firestore.LoadBundleTaskProgress;
136135
await db
137136
.loadBundle(
138137
encoder.encode(
@@ -168,8 +167,8 @@ apiDescribe('Bundles', (persistence: boolean) => {
168167
)
169168
);
170169

171-
let completeProgress: LoadBundleTaskProgress;
172-
const progresses: LoadBundleTaskProgress[] = [];
170+
let completeProgress: firestore.LoadBundleTaskProgress;
171+
const progresses: firestore.LoadBundleTaskProgress[] = [];
173172
await db
174173
.loadBundle(
175174
encoder.encode(

packages/firestore/test/unit/specs/bundle_spec.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describeSpec('Bundles:', [], () => {
241241
.watchAcksFull(query, 250)
242242
// Backend tells there is no such doc.
243243
.expectEvents(query, {})
244-
// Bundle tells otherwise, leads to limbo resolution.
244+
// Bundle tells otherwise, leads to limbo.
245245
.loadBundle(bundleString1)
246246
.expectEvents(query, {
247247
added: [doc('collection/a', 500, { key: 'b' })],

0 commit comments

Comments
 (0)