Skip to content

Commit e0ea694

Browse files
committed
Wait for 'synced' event
1 parent a5925ed commit e0ea694

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

wallets/client/protocols/breezSpark.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import init, { defaultConfig, connect } from '@breeztech/breez-sdk-spark'
2+
import { withTimeout } from '@/lib/time'
23
import { getUsername } from '@/wallets/lib/protocols/breezSpark'
34

45
export const name = 'BREEZ_SPARK'
56

7+
const SDK_SYNC_TIMEOUT_MS = 30_000
8+
69
async function withSdk (mnemonic, cb) {
710
await init()
811

@@ -18,11 +21,38 @@ async function withSdk (mnemonic, cb) {
1821
storageDir: 'breez-sdk-spark'
1922
})
2023

21-
const result = await cb(sdk)
24+
try {
25+
await waitForSync(sdk)
26+
return await cb(sdk)
27+
} finally {
28+
sdk.disconnect()
29+
}
30+
}
31+
32+
async function waitForSync (sdk) {
33+
const syncPromise = new Promise(
34+
resolve => sdk.addEventListener(new SyncEventListener(resolve))
35+
)
36+
await withTimeout(syncPromise, SDK_SYNC_TIMEOUT_MS)
37+
}
38+
39+
class SdkEventListener {
40+
constructor (event, resolve) {
41+
this.event = event
42+
this.resolve = resolve
43+
}
2244

23-
sdk.disconnect()
45+
onEvent (event) {
46+
if (event.type === this.event) {
47+
this.resolve()
48+
}
49+
}
50+
}
2451

25-
return result
52+
class SyncEventListener extends SdkEventListener {
53+
constructor (resolve) {
54+
super('synced', resolve)
55+
}
2656
}
2757

2858
export async function sendPayment (bolt11, { mnemonic }, { signal }) {

0 commit comments

Comments
 (0)