Improve settings listener api
This commit is contained in:
parent
113f47ca7f
commit
1645913b7f
|
@ -37,7 +37,7 @@ try {
|
|||
var settings = mergeDefaults({} as Settings, DefaultSettings);
|
||||
}
|
||||
|
||||
type SubscriptionCallback = ((newValue: any) => void) & { _path?: string; };
|
||||
type SubscriptionCallback = ((newValue: any, path: string) => void) & { _path?: string; };
|
||||
const subscriptions = new Set<SubscriptionCallback>();
|
||||
|
||||
function makeProxy(settings: Settings, root = settings, path = ""): Settings {
|
||||
|
@ -55,7 +55,7 @@ function makeProxy(settings: Settings, root = settings, path = ""): Settings {
|
|||
const setPath = `${path}${path && "."}${p}`;
|
||||
for (const subscription of subscriptions) {
|
||||
if (!subscription._path || subscription._path === setPath) {
|
||||
subscription(v);
|
||||
subscription(v, setPath);
|
||||
}
|
||||
}
|
||||
VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(root, null, 4));
|
||||
|
@ -92,9 +92,19 @@ type ResolvePropDeep<T, P> = P extends "" ? T :
|
|||
P extends `${infer Pre}.${infer Suf}` ?
|
||||
Pre extends keyof T ? ResolvePropDeep<T[Pre], Suf> : never : P extends keyof T ? T[P] : never;
|
||||
|
||||
export function addSettingsListener<Path extends keyof Settings>(path: Path, onUpdate: (newValue: Settings[Path]) => void): void;
|
||||
export function addSettingsListener<Path extends string>(path: Path, onUpdate: (newValue: ResolvePropDeep<Settings, Path>) => void): void;
|
||||
export function addSettingsListener(path: string, onUpdate: (newValue: any) => void) {
|
||||
/**
|
||||
* Add a settings listener that will be invoked whenever the desired setting is updated
|
||||
* @param path Path to the setting that you want to watch, for example "plugins.Unindent.enabled" will fire your callback
|
||||
* whenever Unindent is toggled. Pass an empty string to get notified for all changes
|
||||
* @param onUpdate Callback function whenever a setting matching path is updated. It gets passed the new value and the path
|
||||
* to the updated setting. This path will be the same as your path argument, unless it was an empty string.
|
||||
*
|
||||
* @example addSettingsListener("", (newValue, path) => console.log(`${path} is now ${newValue}`))
|
||||
* addSettingsListener("plugins.Unindent.enabled", v => console.log("Unindent is now", v ? "enabled" : "disabled"))
|
||||
*/
|
||||
export function addSettingsListener<Path extends keyof Settings>(path: Path, onUpdate: (newValue: Settings[Path], path: Path) => void): void;
|
||||
export function addSettingsListener<Path extends string>(path: Path, onUpdate: (newValue: Path extends "" ? any : ResolvePropDeep<Settings, Path>, path: Path extends "" ? string : Path) => void): void;
|
||||
export function addSettingsListener(path: string, onUpdate: (newValue: any, path: string) => void) {
|
||||
(onUpdate as SubscriptionCallback)._path = path;
|
||||
subscriptions.add(onUpdate);
|
||||
}
|
Loading…
Reference in a new issue