Skip to content

Commit d491666

Browse files
committed
feat(adblock): give up after 5 times
1 parent 3487740 commit d491666

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

adblock/adblock.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ const getSlotsClient = (functionModules, transport) => {
3838
return null;
3939
}
4040
};
41+
const map = new Map();
42+
const retryCounter = (slotId, action) => {
43+
if (!map.has(slotId))
44+
map.set(slotId, { count: 0 });
45+
if (action === "increment")
46+
map.get(slotId).count++;
47+
else if (action === "clear")
48+
map.delete(slotId);
49+
else if (action === "get")
50+
return map.get(slotId)?.count;
51+
};
4152
(async function adblockify() {
4253
// @ts-expect-error: Events are not defined in types
4354
await new Promise(res => Spicetify.Events.platformLoaded.on(res));
@@ -69,7 +80,7 @@ const getSlotsClient = (functionModules, transport) => {
6980
await productState.putOverridesValues({ pairs: { ads: "0", catalogue: "premium", product: "premium", type: "premium" } });
7081
}
7182
catch (error) {
72-
console.error("adblockify: Failed inside `disableAds` function", error);
83+
console.error("adblockify: Failed inside `disableAds` function\n", error);
7384
}
7485
};
7586
const configureAdManagers = async () => {
@@ -91,7 +102,7 @@ const getSlotsClient = (functionModules, transport) => {
91102
setTimeout(disableAds, 100);
92103
}
93104
catch (error) {
94-
console.error("adblockify: Failed inside `configureAdManagers` function", error);
105+
console.error("adblockify: Failed inside `configureAdManagers` function\n", error);
95106
}
96107
};
97108
const bindToSlots = async () => {
@@ -112,8 +123,14 @@ const getSlotsClient = (functionModules, transport) => {
112123
updateSlotSettings(slotId);
113124
}
114125
catch (error) {
115-
console.error("adblockify: Failed inside `handleAdSlot` function. Retrying in 100ms...", error);
116-
setTimeout(handleAdSlot, 100, data);
126+
console.error("adblockify: Failed inside `handleAdSlot` function. Retrying in 1 second...\n", error);
127+
retryCounter(slotId, "increment");
128+
if (retryCounter(slotId, "get") > 5) {
129+
console.error(`adblockify: Failed inside \`handleAdSlot\` function for 5th time. Giving up...\nSlot id: ${slotId}.`);
130+
retryCounter(slotId, "clear");
131+
return;
132+
}
133+
setTimeout(handleAdSlot, 1 * 1000, data);
117134
}
118135
configureAdManagers();
119136
};
@@ -127,7 +144,7 @@ const getSlotsClient = (functionModules, transport) => {
127144
await settingsClient.updateDisplayTimeInterval({ slotId, timeInterval: "0" });
128145
}
129146
catch (error) {
130-
console.error("adblockify: Failed inside `updateSlotSettings` function.", error);
147+
console.error("adblockify: Failed inside `updateSlotSettings` function\n", error);
131148
}
132149
};
133150
const intervalUpdateSlotSettings = async () => {
@@ -140,7 +157,7 @@ const getSlotsClient = (functionModules, transport) => {
140157
audio.inStreamApi.adsCoreConnector.subscribeToSlot(slot, handleAdSlot);
141158
}
142159
catch (error) {
143-
console.error("adblockify: Failed inside `subToSlot` function", error);
160+
console.error("adblockify: Failed inside `subToSlot` function\n", error);
144161
}
145162
};
146163
const runObserver = () => {
@@ -183,7 +200,7 @@ const getSlotsClient = (functionModules, transport) => {
183200
expFeatureOverride({ name: "hideUpgradeCTA", default: true });
184201
}
185202
catch (error) {
186-
console.error("adblockify: Failed inside `enableExperimentalFeatures` function", error);
203+
console.error("adblockify: Failed inside `enableExperimentalFeatures` function\n", error);
187204
}
188205
};
189206
enableExperimentalFeatures();

adblock/adblock.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)