Skip to content

Commit

Permalink
fix: use new PlatformAPI in exp features
Browse files Browse the repository at this point in the history
  • Loading branch information
rxri committed Feb 3, 2025
1 parent cfda93b commit f1fc842
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
40 changes: 29 additions & 11 deletions jsHelper/expFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,41 @@

(function waitForRemoteConfigResolver() {
// Don't show options if hooks aren't patched/loaded
if (!hooksPatched || (!Spicetify.RemoteConfigResolver && !Spicetify.Platform?.RemoteConfiguration)) {
if (!hooksPatched || (!Spicetify.RemoteConfigResolver && !Spicetify.Platform?.RemoteConfigDebugAPI && !Spicetify.Platform?.RemoteConfiguration)) {
setTimeout(waitForRemoteConfigResolver, 500);
return;
}

let remoteConfiguration = Spicetify.RemoteConfigResolver?.value.remoteConfiguration || Spicetify.Platform?.RemoteConfiguration;
let setOverrides = () => {};
const setOverrides = async (overrides) => {
if (Spicetify.Platform?.RemoteConfigDebugAPI) {
for (const [name, value] of Object.entries(overrides)) {
const feature = overrideList[name];
const type = feature.values ? "enum" : typeof value === "number" ? "number" : "boolean";
await Spicetify.Platform.RemoteConfigDebugAPI.setOverride(
{
source: "web",
type,
name,
},
value
);
}
} else if (Spicetify.RemoteConfigResolver?.value?.setOverrides) {
Spicetify.RemoteConfigResolver.value.setOverrides(Spicetify.createInternalMap?.(overrides));
}
};

(function waitForResolver() {
if (!Spicetify.RemoteConfigResolver) {
(async function waitForResolver() {
if (!Spicetify.RemoteConfigResolver && !Spicetify.Platform?.RemoteConfigDebugAPI) {
isFallback = true;
notice.innerText = "⚠️ Using fallback mode. Some features may not work.";
setTimeout(waitForResolver, 500);
return;
}
isFallback = false;
notice.remove();
remoteConfiguration = Spicetify.RemoteConfigResolver.value.remoteConfiguration;
setOverrides = Spicetify.RemoteConfigResolver.value.setOverrides;
remoteConfiguration = Spicetify?.RemoteConfigResolver?.remoteConfiguration ?? (await Spicetify.Platform?.RemoteConfigDebugAPI.getProperties());
})();

for (const key of Object.keys(overrideList)) {
Expand All @@ -165,7 +181,7 @@
localStorage.setItem("spicetify-exp-features", JSON.stringify(overrideList));

featureMap[name] = value;
setOverrides(Spicetify.createInternalMap?.(featureMap));
setOverrides({ [name]: value });
}

function createSlider(name, desc, defaultVal) {
Expand Down Expand Up @@ -254,8 +270,12 @@ ${Spicetify.SVGIcons.search}

for (const name of Object.keys(overrideList)) {
if (!prevSessionOverrideList.includes(name) && remoteConfiguration.values.has(name)) {
changeValue(name, remoteConfiguration.values.get(name));
console.log(name, remoteConfiguration.values.get(name), overrideList[name]);
const currentValue = remoteConfiguration.values.get(name);
overrideList[name].value = currentValue;
localStorage.setItem("spicetify-exp-features", JSON.stringify(overrideList));

featureMap[name] = currentValue;
setOverrides({ [name]: currentValue });
}

const feature = overrideList[name];
Expand All @@ -269,8 +289,6 @@ ${Spicetify.SVGIcons.search}
}

content.appendChild(resetButton);

setOverrides(Spicetify.createInternalMap?.(featureMap));
})();

await new Promise((res) => Spicetify.Events.webpackLoaded.on(res));
Expand Down
8 changes: 8 additions & 0 deletions src/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,20 @@ func insertExpFeatures(jsPath string, flags Flag) {
return fmt.Sprintf("%s%s=Spicetify.expFeatureOverride(%s);%s", submatches[1], submatches[2], submatches[2], submatches[3])
})

// utils.ReplaceOnce(
// &content,
// `(\w+\.fromJSON)(\s*=\s*function\b[^{]*{[^}]*})`,
// func(submatches ...string) string {
// return fmt.Sprintf("%s=Spicetify.createInternalMap%s", submatches[1], submatches[2])
// })

utils.ReplaceOnce(
&content,
`(([\w$.]+\.fromJSON)\(\w+\)+;)(return ?[\w{}().,]+[\w$]+\.Provider,)(\{value:\{localConfiguration)`,
func(submatches ...string) string {
return fmt.Sprintf("%sSpicetify.createInternalMap=%s;%sSpicetify.RemoteConfigResolver=%s", submatches[1], submatches[2], submatches[3], submatches[4])
})

return content
})
}
Expand Down

0 comments on commit f1fc842

Please sign in to comment.