|
7 | 7 | Nip47NotificationType, |
8 | 8 | NWCClient, |
9 | 9 | } from "./NWCClient"; |
| 10 | +import { Subscription } from "nostr-tools/lib/types/abstract-relay"; |
10 | 11 |
|
11 | 12 | export type NWAOptions = { |
12 | 13 | relayUrl: string; |
@@ -170,47 +171,87 @@ export class NWAClient { |
170 | 171 | }): Promise<{ |
171 | 172 | unsub: () => void; |
172 | 173 | }> { |
173 | | - await this._checkConnected(); |
174 | | - |
175 | | - const sub = this.relay.subscribe( |
176 | | - [ |
177 | | - { |
178 | | - kinds: [13194], // NIP-47 info event |
179 | | - "#p": [this.options.appPubkey], |
180 | | - }, |
181 | | - ], |
182 | | - { |
183 | | - // eoseTimeout: 10000, |
184 | | - }, |
185 | | - ); |
| 174 | + let subscribed = true; |
| 175 | + let endPromise: (() => void) | undefined; |
| 176 | + let onRelayDisconnect: (() => void) | undefined; |
| 177 | + let sub: Subscription | undefined; |
| 178 | + (async () => { |
| 179 | + while (subscribed) { |
| 180 | + try { |
| 181 | + await this._checkConnected(); |
186 | 182 |
|
187 | | - const unsub = () => { |
188 | | - sub.close(); |
189 | | - this.relay.close(); |
190 | | - }; |
| 183 | + const sub = this.relay.subscribe( |
| 184 | + [ |
| 185 | + { |
| 186 | + kinds: [13194], // NIP-47 info event |
| 187 | + "#p": [this.options.appPubkey], |
| 188 | + }, |
| 189 | + ], |
| 190 | + { |
| 191 | + // eoseTimeout: 10000, |
| 192 | + }, |
| 193 | + ); |
| 194 | + console.info("subscribed to relay"); |
191 | 195 |
|
192 | | - sub.onevent = async (event) => { |
193 | | - const client = new NWCClient({ |
194 | | - relayUrl: this.options.relayUrl, |
195 | | - secret: this.appSecretKey, |
196 | | - walletPubkey: event.pubkey, |
197 | | - }); |
198 | | - |
199 | | - // try to fetch the lightning address |
200 | | - try { |
201 | | - const info = await client.getInfo(); |
202 | | - client.options.lud16 = info.lud16; |
203 | | - client.lud16 = info.lud16; |
204 | | - } catch (error) { |
205 | | - console.error("failed to fetch get_info", error); |
206 | | - } |
| 196 | + const unsub = () => { |
| 197 | + sub.close(); |
| 198 | + this.relay.close(); |
| 199 | + }; |
207 | 200 |
|
208 | | - args.onSuccess(client); |
209 | | - unsub(); |
210 | | - }; |
| 201 | + sub.onevent = async (event) => { |
| 202 | + const client = new NWCClient({ |
| 203 | + relayUrl: this.options.relayUrl, |
| 204 | + secret: this.appSecretKey, |
| 205 | + walletPubkey: event.pubkey, |
| 206 | + }); |
| 207 | + |
| 208 | + // try to fetch the lightning address |
| 209 | + try { |
| 210 | + const info = await client.getInfo(); |
| 211 | + client.options.lud16 = info.lud16; |
| 212 | + client.lud16 = info.lud16; |
| 213 | + } catch (error) { |
| 214 | + console.error("failed to fetch get_info", error); |
| 215 | + } |
| 216 | + |
| 217 | + args.onSuccess(client); |
| 218 | + unsub(); |
| 219 | + }; |
| 220 | + |
| 221 | + await new Promise<void>((resolve) => { |
| 222 | + endPromise = () => { |
| 223 | + resolve(); |
| 224 | + }; |
| 225 | + onRelayDisconnect = () => { |
| 226 | + console.info("relay disconnected"); |
| 227 | + endPromise?.(); |
| 228 | + }; |
| 229 | + this.relay.onclose = onRelayDisconnect; |
| 230 | + }); |
| 231 | + if (onRelayDisconnect !== undefined) { |
| 232 | + this.relay.onclose = null; |
| 233 | + } |
| 234 | + } catch (error) { |
| 235 | + console.error( |
| 236 | + "error subscribing to info event", |
| 237 | + error || "unknown relay error", |
| 238 | + ); |
| 239 | + } |
| 240 | + if (subscribed) { |
| 241 | + // wait a second and try re-connecting |
| 242 | + // any events during this period will be lost |
| 243 | + // unless using a relay that keeps events until client reconnect |
| 244 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 245 | + } |
| 246 | + } |
| 247 | + })(); |
211 | 248 |
|
212 | 249 | return { |
213 | | - unsub, |
| 250 | + unsub: () => { |
| 251 | + subscribed = false; |
| 252 | + endPromise?.(); |
| 253 | + sub?.close(); |
| 254 | + }, |
214 | 255 | }; |
215 | 256 | } |
216 | 257 |
|
|
0 commit comments