Refactor BrowserTabsConnector and PeersManager

This commit is contained in:
schlagmichdoch 2024-05-16 19:37:32 +02:00
parent 00f1a20177
commit 9b3571feac
3 changed files with 75 additions and 30 deletions

View file

@ -2,18 +2,27 @@ class BrowserTabsConnector {
constructor() {
this.bc = new BroadcastChannel('pairdrop');
this.bc.addEventListener('message', e => this._onMessage(e));
Events.on('broadcast-send', e => this._broadcastSend(e.detail));
Events.on('broadcast-send', e => this._broadcastSend(e.detail.type, e.detail.data));
Events.on('broadcast-self-display-name-changed', e => this._onBroadcastSelfDisplayNameChanged(e.detail.displayName));
}
_broadcastSend(message) {
this.bc.postMessage(message);
_broadcastSend(type, data) {
this.bc.postMessage({ type, data });
}
_onBroadcastSelfDisplayNameChanged(displayName) {
this._broadcastSend('self-display-name-changed', { displayName: displayName });
}
_onMessage(e) {
Logger.debug('Broadcast:', e.data)
switch (e.data.type) {
const type = e.data.type;
const data = e.data.data;
Logger.debug('Broadcast:', type, data);
switch (type) {
case 'self-display-name-changed':
Events.fire('self-display-name-changed', e.data.detail);
Events.fire('self-display-name-changed', data.displayName);
break;
}
}

View file

@ -550,7 +550,7 @@ class Peer {
}
Events.fire('peer-display-name-changed', {peerId: this._peerId, displayName: message.displayName});
Events.fire('notify-peer-display-name-changed', this._peerId);
Events.fire('notify-display-name-changed', { recipient: this._peerId });
}
_sendState() {
@ -1487,13 +1487,16 @@ class WSPeer extends Peer {
class PeersManager {
constructor(serverConnection) {
this.peers = {};
this._server = serverConnection;
this.peers = {};
this._device = {
originalDisplayName: '',
displayName: '',
publicRoomId: null
};
Events.on('signal', e => this._onSignal(e.detail));
Events.on('peers', e => this._onPeers(e.detail));
Events.on('files-selected', e => this._onFilesSelected(e.detail));
Events.on('respond-to-files-transfer-request', e => this._onRespondToFileTransferRequest(e.detail))
Events.on('send-text', e => this._onSendText(e.detail));
Events.on('peer-left', e => this._onPeerLeft(e.detail));
Events.on('peer-joined', e => this._onPeerJoined(e.detail));
Events.on('peer-connected', e => this._onPeerConnected(e.detail.peerId));
@ -1505,16 +1508,25 @@ class PeersManager {
// peer closes connection
Events.on('secret-room-deleted', e => this._onSecretRoomDeleted(e.detail));
Events.on('room-secret-regenerated', e => this._onRoomSecretRegenerated(e.detail));
// peer
Events.on('display-name', e => this._onDisplayName(e.detail.displayName));
Events.on('self-display-name-changed', e => this._notifyPeersDisplayNameChanged(e.detail));
Events.on('notify-peer-display-name-changed', e => this._notifyPeerDisplayNameChanged(e.detail));
Events.on('self-display-name-changed', e => this._notifyPeersDisplayNameChanged(e.detail.displayName));
Events.on('notify-display-name-changed', e => this._notifyPeerDisplayNameChanged(e.detail.recipient));
Events.on('auto-accept-updated', e => this._onAutoAcceptUpdated(e.detail.roomSecret, e.detail.autoAccept));
// transfer
Events.on('send-text', e => this._onSendText(e.detail));
Events.on('files-selected', e => this._onFilesSelected(e.detail));
Events.on('respond-to-files-transfer-request', e => this._onRespondToFileTransferRequest(e.detail))
// websocket connection
Events.on('ws-disconnected', _ => this._onWsDisconnected());
Events.on('ws-relay', e => this._onWsRelay(e.detail.peerId, e.detail.message));
Events.on('ws-config', e => this._onWsConfig(e.detail));
// no-sleep
Events.on('evaluate-no-sleep', _ => this._onEvaluateNoSleep());
}
@ -1669,14 +1681,23 @@ class PeersManager {
}
}
_onLeavePublicRoom(publicRoomId) {
this._disconnectOrRemoveRoomTypeByRoomId('public-id', publicRoomId);
_onJoinPublicRoom(roomId) {
this._device.publicRoomId = roomId;
}
_onLeavePublicRoom() {
this._disconnectFromPublicRoom();
}
_onSecretRoomDeleted(roomSecret) {
this._disconnectOrRemoveRoomTypeByRoomId('secret', roomSecret);
}
_disconnectFromPublicRoom() {
this._disconnectOrRemoveRoomTypeByRoomId('public-id', this._device.publicRoomId);
this._device.publicRoomId = null;
}
_disconnectOrRemoveRoomTypeByRoomId(roomType, roomId) {
const peerIds = this._getPeerIdsFromRoomId(roomId);
@ -1690,7 +1711,7 @@ class PeersManager {
_disconnectOrRemoveRoomTypeByPeerId(peerId, roomType) {
const peer = this.peers[peerId];
if (!peer) return;
if (!peer || !peer._getRoomTypes().includes(roomType)) return;
if (peer._getRoomTypes().length > 1) {
peer._removeRoomType(roomType);
@ -1710,7 +1731,10 @@ class PeersManager {
}
_notifyPeersDisplayNameChanged(newDisplayName) {
this._displayName = newDisplayName ? newDisplayName : this._originalDisplayName;
this._device.displayName = newDisplayName
? newDisplayName
: this._device.originalDisplayName;
for (const peerId in this.peers) {
this._notifyPeerDisplayNameChanged(peerId);
}
@ -1719,23 +1743,35 @@ class PeersManager {
_notifyPeerDisplayNameChanged(peerId) {
const peer = this.peers[peerId];
if (!peer) return;
this.peers[peerId]._sendDisplayName(this._displayName);
this.peers[peerId]._sendDisplayName(this._device.displayName);
}
_onDisplayName(displayName) {
this._originalDisplayName = displayName;
this._device.originalDisplayName = displayName;
// if the displayName has not been changed (yet) set the displayName to the original displayName
if (!this._displayName) this._displayName = displayName;
if (!this._device.displayName) this._device.displayName = displayName;
}
_onAutoAcceptUpdated(roomSecret, autoAccept) {
const peerId = this._getPeerIdsFromRoomId(roomSecret)[0];
let peerIds = this._getPeerIdsFromRoomId(roomSecret);
const peerId = this._removePeerIdsSameBrowser(peerIds)[0];
if (!peerId) return;
this.peers[peerId]._setAutoAccept(autoAccept);
}
_removePeerIdsSameBrowser(peerIds) {
let peerIdsNotSameBrowser = [];
for (let i = 0; i < peerIds.length; i++) {
const peer = this.peers[peerIds[i]];
if (!peer._isSameBrowser()) {
peerIdsNotSameBrowser.push(peerIds[i]);
}
}
return peerIdsNotSameBrowser;
}
_getPeerIdsFromRoomId(roomId) {
if (!roomId) return [];

View file

@ -205,7 +205,7 @@ class FooterUI {
this.$displayName.addEventListener('blur', e => this._saveDisplayName(e.target.innerText));
Events.on('display-name', e => this._onDisplayName(e.detail.displayName));
Events.on('self-display-name-changed', e => this._insertDisplayName(e.detail));
Events.on('self-display-name-changed', e => this._insertDisplayName(e.detail.displayName));
// Load saved display name on page load
Events.on('ws-connected', _ => this._loadSavedDisplayName());
@ -239,7 +239,7 @@ class FooterUI {
if (!displayName) return;
Logger.debug("Retrieved edited display name:", displayName)
Events.fire('self-display-name-changed', displayName);
Events.fire('self-display-name-changed', { displayName: displayName });
}
_onDisplayName(displayName){
@ -280,8 +280,8 @@ class FooterUI {
Events.fire('notify-user', Localization.getTranslation("notifications.display-name-changed-temporarily"));
})
.finally(() => {
Events.fire('self-display-name-changed', newDisplayName);
Events.fire('broadcast-send', {type: 'self-display-name-changed', detail: newDisplayName});
Events.fire('self-display-name-changed', { displayName: newDisplayName });
Events.fire('broadcast-self-display-name-changed', { displayName: newDisplayName });
});
}
else {
@ -292,8 +292,8 @@ class FooterUI {
})
.finally(() => {
Events.fire('notify-user', Localization.getTranslation("notifications.display-name-random-again"));
Events.fire('self-display-name-changed', '');
Events.fire('broadcast-send', {type: 'self-display-name-changed', detail: ''});
Events.fire('self-display-name-changed', { displayName: '' });
Events.fire('broadcast-self-display-name-changed', { displayName: '' });
});
}
}