diff --git a/public/scripts/network.js b/public/scripts/network.js index 0674e12..55bce53 100644 --- a/public/scripts/network.js +++ b/public/scripts/network.js @@ -25,10 +25,10 @@ class ServerConnection { Events.on('online', _ => this._connect()); } - _connect() { + async _connect() { clearTimeout(this._reconnectTimer); if (this._isConnected() || this._isConnecting()) return; - const ws = new WebSocket(this._endpoint()); + const ws = new WebSocket(await this._endpoint()); ws.binaryType = 'arraybuffer'; ws.onopen = _ => this._onOpen(); ws.onmessage = e => this._onMessage(e.data); @@ -114,29 +114,32 @@ class ServerConnection { _onDisplayName(msg) { sessionStorage.setItem("peerId", msg.message.peerId); - if (window.matchMedia('(display-mode: minimal-ui)').matches) { - // make peerId persistent when pwa installed - PersistentStorage.set('peerId', msg.message.peerId).then(peerId => { - console.log(`peerId saved to indexedDB: ${peerId}`); - }).catch(_ => _ => PersistentStorage.logBrowserNotCapable()); - } + PersistentStorage.get('peerId').then(peerId => { + if (!peerId) { + // save peerId to indexedDB to retrieve after PWA is installed + PersistentStorage.set('peerId', msg.message.peerId).then(peerId => { + console.log(`peerId saved to indexedDB: ${peerId}`); + }); + } + }).catch(_ => _ => PersistentStorage.logBrowserNotCapable()) Events.fire('display-name', msg); } - _endpoint() { + async _endpoint() { // hack to detect if deployment or development environment const protocol = location.protocol.startsWith('https') ? 'wss' : 'ws'; const webrtc = window.isRtcSupported ? '/webrtc' : '/fallback'; let ws_url = new URL(protocol + '://' + location.host + location.pathname + 'server' + webrtc); - const peerId = this._peerId(); - if (peerId) { - ws_url.searchParams.append('peer_id', peerId) - } + const peerId = await this._peerId(); + if (peerId) ws_url.searchParams.append('peer_id', peerId) return ws_url.toString(); } - _peerId() { - return sessionStorage.getItem("peerId"); + async _peerId() { + // make peerId persistent when pwa is installed + return window.matchMedia('(display-mode: minimal-ui)').matches + ? await PersistentStorage.get('peerId') + : sessionStorage.getItem("peerId"); } _disconnect() { diff --git a/public/scripts/ui.js b/public/scripts/ui.js index 1b105da..558affa 100644 --- a/public/scripts/ui.js +++ b/public/scripts/ui.js @@ -1376,7 +1376,7 @@ class PersistentStorage { const objectStoreRequest = objectStore.put(value, key); objectStoreRequest.onsuccess = _ => { console.log(`Request successful. Added key-pair: ${key} - ${value}`); - resolve(); + resolve(value); }; } DBOpenRequest.onerror = (e) => { @@ -1551,20 +1551,13 @@ if ('serviceWorker' in navigator) { } window.addEventListener('beforeinstallprompt', e => { - if (window.matchMedia('(display-mode: standalone)').matches) { - // make peerId persistent when pwa installed - PersistentStorage.get('peerId').then(peerId => { - sessionStorage.setItem("peerId", peerId); - }).catch(e => console.error(e)); - - // don't display install banner when installed - return e.preventDefault(); - } else { + if (!window.matchMedia('(display-mode: minimal-ui)').matches) { + // only display install btn when installed const btn = document.querySelector('#install') btn.hidden = false; btn.onclick = _ => e.prompt(); - return e.preventDefault(); } + return e.preventDefault(); }); // Background Animation diff --git a/public/service-worker.js b/public/service-worker.js index 23c8b4b..3870f54 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -1,4 +1,4 @@ -var CACHE_NAME = 'pairdrop-cache-v3'; +var CACHE_NAME = 'pairdrop-cache-v4'; var urlsToCache = [ 'index.html', './', @@ -16,13 +16,13 @@ var urlsToCache = [ self.addEventListener('install', function(event) { // Perform install steps - event.waitUntil( - caches.open(CACHE_NAME) - .then(function(cache) { - console.log('Opened cache'); - return cache.addAll(urlsToCache); - }) - ); + event.waitUntil( + caches.open(CACHE_NAME) + .then(function(cache) { + console.log('Opened cache'); + return cache.addAll(urlsToCache); + }) + ); });