From 255bd4e3ef1db4bd05deac8d08a4da428a83412a Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Tue, 30 May 2023 21:28:49 +0000
Subject: [PATCH] Add await calls to remote store wrapper

---
 go.mod             |  2 +-
 go.sum             |  2 ++
 wasm/collective.go | 27 +++++++++++++--------------
 3 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/go.mod b/go.mod
index 254f8d4a..493b6769 100644
--- a/go.mod
+++ b/go.mod
@@ -9,7 +9,7 @@ require (
 	github.com/spf13/cobra v1.7.0
 	github.com/spf13/jwalterweatherman v1.1.0
 	github.com/stretchr/testify v1.8.2
-	gitlab.com/elixxir/client/v4 v4.6.4-0.20230530201605-ba087da90ba2
+	gitlab.com/elixxir/client/v4 v4.6.4-0.20230530211720-a1bc89986df8
 	gitlab.com/elixxir/crypto v0.0.7-0.20230522162218-45433d877235
 	gitlab.com/elixxir/primitives v0.0.3-0.20230214180039-9a25e2d3969c
 	gitlab.com/elixxir/wasm-utils v0.0.0-20230522231408-a43b2c1481b2
diff --git a/go.sum b/go.sum
index 17c6347b..163df083 100644
--- a/go.sum
+++ b/go.sum
@@ -525,6 +525,8 @@ gitlab.com/elixxir/client/v4 v4.6.4-0.20230530182422-d12ab437257f h1:8TJwqKjeD0e
 gitlab.com/elixxir/client/v4 v4.6.4-0.20230530182422-d12ab437257f/go.mod h1:fegbuF1/6a+H3QgsoMG8teLnyuKtDxkELMw8pn5WlZ8=
 gitlab.com/elixxir/client/v4 v4.6.4-0.20230530201605-ba087da90ba2 h1:sxAgLmG2RLarBuXtcH8g5ZcQbAF9PmS3wrnlwZVFrLA=
 gitlab.com/elixxir/client/v4 v4.6.4-0.20230530201605-ba087da90ba2/go.mod h1:fegbuF1/6a+H3QgsoMG8teLnyuKtDxkELMw8pn5WlZ8=
+gitlab.com/elixxir/client/v4 v4.6.4-0.20230530211720-a1bc89986df8 h1:XTklpI9leJYgZ4bYZLQUOzzgoxlYUz7xDmcl2Fy7knc=
+gitlab.com/elixxir/client/v4 v4.6.4-0.20230530211720-a1bc89986df8/go.mod h1:fegbuF1/6a+H3QgsoMG8teLnyuKtDxkELMw8pn5WlZ8=
 gitlab.com/elixxir/comms v0.0.4-0.20230519211512-4a998f4b0938 h1:f27+QUFiGWrprKm+fstOg3ABkYLpWcZi3+8Lf5eDnqY=
 gitlab.com/elixxir/comms v0.0.4-0.20230519211512-4a998f4b0938/go.mod h1:z+qW0D9VpY5QKTd7wRlb5SK4kBNqLYsa4DXBcUXue9Q=
 gitlab.com/elixxir/crypto v0.0.7-0.20230522162218-45433d877235 h1:0BySdXTzRWxzH8k5RiNNMmmn2lpuQWLVcDDA/7ehyqc=
diff --git a/wasm/collective.go b/wasm/collective.go
index e99f03ae..c2b0b3fe 100644
--- a/wasm/collective.go
+++ b/wasm/collective.go
@@ -473,13 +473,11 @@ func newRemoteStore(arg js.Value) *RemoteStore {
 //   - The file data (Uint8Array).
 //   - Catches any thrown errors (of type Error) and returns it as an error.
 func (rsCB *RemoteStore) Read(path string) ([]byte, error) {
-
-	fn := func() js.Value { return rsCB.read(path) }
-	v, err := exception.RunAndCatch(fn)
-	if err != nil {
-		return nil, err
+	v, awaitErr := utils.Await(rsCB.read(path))
+	if awaitErr != nil {
+		return nil, js.Error{Value: awaitErr[0]}
 	}
-	return utils.CopyBytesToGo(v), err
+	return utils.CopyBytesToGo(v[0]), nil
 }
 
 // Write implements [bindings.RemoteStore.Write]
@@ -491,9 +489,11 @@ func (rsCB *RemoteStore) Read(path string) ([]byte, error) {
 // Returns:
 //   - Catches any thrown errors (of type Error) and returns it as an error.
 func (rsCB *RemoteStore) Write(path string, data []byte) error {
-	fn := func() js.Value { return rsCB.write(path, utils.CopyBytesToJS(data)) }
-	_, err := exception.RunAndCatch(fn)
-	return err
+	_, awaitErr := utils.Await(rsCB.write(path, utils.CopyBytesToJS(data)))
+	if awaitErr != nil {
+		return js.Error{Value: awaitErr[0]}
+	}
+	return nil
 }
 
 // GetLastModified implements [bindings.RemoteStore.GetLastModified]
@@ -536,12 +536,11 @@ func (rsCB *RemoteStore) GetLastWrite() ([]byte, error) {
 //   - JSON of []string (Uint8Array).
 //   - Catches any thrown errors (of type Error) and returns it as an error.
 func (rsCB *RemoteStore) ReadDir(path string) ([]byte, error) {
-	fn := func() js.Value { return rsCB.readDir(path) }
-	v, err := exception.RunAndCatch(fn)
-	if err != nil {
-		return nil, err
+	v, awaitErr := utils.Await(rsCB.readDir(path))
+	if awaitErr != nil {
+		return nil, js.Error{Value: awaitErr[0]}
 	}
-	return utils.CopyBytesToGo(v), err
+	return utils.CopyBytesToGo(v[0]), nil
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-- 
GitLab