@@ -100,6 +100,15 @@ const getSlotsClient = (functionModules: any[], transport: any): SlotsClient | n
100100 }
101101} ;
102102
103+ const map = new Map ( ) ;
104+
105+ const retryCounter = ( slotId : string , action : "increment" | "clear" | "get" ) => {
106+ if ( ! map . has ( slotId ) ) map . set ( slotId , { count : 0 } ) ;
107+ if ( action === "increment" ) map . get ( slotId ) . count ++ ;
108+ else if ( action === "clear" ) map . delete ( slotId ) ;
109+ else if ( action === "get" ) return map . get ( slotId ) ?. count ;
110+ } ;
111+
103112( async function adblockify ( ) {
104113 // @ts -expect-error: Events are not defined in types
105114 await new Promise ( res => Spicetify . Events . platformLoaded . on ( res ) ) ;
@@ -134,7 +143,7 @@ const getSlotsClient = (functionModules: any[], transport: any): SlotsClient | n
134143 try {
135144 await productState . putOverridesValues ( { pairs : { ads : "0" , catalogue : "premium" , product : "premium" , type : "premium" } } ) ;
136145 } catch ( error : unknown ) {
137- console . error ( "adblockify: Failed inside `disableAds` function" , error ) ;
146+ console . error ( "adblockify: Failed inside `disableAds` function\n " , error ) ;
138147 }
139148 } ;
140149
@@ -158,7 +167,7 @@ const getSlotsClient = (functionModules: any[], transport: any): SlotsClient | n
158167 }
159168 setTimeout ( disableAds , 100 ) ;
160169 } catch ( error : unknown ) {
161- console . error ( "adblockify: Failed inside `configureAdManagers` function" , error ) ;
170+ console . error ( "adblockify: Failed inside `configureAdManagers` function\n " , error ) ;
162171 }
163172 } ;
164173
@@ -179,8 +188,14 @@ const getSlotsClient = (functionModules: any[], transport: any): SlotsClient | n
179188 if ( slotsClient ) slotsClient . clearAllAds ( { slotId } ) ;
180189 updateSlotSettings ( slotId ) ;
181190 } catch ( error : unknown ) {
182- console . error ( "adblockify: Failed inside `handleAdSlot` function. Retrying in 100ms..." , error ) ;
183- setTimeout ( handleAdSlot , 100 , data ) ;
191+ console . error ( "adblockify: Failed inside `handleAdSlot` function. Retrying in 1 second...\n" , error ) ;
192+ retryCounter ( slotId , "increment" ) ;
193+ if ( retryCounter ( slotId , "get" ) > 5 ) {
194+ console . error ( `adblockify: Failed inside \`handleAdSlot\` function for 5th time. Giving up...\nSlot id: ${ slotId } .` ) ;
195+ retryCounter ( slotId , "clear" ) ;
196+ return ;
197+ }
198+ setTimeout ( handleAdSlot , 1 * 1000 , data ) ;
184199 }
185200 configureAdManagers ( ) ;
186201 } ;
@@ -193,7 +208,7 @@ const getSlotsClient = (functionModules: any[], transport: any): SlotsClient | n
193208 await settingsClient . updateSlotEnabled ( { slotId, enabled : false } ) ;
194209 await settingsClient . updateDisplayTimeInterval ( { slotId, timeInterval : "0" } ) ;
195210 } catch ( error : unknown ) {
196- console . error ( "adblockify: Failed inside `updateSlotSettings` function. " , error ) ;
211+ console . error ( "adblockify: Failed inside `updateSlotSettings` function\n " , error ) ;
197212 }
198213 } ;
199214
@@ -207,7 +222,7 @@ const getSlotsClient = (functionModules: any[], transport: any): SlotsClient | n
207222 try {
208223 audio . inStreamApi . adsCoreConnector . subscribeToSlot ( slot , handleAdSlot ) ;
209224 } catch ( error : unknown ) {
210- console . error ( "adblockify: Failed inside `subToSlot` function" , error ) ;
225+ console . error ( "adblockify: Failed inside `subToSlot` function\n " , error ) ;
211226 }
212227 } ;
213228
@@ -247,7 +262,7 @@ const getSlotsClient = (functionModules: any[], transport: any): SlotsClient | n
247262 if ( inAppMessages ) expFeatureOverride ( { name : "enableInAppMessaging" , default : false } ) ;
248263 if ( ! upgradeCTA ) expFeatureOverride ( { name : "hideUpgradeCTA" , default : true } ) ;
249264 } catch ( error : unknown ) {
250- console . error ( "adblockify: Failed inside `enableExperimentalFeatures` function" , error ) ;
265+ console . error ( "adblockify: Failed inside `enableExperimentalFeatures` function\n " , error ) ;
251266 }
252267 } ;
253268
0 commit comments