Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/core/storage-js/src/packages/BlobDownloadBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { isStorageError } from '../lib/errors'
import { DownloadResult } from '../lib/types'
import StreamDownloadBuilder from './StreamDownloadBuilder'

export default class BlobDownloadBuilder implements PromiseLike<DownloadResult<Blob>> {
export default class BlobDownloadBuilder implements Promise<DownloadResult<Blob>> {
readonly [Symbol.toStringTag]: string = 'BlobDownloadBuilder'
private promise: Promise<DownloadResult<Blob>> | null = null

constructor(
private downloadFn: () => Promise<Response>,
private shouldThrowOnError: boolean
Expand All @@ -16,7 +19,24 @@ export default class BlobDownloadBuilder implements PromiseLike<DownloadResult<B
onfulfilled?: ((value: DownloadResult<Blob>) => TResult1 | PromiseLike<TResult1>) | null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
return this.execute().then(onfulfilled, onrejected)
return this.getPromise().then(onfulfilled, onrejected)
}

catch<TResult = never>(
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null
): Promise<DownloadResult<Blob> | TResult> {
return this.getPromise().catch(onrejected)
}

finally(onfinally?: (() => void) | null): Promise<DownloadResult<Blob>> {
return this.getPromise().finally(onfinally)
}

private getPromise(): Promise<DownloadResult<Blob>> {
if (!this.promise) {
this.promise = this.execute()
}
return this.promise
}

private async execute(): Promise<DownloadResult<Blob>> {
Expand Down