lightninglabs / lnc-web

Lightning Node Connect npm module for web
https://docs.lightning.engineering/lightning-network-tools/lightning-terminal/lnc-npm
48 stars 17 forks source link

Make disconnect async #110

Open huumn opened 7 months ago

huumn commented 7 months ago

wasmClientDisconnect takes a function that's called when the disconnect has concluded, but lnc-web doesn't use it.

As a result, consumers end up needing to do something like this:

          lnc.disconnect()
          logger.info('disconnecting after:', `payment_hash=${hash}`)
          // wait for lnc to disconnect before releasing the mutex
          await new Promise((resolve, reject) => {
            let counter = 0
            const interval = setInterval(() => {
              if (lnc.isConnected) {
                if (counter++ > 100) {
                  logger.error('failed to disconnect from lnc')
                  clearInterval(interval)
                  reject(new Error('failed to disconnect from lnc'))
                }
                return
              }
              clearInterval(interval)
              resolve()
            })
          }, 50)

Happy to create a PR myself to the effect of

async disconnect() {
        return await new Promise(resolve => this.wasm.wasmClientDisconnect(resolve);
}

... if it makes sense