|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { Deferred } from '../util/promise'; |
| 19 | +import { PartialObserver } from './observer'; |
| 20 | +import { debugAssert } from '../util/assert'; |
| 21 | +import { FirestoreError } from '../util/error'; |
| 22 | + |
| 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 | + |
| 52 | +export class LoadBundleTask |
| 53 | + implements ApiLoadBundleTask, PromiseLike<ApiLoadBundleTaskProgress> { |
| 54 | + private _progressObserver: PartialObserver<ApiLoadBundleTaskProgress> = {}; |
| 55 | + private _taskCompletionResolver = new Deferred<ApiLoadBundleTaskProgress>(); |
| 56 | + |
| 57 | + private _lastProgress: ApiLoadBundleTaskProgress = { |
| 58 | + taskState: 'Running', |
| 59 | + totalBytes: 0, |
| 60 | + totalDocuments: 0, |
| 61 | + bytesLoaded: 0, |
| 62 | + documentsLoaded: 0 |
| 63 | + }; |
| 64 | + |
| 65 | + onProgress( |
| 66 | + next?: (progress: ApiLoadBundleTaskProgress) => unknown, |
| 67 | + error?: (err: Error) => unknown, |
| 68 | + complete?: () => void |
| 69 | + ): void { |
| 70 | + this._progressObserver = { |
| 71 | + next, |
| 72 | + error, |
| 73 | + complete |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + catch<R>( |
| 78 | + onRejected: (a: Error) => R | PromiseLike<R> |
| 79 | + ): Promise<R | ApiLoadBundleTaskProgress> { |
| 80 | + return this._taskCompletionResolver.promise.catch(onRejected); |
| 81 | + } |
| 82 | + |
| 83 | + then<T, R>( |
| 84 | + onFulfilled?: (a: ApiLoadBundleTaskProgress) => T | PromiseLike<T>, |
| 85 | + onRejected?: (a: Error) => R | PromiseLike<R> |
| 86 | + ): Promise<T | R> { |
| 87 | + return this._taskCompletionResolver.promise.then(onFulfilled, onRejected); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Notifies all observers that bundle loading has completed, with a provided |
| 92 | + * `LoadBundleTaskProgress` object. |
| 93 | + */ |
| 94 | + _completeWith(progress: ApiLoadBundleTaskProgress): void { |
| 95 | + debugAssert( |
| 96 | + progress.taskState === 'Success', |
| 97 | + 'Task is not completed with Success.' |
| 98 | + ); |
| 99 | + this._updateProgress(progress); |
| 100 | + if (this._progressObserver.complete) { |
| 101 | + this._progressObserver.complete(); |
| 102 | + } |
| 103 | + |
| 104 | + this._taskCompletionResolver.resolve(progress); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Notifies all observers that bundle loading has failed, with a provided |
| 109 | + * `Error` as the reason. |
| 110 | + */ |
| 111 | + _failWith(error: FirestoreError): void { |
| 112 | + this._lastProgress.taskState = 'Error'; |
| 113 | + |
| 114 | + if (this._progressObserver.next) { |
| 115 | + this._progressObserver.next(this._lastProgress); |
| 116 | + } |
| 117 | + |
| 118 | + if (this._progressObserver.error) { |
| 119 | + this._progressObserver.error(error); |
| 120 | + } |
| 121 | + |
| 122 | + this._taskCompletionResolver.reject(error); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Notifies a progress update of loading a bundle. |
| 127 | + * @param progress The new progress. |
| 128 | + */ |
| 129 | + _updateProgress(progress: ApiLoadBundleTaskProgress): void { |
| 130 | + debugAssert( |
| 131 | + this._lastProgress.taskState === 'Running', |
| 132 | + 'Cannot update progress on a completed or failed task' |
| 133 | + ); |
| 134 | + |
| 135 | + this._lastProgress = progress; |
| 136 | + if (this._progressObserver.next) { |
| 137 | + this._progressObserver.next(progress); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments