Skip to content

Commit 3ab169a

Browse files
xecdevxecdevgreptile-apps[bot]
authored
Improve Content Unlock Speed (#101)
* Improve Content Unlock Speed * Improve console message Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Improve console message 2 Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * responding to bot's feedback - improve console log messaging * remove redundant console log * Refactor unlock retry logic to use loop and comment debug log * remove debugging code * fix outdated comment --------- Co-authored-by: xecdev <ecashinformer@gmail.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent f108568 commit 3ab169a

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

assets/js/paywalled-content.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jQuery(document).ready(function($) {
3434
}
3535
}
3636

37-
// Shared state: user wallet address + unlock tx captured in onSuccess, consumed in onClose.
37+
// Shared state: user wallet address + unlock tx captured in onSuccess.
3838
let unlockAddr = null;
3939
let unlockTx = null;
4040

@@ -95,8 +95,7 @@ jQuery(document).ready(function($) {
9595
}
9696

9797
// Configure the PayButton like before, but:
98-
// - onSuccess only captures tx data
99-
// - onClose does the secure validate -> mark_payment_successful -> fetch flow
98+
// - onSuccess captures tx data and does the secure validate -> mark_payment_successful -> fetch flow
10099
PayButton.render($container[0], {
101100
to: configData.to,
102101
amount: configData.amount,
@@ -117,9 +116,7 @@ jQuery(document).ready(function($) {
117116
amount: tx.amount || '',
118117
timestamp: tx.timestamp || 0
119118
};
120-
},
121119

122-
onClose: function () {
123120
if (unlockAddr && unlockTx && unlockTx.hash) {
124121
const addrCopy = unlockAddr;
125122
const hashCopy = unlockTx.hash;
@@ -128,6 +125,10 @@ jQuery(document).ready(function($) {
128125
const postIdCopy = configData.postId;
129126

130127
function tryValidateUnlock(attempt) {
128+
const maxAttempts = 4; // total attempts
129+
const baseDelayMs = 1000; // 1.0s
130+
const stepDelayMs = 500; // +0.5s per retry
131+
131132
jQuery.post(
132133
PaywallAjax.ajaxUrl,
133134
{
@@ -159,9 +160,15 @@ jQuery(document).ready(function($) {
159160
}
160161
});
161162
} else {
162-
if (attempt === 1) {
163-
// Retry once after brief delay
164-
setTimeout(function () { tryValidateUnlock(2); }, 3000);
163+
if (attempt < maxAttempts) {
164+
// Retry after brief delay with incremental backoff:
165+
// 1s, 1.5s, 2s, 2.5s
166+
const nextAttempt = attempt + 1;
167+
const nextDelay = baseDelayMs + (nextAttempt - 1) * stepDelayMs;
168+
169+
setTimeout(function () {
170+
tryValidateUnlock(nextAttempt);
171+
}, nextDelay);
165172
} else {
166173
alert('⚠️ Payment could not be verified on-chain. Please try again.');
167174
}
@@ -170,13 +177,16 @@ jQuery(document).ready(function($) {
170177
);
171178
}
172179

173-
tryValidateUnlock(1);
180+
// Initial delay before first attempt to give the PayButton webhook time
181+
setTimeout(function () {
182+
tryValidateUnlock(1);
183+
}, 1000); // First attempt, 1s delay (selected experimentally)
174184
}
175185

176186
// Safe to clear shared state (the flow above uses the copies)
177187
unlockAddr = null;
178188
unlockTx = null;
179-
}
189+
},
180190
});
181191
});
182192
});

0 commit comments

Comments
 (0)