From 13f36941cc4b128fd4b91628e0a57393b51de1e4 Mon Sep 17 00:00:00 2001
From: Moritz Langenstein <ml5717@ic.ac.uk>
Date: Mon, 6 Jan 2020 09:18:52 +0000
Subject: [PATCH] (ml5717) Fixed wasm-crdt room connection await

---
 src/app.js       | 15 +++++++++++----
 src/wasm-crdt.js | 20 +++++++++++++-------
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/src/app.js b/src/app.js
index 42e06c0..329e7a0 100644
--- a/src/app.js
+++ b/src/app.js
@@ -576,10 +576,17 @@ canvas.input.addEventListener("strokemove", ({ detail: e }) => {
   switch (currentTool) {
     case toolSelection.Tools.PEN: {
       const pathID = pathIDsByPointerID.get(e.pointerId)
-      room.extendPath(
-        pathID,
-        selectedColorAndRadiusPoint(...mousePos, pressure),
-      )
+      if (pathID) {
+        room.extendPath(
+          pathID,
+          selectedColorAndRadiusPoint(...mousePos, pressure),
+        )
+      } else {
+        pathIDsByPointerID.set(
+          e.pointerId,
+          room.addPath(selectedColorAndRadiusPoint(...mousePos, pressure)),
+        )
+      }
       if (toolSelection.isRecognitionModeSet()) {
         drawRecognizedUpcoming(room.getPathPoints(pathID), pressure)
       }
diff --git a/src/wasm-crdt.js b/src/wasm-crdt.js
index 4709f54..eb873ce 100644
--- a/src/wasm-crdt.js
+++ b/src/wasm-crdt.js
@@ -14,9 +14,10 @@ Array.prototype.remove = function(elem) {
 }
 
 export default class WasmCRDTWrapper {
-  constructor(WasmCRDT, room, interval) {
+  constructor(WasmCRDT, room, interval, resolve) {
     this.room = room
     this.mesh = null
+    this.resolve = resolve
 
     this.users = {
       synced: [],
@@ -84,12 +85,16 @@ export default class WasmCRDTWrapper {
   static async initialise(room, options) {
     const { WasmCRDT } = await WasmCRDTAsync
 
-    room.crdt = new WasmCRDTWrapper(
-      WasmCRDT,
-      room,
-      (options.wasm && options.wasm.interval) || 0,
-    )
-    room.crdt.mesh = new P2PMesh(room.crdt, options)
+    await new Promise((resolve) => {
+      room.crdt = new WasmCRDTWrapper(
+        WasmCRDT,
+        room,
+        (options.wasm && options.wasm.interval) || 0,
+        resolve,
+      )
+
+      room.crdt.mesh = new P2PMesh(room.crdt, options)
+    })
   }
 
   getUserID() {
@@ -100,6 +105,7 @@ export default class WasmCRDTWrapper {
     const success = this.crdt.set_user(uid)
 
     if (success) {
+      this.resolve()
       this.room.dispatchEvent(new CustomEvent("allocateOwnID", { detail: uid }))
     }
 
-- 
GitLab