Skip to content

Commit

Permalink
fix: circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Aug 25, 2023
1 parent 0df267b commit e2ad6c9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@web/dev-server-esbuild": "^0.4.1",
"@web/test-runner": "^0.15.0",
"@web/test-runner-playwright": "^0.9.0",
"@webbtc/webln-types": "^1.0.12",
"@webbtc/webln-types": "^2.0.0",
"@webcomponents/webcomponentsjs": "^2.6.0",
"concurrently": "^8.2.0",
"esbuild": "^0.18.17",
Expand Down
22 changes: 9 additions & 13 deletions src/connectors/Connector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import store from '../state/store';
import {ConnectorConfig} from '../types/ConnectorConfig';

export abstract class Connector {
Expand All @@ -13,34 +12,31 @@ export abstract class Connector {
throw new Error('window.webln does not exist');
}
await window.webln.enable();
await this.loadAlias();
await this.loadBalance();
}

async loadBalance() {
async getBalance() {
try {
// FIXME: typings
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const balanceResponse = await (window.webln as any).getBalance();
if (balanceResponse.balance !== undefined) {
store.getState().setBalance(balanceResponse.balance);
if (!window.webln) {
throw new Error('webln not found');
}
const balanceResponse = await window.webln.getBalance?.();
return balanceResponse?.balance;
} catch (error) {
console.error('Failed to get balance', error);
}
return undefined;
}

async loadAlias() {
async getAlias() {
try {
if (!window.webln) {
throw new Error('webln not found');
}
const info = await window.webln.getInfo();
if (info.node.alias) {
store.getState().setAlias(info.node.alias);
}
return info.node.alias;
} catch (error) {
console.error('Failed to get alias', error);
return undefined;
}
}
}
6 changes: 5 additions & 1 deletion src/state/store.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {createStore} from 'zustand/vanilla';
import {Connector} from '../connectors/Connector';
import {ConnectorConfig} from '../types/ConnectorConfig';
import {connectors} from '../connectors';
import {dispatchLwcEvent} from '../utils/dispatchLwcEvent';
import {Connector} from '../connectors/Connector';

interface PrivateStore {
readonly connector: Connector | undefined;
Expand Down Expand Up @@ -49,11 +49,15 @@ const store = createStore<Store>((set) => ({
try {
const connector = new connectors[config.connectorType](config);
await connector.init();
const balance = await connector.getBalance();
const alias = await connector.getAlias();
privateStore.getState().setConfig(config);
privateStore.getState().setConnector(connector);
set({
connected: true,
connecting: false,
balance,
alias,
connectorName: config.connectorName,
});
dispatchLwcEvent('lwc:connected');
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2376,10 +2376,10 @@
portfinder "^1.0.32"
source-map "^0.7.3"

"@webbtc/webln-types@^1.0.12":
version "1.0.12"
resolved "https://registry.yarnpkg.com/@webbtc/webln-types/-/webln-types-1.0.12.tgz#ddb5f0dbaa0a853ef21a4f36a603199d43cc8682"
integrity sha512-uCsJt78RaW/UYDXwAjjs6aj7fiXyozwMknWvPROCaGMx+rXoPddtDjMIMbMFLvUJVQmnyzpqGkx/0jBIvVaVvA==
"@webbtc/webln-types@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@webbtc/webln-types/-/webln-types-2.0.0.tgz#d0fae5d2bf253a77dccbd37b2c9752292b798686"
integrity sha512-tLYbKHDHUugArcw1nyAOelpMhno4FrCAcHiNm6Fzt4c9EqVbGpP9HSE9v4tSiWP+5oExecZ9cdWCAarDL/qoLw==

"@webcomponents/webcomponentsjs@^2.6.0":
version "2.8.0"
Expand Down

0 comments on commit e2ad6c9

Please sign in to comment.