chore(download): follow up to remove the redundant checks (#3097)

This commit is contained in:
Pavel Feldman 2020-07-22 15:59:11 -07:00 committed by GitHub
parent baa0956915
commit 2a08883ef8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,7 +28,7 @@ export class Download {
private _finishedCallback: () => void; private _finishedCallback: () => void;
private _finishedPromise: Promise<void>; private _finishedPromise: Promise<void>;
private _saveAsRequests: { fulfill: () => void; reject: (error?: any) => void; path: string }[] = []; private _saveAsRequests: { fulfill: () => void; reject: (error?: any) => void; path: string }[] = [];
private _loaded: boolean = false; private _finished: boolean = false;
private _page: Page; private _page: Page;
private _acceptDownloads: boolean; private _acceptDownloads: boolean;
private _failure: string | null = null; private _failure: string | null = null;
@ -75,7 +75,14 @@ export class Download {
} }
async saveAs(path: string) { async saveAs(path: string) {
if (this._loaded) { if (!this._acceptDownloads)
throw new Error('Pass { acceptDownloads: true } when you are creating your browser context.');
if (this._deleted)
throw new Error('Download already deleted. Save before deleting.');
if (this._failure)
throw new Error('Download not found on disk. Check download.failure() for details.');
if (this._finished) {
await this._saveAs(path); await this._saveAs(path);
return; return;
} }
@ -83,15 +90,9 @@ export class Download {
return new Promise((fulfill, reject) => this._saveAsRequests.push({fulfill, reject, path})); return new Promise((fulfill, reject) => this._saveAsRequests.push({fulfill, reject, path}));
} }
async _saveAs(dlPath: string) { async _saveAs(downloadPath: string) {
if (!this._acceptDownloads)
throw new Error('Pass { acceptDownloads: true } when you are creating your browser context.');
const fileName = path.join(this._downloadsPath, this._uuid); const fileName = path.join(this._downloadsPath, this._uuid);
if (this._failure) await util.promisify(fs.copyFile)(fileName, downloadPath);
throw new Error('Download not found on disk. Check download.failure() for details.');
if (this._deleted)
throw new Error('Download already deleted. Save before deleting.');
await util.promisify(fs.copyFile)(fileName, dlPath);
} }
async failure(): Promise<string | null> { async failure(): Promise<string | null> {
@ -118,13 +119,12 @@ export class Download {
} }
async _reportFinished(error?: string) { async _reportFinished(error?: string) {
this._finished = true;
this._failure = error || null;
if (error) { if (error) {
for (const { reject } of this._saveAsRequests) { for (const { reject } of this._saveAsRequests)
if (!this._acceptDownloads) reject(error);
reject(new Error('Pass { acceptDownloads: true } when you are creating your browser context.'));
else
reject(error);
}
} else { } else {
for (const { fulfill, reject, path } of this._saveAsRequests) { for (const { fulfill, reject, path } of this._saveAsRequests) {
try { try {
@ -136,8 +136,6 @@ export class Download {
} }
} }
this._loaded = true;
this._failure = error || null;
this._finishedCallback(); this._finishedCallback();
} }
} }