Skip to content

Add bundles to d.ts and rearrange bundles source code for building it as a separate module #4120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 3, 2020
35 changes: 35 additions & 0 deletions packages/firebase/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import { DocumentData, LoadBundleTask, Query } from '@firebase/firestore-types';

/**
* <code>firebase</code> is a global namespace from which all Firebase
* services are accessed.
Expand Down Expand Up @@ -8289,12 +8291,45 @@ declare namespace firebase.firestore {
*/
terminate(): Promise<void>;

loadBundle(
bundleData: ArrayBuffer | ReadableStream<ArrayBuffer> | string
): LoadBundleTask;

namedQuery(name: string): Promise<Query<DocumentData> | null>;

/**
* @hidden
*/
INTERNAL: { delete: () => Promise<void> };
}

export interface LoadBundleTask {
onProgress(
next?: (progress: LoadBundleTaskProgress) => any,
error?: (error: Error) => any,
complete?: () => void
): void;

then<T, R>(
onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike<T>,
onRejected?: (a: Error) => R | PromiseLike<R>
): Promise<T | R>;

catch<R>(
onRejected: (a: Error) => R | PromiseLike<R>
): Promise<R | LoadBundleTaskProgress>;
}

export interface LoadBundleTaskProgress {
documentsLoaded: number;
totalDocuments: number;
bytesLoaded: number;
totalBytes: number;
taskState: TaskState;
}

export type TaskState = 'Error' | 'Running' | 'Success';

/**
* An immutable object representing a geo point in Firestore. The geo point
* is represented as latitude/longitude pair.
Expand Down
33 changes: 33 additions & 0 deletions packages/firestore-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,42 @@ export class FirebaseFirestore {

terminate(): Promise<void>;

loadBundle(
bundleData: ArrayBuffer | ReadableStream<ArrayBuffer> | string
): LoadBundleTask;

namedQuery(name: string): Promise<Query<DocumentData> | null>;

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

export interface LoadBundleTask {
onProgress(
next?: (progress: LoadBundleTaskProgress) => any,
error?: (error: Error) => any,
complete?: () => void
): void;

then<T, R>(
onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike<T>,
onRejected?: (a: Error) => R | PromiseLike<R>
): Promise<T | R>;

catch<R>(
onRejected: (a: Error) => R | PromiseLike<R>
): Promise<R | LoadBundleTaskProgress>;
}

export interface LoadBundleTaskProgress {
documentsLoaded: number;
totalDocuments: number;
bytesLoaded: number;
totalBytes: number;
taskState: TaskState;
}

export type TaskState = 'Error' | 'Running' | 'Success';

export class GeoPoint {
constructor(latitude: number, longitude: number);

Expand Down
37 changes: 37 additions & 0 deletions packages/firestore/exp-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,43 @@ export function snapshotEqual<T>(
right: DocumentSnapshot<T> | QuerySnapshot<T>
): boolean;

export interface LoadBundleTask {
onProgress(
next?: (progress: LoadBundleTaskProgress) => any,
error?: (error: Error) => any,
complete?: () => void
): void;

then<T, R>(
onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike<T>,
onRejected?: (a: Error) => R | PromiseLike<R>
): Promise<T | R>;

catch<R>(
onRejected: (a: Error) => R | PromiseLike<R>
): Promise<R | LoadBundleTaskProgress>;
}

export interface LoadBundleTaskProgress {
documentsLoaded: number;
totalDocuments: number;
bytesLoaded: number;
totalBytes: number;
taskState: TaskState;
}

export type TaskState = 'Error' | 'Running' | 'Success';

export function loadBundle(
firestore: FirebaseFirestore,
bundleData: ArrayBuffer | ReadableStream<Uint8Array> | string
): LoadBundleTask;

export function namedQuery(
firestore: FirebaseFirestore,
name: string
): Promise<Query<DocumentData> | null>;

export type FirestoreErrorCode =
| 'cancelled'
| 'unknown'
Expand Down
39 changes: 39 additions & 0 deletions packages/firestore/index.bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Firestore } from './src/api/database';
import { loadBundle, namedQuery } from './src/api/bundle';

/**
* Registers Firestore bundle loading features with the components framework.
*/
export function registerBundle(instance: typeof Firestore): void {
instance.prototype.loadBundle = function (
this: Firestore,
data: ArrayBuffer | ReadableStream<Uint8Array> | string
) {
return loadBundle(this, data);
};
instance.prototype.namedQuery = function (
this: Firestore,
queryName: string
) {
return namedQuery(this, queryName);
};
}

registerBundle(Firestore);
84 changes: 46 additions & 38 deletions packages/firestore/src/api/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,27 @@
* limitations under the License.
*/

import {
LoadBundleTask as ApiLoadBundleTask,
LoadBundleTaskProgress
} from '@firebase/firestore-types';
import { Deferred } from '../util/promise';
import { PartialObserver } from './observer';
import { debugAssert } from '../util/assert';
import { FirestoreError } from '../util/error';

export interface ApiLoadBundleTask {
onProgress(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
next?: (progress: ApiLoadBundleTaskProgress) => any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
error?: (error: Error) => any,
complete?: () => void
): void;

then<T, R>(
onFulfilled?: (a: ApiLoadBundleTaskProgress) => T | PromiseLike<T>,
onRejected?: (a: Error) => R | PromiseLike<R>
): Promise<T | R>;

catch<R>(
onRejected: (a: Error) => R | PromiseLike<R>
): Promise<R | ApiLoadBundleTaskProgress>;
}

export interface ApiLoadBundleTaskProgress {
documentsLoaded: number;
totalDocuments: number;
bytesLoaded: number;
totalBytes: number;
taskState: TaskState;
}

export type TaskState = 'Error' | 'Running' | 'Success';
import { ensureFirestoreConfigured, Query, Firestore } from './database';
import { Query as ExpQuery } from '../../exp/src/api/reference';
import {
firestoreClientGetNamedQuery,
firestoreClientLoadBundle
} from '../core/firestore_client';

export class LoadBundleTask
implements ApiLoadBundleTask, PromiseLike<ApiLoadBundleTaskProgress> {
private _progressObserver: PartialObserver<ApiLoadBundleTaskProgress> = {};
private _taskCompletionResolver = new Deferred<ApiLoadBundleTaskProgress>();
implements ApiLoadBundleTask, PromiseLike<LoadBundleTaskProgress> {
private _progressObserver: PartialObserver<LoadBundleTaskProgress> = {};
private _taskCompletionResolver = new Deferred<LoadBundleTaskProgress>();

private _lastProgress: ApiLoadBundleTaskProgress = {
private _lastProgress: LoadBundleTaskProgress = {
taskState: 'Running',
totalBytes: 0,
totalDocuments: 0,
Expand All @@ -63,7 +44,7 @@ export class LoadBundleTask
};

onProgress(
next?: (progress: ApiLoadBundleTaskProgress) => unknown,
next?: (progress: LoadBundleTaskProgress) => unknown,
error?: (err: Error) => unknown,
complete?: () => void
): void {
Expand All @@ -76,12 +57,12 @@ export class LoadBundleTask

catch<R>(
onRejected: (a: Error) => R | PromiseLike<R>
): Promise<R | ApiLoadBundleTaskProgress> {
): Promise<R | LoadBundleTaskProgress> {
return this._taskCompletionResolver.promise.catch(onRejected);
}

then<T, R>(
onFulfilled?: (a: ApiLoadBundleTaskProgress) => T | PromiseLike<T>,
onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike<T>,
onRejected?: (a: Error) => R | PromiseLike<R>
): Promise<T | R> {
return this._taskCompletionResolver.promise.then(onFulfilled, onRejected);
Expand All @@ -91,7 +72,7 @@ export class LoadBundleTask
* Notifies all observers that bundle loading has completed, with a provided
* `LoadBundleTaskProgress` object.
*/
_completeWith(progress: ApiLoadBundleTaskProgress): void {
_completeWith(progress: LoadBundleTaskProgress): void {
debugAssert(
progress.taskState === 'Success',
'Task is not completed with Success.'
Expand Down Expand Up @@ -126,7 +107,7 @@ export class LoadBundleTask
* Notifies a progress update of loading a bundle.
* @param progress - The new progress.
*/
_updateProgress(progress: ApiLoadBundleTaskProgress): void {
_updateProgress(progress: LoadBundleTaskProgress): void {
debugAssert(
this._lastProgress.taskState === 'Running',
'Cannot update progress on a completed or failed task'
Expand All @@ -138,3 +119,30 @@ export class LoadBundleTask
}
}
}

export function loadBundle(
db: Firestore,
bundleData: ArrayBuffer | ReadableStream<Uint8Array> | string
): LoadBundleTask {
const resultTask = new LoadBundleTask();
firestoreClientLoadBundle(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should drop the Promise from firestoreClientLoadBundle. It doesn't seem to be needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

ensureFirestoreConfigured(db._delegate),
db._databaseId,
bundleData,
resultTask
);
return resultTask;
}

export function namedQuery(db: Firestore, name: string): Promise<Query | null> {
return firestoreClientGetNamedQuery(
ensureFirestoreConfigured(db._delegate),
name
).then(namedQuery => {
if (!namedQuery) {
return null;
}

return new Query(db, new ExpQuery(db._delegate, null, namedQuery.query));
});
}
Loading