@@ -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 }
0 commit comments