fuzzyworld

dao-game / commit

Waiting for the connections to actually open before showing the UI.

c1cd0c65e18f1150f77b30e602f2541ab7036c0a · Pavel Safronov · 2022-03-22 14:26:49 -0700 · parents 7ea1cc21c41f

Waiting for the connections to actually open before showing the UI.
 src/game/remote-game.ts | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/src/game/remote-game.ts b/src/game/remote-game.ts
index 2a377e4..0549c28 100644
--- a/src/game/remote-game.ts
+++ b/src/game/remote-game.ts
@@ -88,14 +88,13 @@ export class PeerGame extends LocalGame {
         const main = document.getElementsByTagName('main')[0];
         let game: PeerGame = undefined;
 
-        const handleConn = (conn: Peer.DataConnection) => {
-            if (game !== undefined) {
-                conn.close();
-                return;
-            }
-
+        const handleConn = async (conn: Peer.DataConnection) => {
             shareContainer.classList.add('invisible');
 
+            await new Promise<void>((resolve) => {
+                conn.on('open', () => resolve());
+            });
+
             id = peer.id;
             peerId = conn.peer;
             setData({ id, peerId });
@@ -166,9 +165,6 @@ export class PeerGame extends LocalGame {
             conn.on('close', () => {
                 showMessageAndLeave(`Connection closed`, 'Connection closed for some reason')
             });
-            // conn.on('open', () => {
-            //     showModal({ title: 'conn-on-open', text: 'connected!' });
-            // });
         };
 
         const { host, protocol, pathname, search } = location;