Open andreykutkov opened 4 years ago
Thanks for your comments.
The parseKeysendInvoice callback is called many times, as invoices payments are received from LND over the GRPC subscription. How would you propose handling that with a promise?
As there's only one party listening for subscribeInvoices, current callback style will work fine. If there're more than one listeners, other coding paradigms can be considered.
Regarding node B: Another drawback of circular dependency is that the call stack can get deepen resulting in possible failure of system on constant failures. So I suggest the following style. The pseudocode is:
// src/network/receive.ts
export async function initGrpcSubscriptions() {
await getInfo() // Just leaving this will rethrow error.
let error = InitialError();
// The loop that retries subscribeInvoices
while (error) {
try {
await lndService.subscribeInvoices(parseKeysendInvoice)
}
catch (caught) {
if (error is CriticalError) {
throw caught
}
else {
error = caught;
}
}
}
}
// src/grpc/index.ts
export function subscribeInvoices(parseKeysendInvoice) {
// On success:
parseKeysendInvoice(response)
resolve(status);
// On failure that requires retry
reject(SomeError());
// On critical failure that doesn't require retry
rejects(CriticalError());
}
The codebase seems to have some points to improve code convention-wise. Two major improvements that can made are:
Example call stack:
note A: Callback style here. Can be replaced with Promise. note B: Circular dependency. *** The code lines might be different due to frequent code update. But left there for reference assuming the difference might be not so huge.