1
1
mirror of https://github.com/bumi/lntip synced 2025-06-17 02:05:35 +00:00

Refactor JS widget API to use promises

This commit is contained in:
bumi 2019-01-08 16:53:35 +01:00
parent 625b13d8fc
commit a807fce81d

View File

@ -6,8 +6,7 @@ LnTip = function (options) {
this.host = options.host || host; this.host = options.host || host;
this.amount = options.amount; this.amount = options.amount;
this.memo = options.memo || ''; this.memo = options.memo || '';
this.loadStylesheet(); this.loadStylesheet(); // load it early that styles are ready when the popup is opened
this.getInvoice();
} }
LnTip.prototype.loadStylesheet = function () { LnTip.prototype.loadStylesheet = function () {
@ -50,16 +49,19 @@ LnTip.prototype.thanks = function () {
LnTip.prototype.watchPayment = function () { LnTip.prototype.watchPayment = function () {
if (this.paymentWatcher) { window.clearInterval(this.paymentWatcher) } if (this.paymentWatcher) { window.clearInterval(this.paymentWatcher) }
this.paymentWatcher = window.setInterval(() => {
this._request(`${this.host}/settled/${this.invoice.ImplDepID}`) return new Promise((resolve, reject) => {
.then((settled) => { this.paymentWatcher = window.setInterval(() => {
if (settled) { this._fetch(`${this.host}/settled/${this.invoice.ImplDepID}`)
this.invoice.settled = true; .then((settled) => {
this.thanks(); if (settled) {
this.stopWatchingPayment(); this.invoice.settled = true;
} this.stopWatchingPayment();
}) resolve(this.invoice);
}, 2000); }
});
}, 2000);
});
} }
LnTip.prototype.stopWatchingPayment = function () { LnTip.prototype.stopWatchingPayment = function () {
@ -72,14 +74,14 @@ LnTip.prototype.payWithWebln = function () {
webln.enable().then((weblnResponse) => { webln.enable().then((weblnResponse) => {
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest }) return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
}).catch((e) => { }).catch((e) => {
this.requestPayment(); return this.requestPayment();
}) })
} else { } else {
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest }) return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
} }
} }
LnTip.prototype.requestPayment = function () { LnTip.prototype.showPaymentRequest = function () {
var content = `<div class="lntip-payment-request"> var content = `<div class="lntip-payment-request">
<h1>${this.memo}</h1> <h1>${this.memo}</h1>
<h2>${this.amount} satoshi</h2> <h2>${this.amount} satoshi</h2>
@ -101,7 +103,7 @@ LnTip.prototype.requestPayment = function () {
navigator.clipboard.writeText(this.invoice.PaymentRequest); navigator.clipboard.writeText(this.invoice.PaymentRequest);
alert('Copied to clipboad'); alert('Copied to clipboad');
} }
return Promise.resolve(); return Promise.resolve(); // be compatible to payWithWebln()
} }
LnTip.prototype.getInvoice = function () { LnTip.prototype.getInvoice = function () {
@ -111,22 +113,34 @@ LnTip.prototype.getInvoice = function () {
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ memo: this.memo, amount: this.amount }) body: JSON.stringify({ memo: this.memo, amount: this.amount })
}; };
return this._request( return this._fetch(
`${this.host}/invoice`, `${this.host}/invoice`,
args args
).then((invoice) => { ).then((invoice) => {
this.invoice = invoice; this.invoice = invoice;
this.watchPayment(); return invoice;
if (typeof webln !== 'undefined') {
this.payWithWebln();
} else {
this.requestPayment();
}
}) })
} }
LnTip.prototype._request = function(url, args) { LnTip.prototype.requestPayment = function () {
return this.getInvoice().then((invoice) => {
if (typeof webln !== 'undefined') {
return this.payWithWebln();
} else {
return this.showPaymentRequest();
}
});
}
LnTip.prototype.request = function () {
return this.requestPayment().then(() => {
this.watchPayment().then((invoice) => {
this.thanks();
});
});
}
LnTip.prototype._fetch = function(url, args) {
return fetch(url, args).then((response) => { return fetch(url, args).then((response) => {
return response.json(); return response.json();
}) })