Skip to content

Commit d58e391

Browse files
committed
mobile: ConnectServer: spin off blocking RPCConnection call into goroutine
1 parent b66c421 commit d58e391

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

mobile/mobile.go

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -181,51 +181,56 @@ func ConnectServer(nameSpace string, mailboxServer string, isDevServer bool,
181181
return fmt.Errorf("unknown namespace: %s", nameSpace)
182182
}
183183

184-
statusChecker, lndConnect, err := core.MailboxRPCConnection(
185-
mailboxServer, pairingPhrase, localPriv, remotePub,
186-
func(key *btcec.PublicKey) error {
187-
mc.remoteKeyReceiveCallback.SendResult(
188-
hex.EncodeToString(key.SerializeCompressed()),
189-
)
190-
191-
return nil
192-
}, func(data []byte) error {
193-
parts := strings.Split(string(data), ": ")
194-
if len(parts) != 2 || parts[0] != "Macaroon" {
195-
return fmt.Errorf("authdata does " +
196-
"not contain a macaroon")
197-
}
198-
199-
macBytes, err := hex.DecodeString(parts[1])
200-
if err != nil {
201-
return err
202-
}
203-
204-
mac := &macaroon.Macaroon{}
205-
err = mac.UnmarshalBinary(macBytes)
206-
if err != nil {
207-
return fmt.Errorf("unable to decode "+
208-
"macaroon: %v", err)
209-
}
210-
211-
mc.mac = mac
212-
213-
mc.authDataCallback.SendResult(string(data))
184+
// Since the connection function is blocking, we need to spin it off
185+
// in another goroutine here. See https://pkg.go.dev/syscall/js#FuncOf.
186+
go func() {
187+
statusChecker, lndConnect, err := core.MailboxRPCConnection(
188+
mailboxServer, pairingPhrase, localPriv, remotePub,
189+
func(key *btcec.PublicKey) error {
190+
mc.remoteKeyReceiveCallback.SendResult(
191+
hex.EncodeToString(key.SerializeCompressed()),
192+
)
193+
194+
return nil
195+
}, func(data []byte) error {
196+
parts := strings.Split(string(data), ": ")
197+
if len(parts) != 2 || parts[0] != "Macaroon" {
198+
return fmt.Errorf("authdata does " +
199+
"not contain a macaroon")
200+
}
201+
202+
macBytes, err := hex.DecodeString(parts[1])
203+
if err != nil {
204+
return err
205+
}
206+
207+
mac := &macaroon.Macaroon{}
208+
err = mac.UnmarshalBinary(macBytes)
209+
if err != nil {
210+
return fmt.Errorf("unable to decode "+
211+
"macaroon: %v", err)
212+
}
213+
214+
mc.mac = mac
215+
216+
mc.authDataCallback.SendResult(string(data))
217+
218+
return nil
219+
},
220+
)
221+
if err != nil {
222+
log.Errorf("Error running wasm client: %v", err)
223+
}
214224

215-
return nil
216-
},
217-
)
218-
if err != nil {
219-
return err
220-
}
225+
mc.statusChecker = statusChecker
226+
mc.lndConn, err = lndConnect()
227+
if err != nil {
228+
log.Errorf("Error running wasm client: %v", err)
229+
}
221230

222-
mc.statusChecker = statusChecker
223-
mc.lndConn, err = lndConnect()
224-
if err != nil {
225-
return err
226-
}
231+
log.Debugf("Mobile client connected to RPC")
232+
}()
227233

228-
log.Debugf("Mobile client connected to RPC")
229234
return nil
230235
}
231236

0 commit comments

Comments
 (0)