Skip to content

Commit f1dbb94

Browse files
committed
Merge branch '2.4.9-alpha3-develop' of github.com:magento-commerce/magento2ce into ACP2E-4018-alpha3
2 parents 3f1eea0 + b3774fb commit f1dbb94

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

app/code/Magento/Wishlist/view/frontend/web/js/view/wishlist-mixin.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,52 @@ define(['jquery', 'Magento_Customer/js/customer-data'], function ($, customerDat
1111
initialize: function () {
1212
this._super();
1313

14+
let updateTimer = null;
15+
1416
const selector = '.wishlist .counter.qty, .customer-menu .wishlist .counter, .link.wishlist .counter',
15-
wishlist = customerData.get('wishlist');
17+
wishlist = customerData.get('wishlist'),
18+
clearPeriodicCounterUpdate = function () {
19+
if (updateTimer) {
20+
clearInterval(updateTimer);
21+
updateTimer = null;
22+
}
23+
},
24+
updateCounters = function (updatedWishlist) {
25+
const counters = $(selector);
1626

17-
wishlist.subscribe(function (updatedWishlist) {
18-
const counters = $(selector);
27+
if (typeof updatedWishlist.counter !== 'undefined'
28+
&& updatedWishlist.counter !== null
29+
&& counters.length
30+
) {
31+
const expectedText = updatedWishlist.counter.toString(),
32+
currentText = counters.first().text().trim();
1933

20-
if (typeof updatedWishlist.counter !== 'undefined'
21-
&& updatedWishlist.counter !== null
22-
&& counters.length
23-
) {
24-
counters.text(updatedWishlist.counter);
25-
}
26-
if (updatedWishlist.counter === null) {
27-
counters.hide();
34+
// Check if text is already correct
35+
if (currentText === expectedText) {
36+
clearPeriodicCounterUpdate();
37+
return;
38+
}
39+
40+
counters.text(updatedWishlist.counter);
41+
counters.show();
42+
}
43+
if (updatedWishlist.counter === null) {
44+
clearPeriodicCounterUpdate();
45+
counters.hide();
46+
}
47+
};
48+
49+
// Subscribe to future wishlist changes
50+
wishlist.subscribe(updateCounters);
51+
52+
// Simple timer to periodically update counters
53+
updateTimer = setInterval(function () {
54+
const wishlistData = wishlist();
55+
56+
if (wishlistData && typeof wishlistData.counter !== 'undefined') {
57+
updateCounters(wishlistData);
2858
}
29-
});
59+
}, 1000);
3060

3161
return this;
3262
}

dev/tests/js/jasmine/tests/app/code/Magento/Wishlist/view/frontend/web/js/view/wishlist-mixin.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ define([
2525
subscribeCallback = callback;
2626
}
2727

28-
wishlistDataMock = {
29-
subscribe: jasmine.createSpy('subscribe').and.callFake(setupSubscribeCallback)
30-
};
28+
// Create wishlistDataMock as a function (KnockoutJS observable) with subscribe method
29+
wishlistDataMock = jasmine.createSpy('wishlistObservable').and.returnValue({
30+
counter: null // Default return value
31+
});
32+
wishlistDataMock.subscribe = jasmine.createSpy('subscribe').and.callFake(setupSubscribeCallback);
3133

3234
customerDataMock.get.and.returnValue(wishlistDataMock);
3335

0 commit comments

Comments
 (0)