diff --git a/src/components/bc-button.ts b/src/components/bc-button.ts index 6c654e3..97cf8c1 100644 --- a/src/components/bc-button.ts +++ b/src/components/bc-button.ts @@ -11,7 +11,7 @@ import store from '../state/store.js'; import {waitingIcon} from './icons/waitingIcon.js'; /** - * A button that when clicked launches the modal. + * A button that launches the Bitcoin Connect modal. */ @customElement('bc-button') export class Button extends withTwind()(BitcoinConnectElement) { @@ -19,57 +19,78 @@ export class Button extends withTwind()(BitcoinConnectElement) { override title = 'Connect Wallet'; @state() - protected _showBalance: boolean | undefined = undefined; + protected _showBalance = false; + + private _unsubscribe?: () => void; constructor() { super(); - this._showBalance = - store.getState().bitcoinConnectConfig.showBalance && - store.getState().supports('getBalance'); + this._updateShowBalance(); - // TODO: handle unsubscribe - store.subscribe((store) => { + this._unsubscribe = store.subscribe((state) => { this._showBalance = - store.bitcoinConnectConfig.showBalance && store.supports('getBalance'); + state.bitcoinConnectConfig.showBalance && state.supports('getBalance'); }); } + disconnectedCallback() { + super.disconnectedCallback(); + this._unsubscribe?.(); + this._unsubscribe = undefined; + } + + private _updateShowBalance() { + const state = store.getState(); + this._showBalance = + state.bitcoinConnectConfig.showBalance && state.supports('getBalance'); + } + override render() { const isLoading = this._connecting || (!this._connected && this._modalOpen); - return html`