Skip to content

Commit

Permalink
allow multiple file transfer requests by multiple peers simultaneousl…
Browse files Browse the repository at this point in the history
…y by putting them in a queue
  • Loading branch information
schlagmichdoch committed Feb 8, 2023
1 parent b2d6a62 commit d111bbf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
38 changes: 19 additions & 19 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,7 @@ <h2 class="center">Unpair Devices</h2>
</x-background>
</form>
</x-dialog>
<!-- Receive File Dialog -->
<x-dialog id="receiveFileDialog">
<x-background class="full center">
<x-paper shadow="2">
<h2 class="center" id="receiveTitle"></h2>
<div class="text-center file-description"></div>
<div class="font-body2 text-center file-size"></div>
<div class="center file-preview"></div>
<div class="row-reverse space-between">
<a class="button" id="shareOrDownload" autofocus></a>
<div class="separator"></div>
<button class="button" close>Close</button>
</div>
</x-paper>
</x-background>
</x-dialog>
<!-- Receive Dialog -->
<!-- Receive Request Dialog -->
<x-dialog id="receiveRequestDialog">
<x-background class="full center">
<x-paper shadow="2">
Expand All @@ -171,9 +155,25 @@ <h2 class="center">PairDrop</h2>
<div class="font-body2 text-center file-size"></div>
<div class="center file-preview"></div>
<div class="row-reverse space-between">
<button class="button" id="acceptRequest" title="ENTER" close autofocus>Accept</button>
<button class="button" id="acceptRequest" title="ENTER" autofocus>Accept</button>
<div class="separator"></div>
<button class="button" id="declineRequest" title="ESCAPE" close>Decline</button>
<button class="button" id="declineRequest" title="ESCAPE">Decline</button>
</div>
</x-paper>
</x-background>
</x-dialog>
<!-- Receive File Dialog -->
<x-dialog id="receiveFileDialog">
<x-background class="full center">
<x-paper shadow="2">
<h2 class="center" id="receiveTitle"></h2>
<div class="text-center file-description"></div>
<div class="font-body2 text-center file-size"></div>
<div class="center file-preview"></div>
<div class="row-reverse space-between">
<a class="button" id="shareOrDownload" autofocus></a>
<div class="separator"></div>
<button class="button" close>Close</button>
</div>
</x-paper>
</x-background>
Expand Down
34 changes: 23 additions & 11 deletions public/scripts/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ class PeerUI {
}

class Dialog {
constructor(id, hideOnDisconnect = true) {
constructor(id) {
this.$el = $(id);
this.$el.querySelectorAll('[close]').forEach(el => el.addEventListener('click', _ => this.hide()))
this.$autoFocus = this.$el.querySelector('[autofocus]');
if (hideOnDisconnect) Events.on('peer-disconnected', e => this._onPeerDisconnected(e.detail));
Events.on('peer-disconnected', e => this._onPeerDisconnected(e.detail));
}

show() {
Expand All @@ -428,20 +428,19 @@ class Dialog {
}
document.title = 'PairDrop';
document.changeFavicon("images/favicon-96x96.png");
if (this.correspondingPeerId) setTimeout(_ => this.correspondingPeerId = undefined, 300);
}

_onPeerDisconnected(peerId) {
if (this.correspondingPeerId && this.correspondingPeerId === peerId) {
if (this.correspondingPeerId === peerId) {
this.hide();
Events.fire('notify-user', 'Selected peer left.')
}
}
}

class ReceiveDialog extends Dialog {
constructor(id, hideOnDisconnect = true) {
super(id, hideOnDisconnect);
constructor(id) {
super(id);

this.$fileDescriptionNode = this.$el.querySelector('.file-description');
this.$fileSizeNode = this.$el.querySelector('.file-size');
Expand All @@ -466,7 +465,7 @@ class ReceiveDialog extends Dialog {
class ReceiveFileDialog extends ReceiveDialog {

constructor() {
super('receiveFileDialog', false);
super('receiveFileDialog');

this.$shareOrDownloadBtn = this.$el.querySelector('#shareOrDownload');
this.$receiveTitleNode = this.$el.querySelector('#receiveTitle')
Expand Down Expand Up @@ -632,16 +631,28 @@ class ReceiveRequestDialog extends ReceiveDialog {

Events.on('files-transfer-request', e => this._onRequestFileTransfer(e.detail.request, e.detail.peerId))
Events.on('keydown', e => this._onKeyDown(e));
this._filesTransferRequestQueue = [];
}

_onKeyDown(e) {
if (this.$el.attributes["show"] && e.code === "Escape") {
this._respondToFileTransferRequest(false)
setTimeout(_ => this.hide(), 500);
}
}

_onRequestFileTransfer(request, peerId) {
this._filesTransferRequestQueue.push({request: request, peerId: peerId});
if (this.$el.attributes["show"]) return;
this._dequeueRequests();
}

_dequeueRequests() {
if (!this._filesTransferRequestQueue.length) return;
let {request, peerId} = this._filesTransferRequestQueue.shift();
this._showRequestDialog(request, peerId)
}

_showRequestDialog(request, peerId) {
this.correspondingPeerId = peerId;

const peer = $(peerId);
Expand Down Expand Up @@ -684,12 +695,13 @@ class ReceiveRequestDialog extends ReceiveDialog {
Events.fire('set-progress', {peerId: this.correspondingPeerId, progress: 0, status: 'wait'});
NoSleepUI.enable();
}
this.hide();
}

hide() {
this.$previewBox.innerHTML = '';
this.$fileOtherNode.innerText = '';
super.hide();
this._dequeueRequests();
}
}

Expand Down Expand Up @@ -974,7 +986,7 @@ class SendTextDialog extends Dialog {

class ReceiveTextDialog extends Dialog {
constructor() {
super('receiveTextDialog', false);
super('receiveTextDialog');
Events.on('text-received', e => this._onText(e.detail))
this.$text = this.$el.querySelector('#text');
const copy = this.$el.querySelector('#copy');
Expand Down Expand Up @@ -1020,7 +1032,7 @@ class ReceiveTextDialog extends Dialog {
class Base64ZipDialog extends Dialog {

constructor() {
super('base64ZipDialog', false);
super('base64ZipDialog');
const urlParams = new URL(window.location).searchParams;
const base64Zip = urlParams.get('base64zip');
const base64Text = urlParams.get('base64text');
Expand Down
2 changes: 1 addition & 1 deletion public/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cacheVersion = 'v6';
const cacheVersion = 'v7';
const cacheTitle = `pairdrop-cache-${cacheVersion}`;
const urlsToCache = [
'index.html',
Expand Down

0 comments on commit d111bbf

Please sign in to comment.