From 69e5a3a2fa92dff627c565a88bcfc3d3e19df1a5 Mon Sep 17 00:00:00 2001
From: Greg Fodor <gfodor@gmail.com>
Date: Sun, 30 Sep 2018 05:38:57 +0000
Subject: [PATCH] Refactor

---
 src/components/gltf-model-plus.js |  7 +---
 src/hub.js                        | 58 +++++++++++--------------------
 src/utils/next-tick.js            |  5 +++
 3 files changed, 27 insertions(+), 43 deletions(-)
 create mode 100644 src/utils/next-tick.js

diff --git a/src/components/gltf-model-plus.js b/src/components/gltf-model-plus.js
index e31e43dae..a753820a9 100644
--- a/src/components/gltf-model-plus.js
+++ b/src/components/gltf-model-plus.js
@@ -1,3 +1,4 @@
+import nextTick from "../utils/next-tick";
 import SketchfabZipWorker from "../workers/sketchfab-zip.worker.js";
 import cubeMapPosX from "../assets/images/cubemap/posx.jpg";
 import cubeMapNegX from "../assets/images/cubemap/negx.jpg";
@@ -190,12 +191,6 @@ function attachTemplate(root, name, templateRoot) {
   }
 }
 
-function nextTick() {
-  return new Promise(resolve => {
-    setTimeout(resolve, 0);
-  });
-}
-
 function getFilesFromSketchfabZip(src) {
   return new Promise((resolve, reject) => {
     const worker = new SketchfabZipWorker();
diff --git a/src/hub.js b/src/hub.js
index df3083d3e..94fd4aeb1 100644
--- a/src/hub.js
+++ b/src/hub.js
@@ -20,6 +20,7 @@ import "aframe-motion-capture-components";
 import "./utils/audio-context-fix";
 import { getReticulumFetchUrl } from "./utils/phoenix-utils";
 
+import nextTick from "./utils/next-tick";
 import trackpad_dpad4 from "./behaviours/trackpad-dpad4";
 import trackpad_scrolling from "./behaviours/trackpad-scrolling";
 import joystick_dpad4 from "./behaviours/joystick-dpad4";
@@ -264,47 +265,30 @@ async function handleHubChannelJoined(entryManager, data) {
     entryManager.enterSceneWhenLoaded(new MediaStream(), false);
   }
 
-  const sendHubDataMessage = function(clientId, dataType, data, reliable) {
-    const event = "naf";
-    const payload = { dataType, data };
+  while (!scene.components["networked-scene"] || !scene.components["networked-scene"].data) await nextTick();
 
-    if (clientId != null) {
-      payload.clientId = clientId;
-    }
-
-    if (reliable) {
-      hubChannel.channel.push(event, payload);
-    } else {
-      const topic = hubChannel.channel.topic;
-      const join_ref = hubChannel.channel.joinRef();
-      hubChannel.channel.socket.push({ topic, event, payload, join_ref, ref: null }, false);
-    }
-  };
-
-  const connectWhenNetworkedSceneReady = () => {
-    if (!scene.components["networked-scene"] || !scene.components["networked-scene"].data) {
-      setTimeout(connectWhenNetworkedSceneReady, 0);
-      return;
-    }
+  scene.components["networked-scene"]
+    .connect()
+    .then(() => {
+      NAF.connection.adapter.reliableTransport = (clientId, dataType, data) => {
+        const payload = { dataType, data };
 
-    scene.components["networked-scene"]
-      .connect()
-      .then(() => {
-        NAF.connection.adapter.reliableTransport = (clientId, dataType, data) =>
-          sendHubDataMessage(clientId, dataType, data, true);
-      })
-      .catch(connectError => {
-        // hacky until we get return codes
-        const isFull = connectError.error && connectError.error.msg.match(/\bfull\b/i);
-        console.error(connectError);
-        remountUI({ roomUnavailableReason: isFull ? "full" : "connect_error" });
-        entryManager.exitScene();
+        if (clientId) {
+          payload.clientId = clientId;
+        }
 
-        return;
-      });
-  };
+        hubChannel.channel.push("naf", payload);
+      };
+    })
+    .catch(connectError => {
+      // hacky until we get return codes
+      const isFull = connectError.error && connectError.error.msg.match(/\bfull\b/i);
+      console.error(connectError);
+      remountUI({ roomUnavailableReason: isFull ? "full" : "connect_error" });
+      entryManager.exitScene();
 
-  connectWhenNetworkedSceneReady();
+      return;
+    });
 }
 
 document.addEventListener("DOMContentLoaded", () => {
diff --git a/src/utils/next-tick.js b/src/utils/next-tick.js
new file mode 100644
index 000000000..85f66b712
--- /dev/null
+++ b/src/utils/next-tick.js
@@ -0,0 +1,5 @@
+export default function nextTick() {
+  return new Promise(resolve => {
+    setTimeout(resolve, 0);
+  });
+}
-- 
GitLab