diff --git a/wasm/cmix.go b/wasm/cmix.go index c33cbb98c3ccab1ef4432e8739ee12ae4aa1cf03..909a8b29d20105f4535205793496fbbf1f9ab14c 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 {