From 69b0cc3fda9df153eddd8e7538a33668be7a854a Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Wed, 31 May 2023 16:59:03 +0000
Subject: [PATCH] Add a guard, don't allow loads when New is running

---
 wasm/cmix.go | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/wasm/cmix.go b/wasm/cmix.go
index c33cbb98..909a8b29 100644
--- a/wasm/cmix.go
+++ b/wasm/cmix.go
@@ -10,6 +10,8 @@
 package wasm
 
 import (
+	"fmt"
+	"sync/atomic"
 	"syscall/js"
 
 	"gitlab.com/elixxir/client/v4/bindings"
@@ -17,6 +19,8 @@ import (
 	"gitlab.com/elixxir/wasm-utils/utils"
 )
 
+var initializing atomic.Bool
+
 // Cmix wraps the [bindings.Cmix] object so its methods can be wrapped to be
 // Javascript compatible.
 type Cmix struct {
@@ -124,6 +128,7 @@ func NewCmix(_ js.Value, args []js.Value) any {
 //   - Resolves on success.
 //   - Rejected with an error if creating a new cMix client fails.
 func NewSynchronizedCmix(_ js.Value, args []js.Value) any {
+	initializing.Store(true)
 	ndfJSON := args[0].String()
 	storageDir := args[1].String()
 	password := utils.CopyBytesToGo(args[2])
@@ -135,6 +140,7 @@ func NewSynchronizedCmix(_ js.Value, args []js.Value) any {
 		if err != nil {
 			reject(exception.NewTrace(err))
 		} else {
+			initializing.Store(false)
 			resolve()
 		}
 	}
@@ -196,6 +202,10 @@ func LoadSynchronizedCmix(_ js.Value, args []js.Value) any {
 	cmixParamsJSON := utils.CopyBytesToGo(args[3])
 
 	promiseFn := func(resolve, reject func(args ...any) js.Value) {
+		if initializing.Load() {
+			reject(exception.NewTrace(fmt.Errorf(
+				"cannot Load when New is running")))
+		}
 		net, err := bindings.LoadSynchronizedCmix(storageDir, password,
 			rs, cmixParamsJSON)
 		if err != nil {
-- 
GitLab