Skip to content

Commit 2a08883

Browse files
authored
chore(download): follow up to remove the redundant checks (#3097)
1 parent baa0956 commit 2a08883

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/download.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class Download {
2828
private _finishedCallback: () => void;
2929
private _finishedPromise: Promise<void>;
3030
private _saveAsRequests: { fulfill: () => void; reject: (error?: any) => void; path: string }[] = [];
31-
private _loaded: boolean = false;
31+
private _finished: boolean = false;
3232
private _page: Page;
3333
private _acceptDownloads: boolean;
3434
private _failure: string | null = null;
@@ -75,23 +75,24 @@ export class Download {
7575
}
7676

7777
async saveAs(path: string) {
78-
if (this._loaded) {
78+
if (!this._acceptDownloads)
79+
throw new Error('Pass { acceptDownloads: true } when you are creating your browser context.');
80+
if (this._deleted)
81+
throw new Error('Download already deleted. Save before deleting.');
82+
if (this._failure)
83+
throw new Error('Download not found on disk. Check download.failure() for details.');
84+
85+
if (this._finished) {
7986
await this._saveAs(path);
8087
return;
8188
}
8289

8390
return new Promise((fulfill, reject) => this._saveAsRequests.push({fulfill, reject, path}));
8491
}
8592

86-
async _saveAs(dlPath: string) {
87-
if (!this._acceptDownloads)
88-
throw new Error('Pass { acceptDownloads: true } when you are creating your browser context.');
93+
async _saveAs(downloadPath: string) {
8994
const fileName = path.join(this._downloadsPath, this._uuid);
90-
if (this._failure)
91-
throw new Error('Download not found on disk. Check download.failure() for details.');
92-
if (this._deleted)
93-
throw new Error('Download already deleted. Save before deleting.');
94-
await util.promisify(fs.copyFile)(fileName, dlPath);
95+
await util.promisify(fs.copyFile)(fileName, downloadPath);
9596
}
9697

9798
async failure(): Promise<string | null> {
@@ -118,13 +119,12 @@ export class Download {
118119
}
119120

120121
async _reportFinished(error?: string) {
122+
this._finished = true;
123+
this._failure = error || null;
124+
121125
if (error) {
122-
for (const { reject } of this._saveAsRequests) {
123-
if (!this._acceptDownloads)
124-
reject(new Error('Pass { acceptDownloads: true } when you are creating your browser context.'));
125-
else
126-
reject(error);
127-
}
126+
for (const { reject } of this._saveAsRequests)
127+
reject(error);
128128
} else {
129129
for (const { fulfill, reject, path } of this._saveAsRequests) {
130130
try {
@@ -136,8 +136,6 @@ export class Download {
136136
}
137137
}
138138

139-
this._loaded = true;
140-
this._failure = error || null;
141139
this._finishedCallback();
142140
}
143141
}

0 commit comments

Comments
 (0)