Skip to content

Commit e712d55

Browse files
committed
Multiple fixes and performance improvements
1 parent 0516d0c commit e712d55

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/js/script.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ function generateRoomID() {
311311
// On document loaded, execute onLoad() method.
312312
(() => onLoad())();
313313

314+
// Display a confirmation dialog when the user attempts to refresh or navigate away from the page.
315+
window.addEventListener("beforeunload", (event) => {
316+
event.preventDefault();
317+
});
318+
314319
// Focus the password input after the fade animation
315320
password_modal.addEventListener('shown.bs.modal', () => {
316321
password_modal_value.focus()

src/js/webrtc/file.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class File {
7575
return this._in_progress
7676
}
7777

78+
set in_progress(value) {
79+
this._in_progress = value
80+
}
81+
7882
get aborted() {
7983
return this._aborted
8084
}
@@ -153,16 +157,19 @@ class File {
153157
document.getElementById(`file-${this._id}-progress`).innerHTML = '0% | '
154158

155159
// Store peer data
156-
this._remotePeers[data.peer_id] = {"user_id": data.requester_id, "user_name": data.requester_name, "peer": null, "conn": null, "online": true, "interval": setInterval(() => this._isAlive(conn.peer), 1000), "progress": 0, "aborted": false}
160+
this._remotePeers[data.peer_id] = {"user_id": data.requester_id, "user_name": data.requester_name, "peer": null, "conn": null, "online": true, "interval": null, "progress": 0, "aborted": false}
157161

158162
// Connect to peer_id
159163
await this.connect(data.peer_id)
160164

161165
// Get connection
162166
const conn = this._remotePeers[data.peer_id].conn
163167

168+
// Init interval to check connection status
169+
this._remotePeers[data.peer_id].interval = setInterval(() => this._isAlive(conn.peer), 500)
170+
164171
// Define vars for chunk processing
165-
const chunkSize = 1024**2 // 1 MB
172+
const chunkSize = 65536 // 64 KB
166173
let offset = 0
167174

168175
// Recursive function to process each chunk
@@ -225,7 +232,6 @@ class File {
225232
this._chunks = []
226233
this._transferred = 0
227234
this._aborted = false
228-
this._in_progress = true
229235
}
230236
}
231237

@@ -305,7 +311,6 @@ class File {
305311

306312
// Update internal parameters
307313
this._in_progress = false
308-
this._zip = false
309314

310315
// Close connection
311316
conn.close()
@@ -329,17 +334,16 @@ class File {
329334
// Update UI: Show the overall progress
330335
document.getElementById(`file-${this._id}-progress`).innerHTML = `${overall_progress}% | `
331336

332-
if (!this._aborted && onlinePeers.filter(x => !x.aborted).length == 0) {
333-
document.getElementById(`file-${this._id}-progress`).innerHTML = `${overall_progress}% | `
334-
document.getElementById(`file-${this._id}-icon-loading`).style.display = 'none'
335-
document.getElementById(`file-${this._id}-error`).style.display = 'block'
336-
document.getElementById(`file-${this._id}-error`).innerHTML = 'All users stopped the file transfer.'
337-
}
338-
else if (overall_progress == 100) {
337+
if (overall_progress == 100) {
339338
document.getElementById(`file-${this._id}-abort`).style.display = 'none'
340339
document.getElementById(`file-${this._id}-icon-loading`).style.display = 'none'
341340
document.getElementById(`file-${this._id}-icon-success`).style.display = 'block'
342341
}
342+
else if (!this._aborted && onlinePeers.filter(x => !x.aborted).length == 0) {
343+
document.getElementById(`file-${this._id}-icon-loading`).style.display = 'none'
344+
document.getElementById(`file-${this._id}-error`).style.display = 'block'
345+
document.getElementById(`file-${this._id}-error`).innerHTML = 'All users stopped the file transfer.'
346+
}
343347
}
344348

345349
_onFileAborted(conn) {

src/js/webrtc/user.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class User {
297297
"file": null,
298298
"current": 0,
299299
"sizes": files.map(x => x.size),
300-
"interval": setInterval(() => this._downloadAllProgress(), 1000),
300+
"interval": setInterval(() => this._downloadAllProgress(), 500),
301301
};
302302

303303
for (let file of files) {
@@ -314,6 +314,9 @@ class User {
314314
// Init Peering connection to receive the file
315315
await file.init()
316316

317+
// Mark the file to be in progress
318+
file.in_progress = true
319+
317320
// State to convert the file in zip, instead of downloading it
318321
file.zip = true
319322

@@ -349,7 +352,7 @@ class User {
349352
}
350353

351354
// Disable zip mode
352-
for (let file of files) file.zip = false
355+
for (let file of Object.values(this._files)) file.zip = false
353356
}
354357

355358
_waitFileTranster(file) {
@@ -359,22 +362,23 @@ class User {
359362
clearInterval(checkInterval);
360363
resolve();
361364
}
362-
}, 100);
365+
}, 500);
363366
});
364367
}
365368

366369
downloadAllCancel() {
367370
// Abort the downloadAll operation
368371
this._downloadAll.active = false
369372
this._downloadAll.aborted = true
373+
374+
// Update UI
370375
download_modal_cancel.setAttribute("disabled", "")
371376
download_modal_cancel_spinner.style.display = 'inline-block'
372377
}
373378

374379
_downloadAllProgress() {
375380
// Abort file transfer
376381
if (!this._downloadAll.active) {
377-
this._downloadAll.file.abort()
378382
clearInterval(this._downloadAll.interval)
379383
}
380384

@@ -387,7 +391,7 @@ class User {
387391
download_modal_value.innerHTML = `${Math.floor(overallProgress)}%`
388392
download_modal_active.querySelector('.progress-bar').style.width = `${Math.floor(overallProgress)}%`
389393

390-
if (this._downloadAll.active && overallProgress == 100) {
394+
if (overallProgress == 100) {
391395
clearInterval(this._downloadAll.interval)
392396
download_modal_active.style.display = 'none'
393397
download_modal_success.style.display = 'flex'
@@ -689,7 +693,8 @@ class User {
689693
}
690694
// Redirect
691695
else {
692-
this._remotePeers[data.peer_id].conn.send({"webrtc-file-download": data})
696+
const owner_id = this._files[data.file_id].owner_id
697+
this._remotePeers[owner_id].conn.send({"webrtc-file-download": data})
693698
}
694699
}
695700

0 commit comments

Comments
 (0)