diff --git a/cmd/wasm-client/log.go b/cmd/wasm-client/log.go index 1b4f31a..f699750 100644 --- a/cmd/wasm-client/log.go +++ b/cmd/wasm-client/log.go @@ -4,6 +4,7 @@ package main import ( "github.com/btcsuite/btclog" + "github.com/lightninglabs/lightning-node-connect/gbn" "github.com/lightninglabs/lightning-node-connect/mailbox" "github.com/lightningnetwork/lnd" "github.com/lightningnetwork/lnd/build" @@ -25,6 +26,7 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) { lnd.SetSubLogger(root, Subsystem, log) lnd.AddSubLogger(root, mailbox.Subsystem, intercept, mailbox.UseLogger) + lnd.AddSubLogger(root, gbn.Subsystem, intercept, gbn.UseLogger) grpclog.SetLoggerV2(NewGrpcLogLogger(root, intercept, "GRPC")) } diff --git a/cmd/wasm-client/main.go b/cmd/wasm-client/main.go index c811180..7d71363 100644 --- a/cmd/wasm-client/main.go +++ b/cmd/wasm-client/main.go @@ -288,12 +288,29 @@ func (w *wasmClient) IsConnected(_ js.Value, _ []js.Value) interface{} { return js.ValueOf(w.lndConn != nil) } -func (w *wasmClient) Disconnect(_ js.Value, _ []js.Value) interface{} { +// Disconnect disconnects the client, and closes the connection. +// The first argument passed should be a onDisconnect callback, which will be +// invoked once the client has disconnected. +func (w *wasmClient) Disconnect(_ js.Value, args []js.Value) interface{} { if w.lndConn != nil { - if err := w.lndConn.Close(); err != nil { - log.Errorf("Error closing RPC connection: %v", err) - } - w.lndConn = nil + // We launch the closure of the connection in a goroutine to + // avoid that the JS websocket freezes and blocks while closing. + go func() { + if err := w.lndConn.Close(); err != nil { + log.Errorf("Error closing RPC connection: %v", + err) + } + w.lndConn = nil + + // We expect the first arg to be the onDisconnect + // callback + if len(args) > 0 && args[0].Type() == js.TypeFunction { + callback := args[0] + + // Call the onDisconnect callback. + callback.Invoke() + } + }() } return nil diff --git a/example/index.html b/example/index.html index 10f3f6f..242fef4 100644 --- a/example/index.html +++ b/example/index.html @@ -98,8 +98,10 @@ } async function disconnect() { - window[namespace].wasmClientDisconnect(); + window[namespace].wasmClientDisconnect(onDisconnect); + } + function onDisconnect() { document.getElementById('disconnectBtn').disabled = true; document.getElementById('reconnectBtn').disabled = false; document.getElementById('ready').style.display= 'none' ;