Skip to content

Commit

Permalink
refactor: update ttl (#948)
Browse files Browse the repository at this point in the history
* refactor: update ttl

* Remove close

* Fix call

* Fix issues
  • Loading branch information
merlinpaypal authored Jun 20, 2023
1 parent 4ab58be commit 888fc7e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/library/zoid/treatments/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export default createGlobalVariableGetter('__paypal_credit_treatments__', () =>
onReady: {
type: 'function',
queryParam: false,
value: ({ close }) => {
// 1 day in milliseconds
const TREATMENTS_MAX_AGE = 1000 * 60 * 60 * 24;
value: () => {
// 15 minutes in milliseconds
const TREATMENTS_MAX_AGE = 1000 * 60 * 15;

return ({ treatmentsHash, deviceID }) => {
writeToLocalStorage({
Expand All @@ -60,8 +60,6 @@ export default createGlobalVariableGetter('__paypal_credit_treatments__', () =>
});

globalEvent.trigger('treatments');

close();
};
}
},
Expand Down
5 changes: 4 additions & 1 deletion src/utils/experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export function getLocalTreatments() {
if (experiments.expiration < Date.now()) {
// use existing value, but update treatments in the background
if (document.readyState === 'loading') {
window.addEventListener('DOMContentLoaded', fetchTreatments);
// This ensures that unique event objects are not passed by the event listener into fetchTreatments,
// which avoids the benefit of memoization, because we only want the logic inside fetchTreatments
// to execute once, which only occurs when fetchTreatments is always called with ()
window.addEventListener('DOMContentLoaded', () => fetchTreatments());
} else {
fetchTreatments();
}
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/spec/src/zoid/treatments/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('treatments component', () => {

test('handles treatment data', () => {
const {
close,
props: { onReady }
} = getTreatmentsComponent();

Expand All @@ -39,6 +38,5 @@ describe('treatments component', () => {
});

expect(globalEvent.trigger).toHaveBeenCalledWith('treatments');
expect(close).toHaveBeenCalled();
});
});

0 comments on commit 888fc7e

Please sign in to comment.