-
Notifications
You must be signed in to change notification settings - Fork 928
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
Changes from 1 commit
e11f1fe
79b1ed2
6000ea1
326bb02
fe8ac37
0b1a4c7
686d333
571ddcf
3f83901
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 the memory-only Firestore build with the components framework. | ||
*/ | ||
export function registerBundle(instance: typeof Firestore): void { | ||
instance.prototype.loadBundle = function ( | ||
data: ArrayBuffer | ReadableStream<Uint8Array> | string | ||
) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return loadBundle(this as any, data); | ||
}; | ||
wu-hui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
instance.prototype.namedQuery = function (queryName: string) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return namedQuery(this as any, queryName); | ||
}; | ||
|
||
//TODO: add loadBundle and namedQuery to the firestore namespace | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please assign TODO to yourself or to a bug There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was added by Fei, I actually don't know what he meant by this. Added my name anyways. |
||
} | ||
|
||
registerBundle(Firestore); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,46 +15,24 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import { 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 { | ||
firestoreClientGetNamedQuery, | ||
firestoreClientLoadBundle | ||
} from '../core/bundle'; | ||
import { Query as ExpQuery } from '../../exp/src/api/reference'; | ||
|
||
export class LoadBundleTask | ||
implements ApiLoadBundleTask, PromiseLike<ApiLoadBundleTaskProgress> { | ||
private _progressObserver: PartialObserver<ApiLoadBundleTaskProgress> = {}; | ||
private _taskCompletionResolver = new Deferred<ApiLoadBundleTaskProgress>(); | ||
implements LoadBundleTask, PromiseLike<LoadBundleTaskProgress> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meant to be the interface from d.ts, fixed. |
||
private _progressObserver: PartialObserver<LoadBundleTaskProgress> = {}; | ||
private _taskCompletionResolver = new Deferred<LoadBundleTaskProgress>(); | ||
|
||
private _lastProgress: ApiLoadBundleTaskProgress = { | ||
private _lastProgress: LoadBundleTaskProgress = { | ||
taskState: 'Running', | ||
totalBytes: 0, | ||
totalDocuments: 0, | ||
|
@@ -63,7 +41,7 @@ export class LoadBundleTask | |
}; | ||
|
||
onProgress( | ||
next?: (progress: ApiLoadBundleTaskProgress) => unknown, | ||
next?: (progress: LoadBundleTaskProgress) => unknown, | ||
error?: (err: Error) => unknown, | ||
complete?: () => void | ||
): void { | ||
|
@@ -76,12 +54,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); | ||
|
@@ -91,7 +69,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.' | ||
|
@@ -126,7 +104,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' | ||
|
@@ -138,3 +116,30 @@ export class LoadBundleTask | |
} | ||
} | ||
} | ||
|
||
export function loadBundle( | ||
db: Firestore, | ||
bundleData: ArrayBuffer | ReadableStream<Uint8Array> | string | ||
): LoadBundleTask { | ||
const resultTask = new LoadBundleTask(); | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
firestoreClientLoadBundle( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should drop the Promise from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
ensureFirestoreConfigured(db._delegate), | ||
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)); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment needs updating.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.