Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 33c37e4

Browse files
committed
Refactor timeout logic
1 parent 5f142ce commit 33c37e4

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/action/payment.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,34 +170,28 @@ class PaymentAction {
170170
* @return {Promise<undefined>}
171171
*/
172172
async payLightning() {
173+
const to = setTimeout(() => this._nav.goPaymentFailed(), PAYMENT_TIMEOUT);
173174
try {
174175
this._nav.goWait();
175176
const invoice = this._store.payment.address.replace(PREFIX_URI, '');
176177
const stream = this._grpc.sendStreamCommand('sendPayment');
177-
const success = await new Promise((resolve, reject) => {
178-
const timeout = setTimeout(() => resolve(false), PAYMENT_TIMEOUT);
178+
await new Promise((resolve, reject) => {
179179
stream.on('data', data => {
180180
if (data.payment_error) {
181-
clearTimeout(timeout);
182181
reject(new Error(`Lightning payment error: ${data.payment_error}`));
183182
} else {
184-
clearTimeout(timeout);
185-
resolve(true);
183+
resolve();
186184
}
187185
});
188-
stream.on('error', () => {
189-
clearTimeout(timeout);
190-
reject();
191-
});
186+
stream.on('error', reject);
192187
stream.write(JSON.stringify({ payment_request: invoice }), 'utf8');
193188
});
194-
if (!success) {
195-
this._nav.goPaymentFailed();
196-
}
197189
this._nav.goPayLightningDone();
198190
} catch (err) {
199191
this._nav.goPayLightningConfirm();
200192
this._notification.display({ msg: 'Lightning payment failed!', err });
193+
} finally {
194+
clearTimeout(to);
201195
}
202196
}
203197
}

test/unit/action/payment.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('Action Payments Unit Tests', () => {
2222
store = new Store();
2323
store.settings.displayFiat = false;
2424
require('../../../src/config').RETRY_DELAY = 1;
25-
require('../../../src/config').PAYMENT_TIMEOUT = 500;
25+
require('../../../src/config').PAYMENT_TIMEOUT = 10;
2626
grpc = sinon.createStubInstance(GrpcAction);
2727
notification = sinon.createStubInstance(NotificationAction);
2828
nav = sinon.createStubInstance(NavAction);
@@ -243,9 +243,10 @@ describe('Action Payments Unit Tests', () => {
243243
});
244244

245245
it('should go to error page on timeout', async () => {
246-
await payment.payLightning({ invoice: 'some-invoice' });
246+
payment.payLightning({ invoice: 'some-invoice' });
247+
await nap(100);
247248
expect(nav.goPaymentFailed, 'was called once');
248-
expect(nav.goPayLightningConfirm, 'was not called');
249+
expect(nav.goPayLightningDone, 'was not called');
249250
});
250251
});
251252
});

0 commit comments

Comments
 (0)