Improve settings listener api
This commit is contained in:
parent
113f47ca7f
commit
1645913b7f
1 changed files with 16 additions and 6 deletions
|
@ -37,7 +37,7 @@ try {
|
||||||
var settings = mergeDefaults({} as Settings, DefaultSettings);
|
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>();
|
const subscriptions = new Set<SubscriptionCallback>();
|
||||||
|
|
||||||
function makeProxy(settings: Settings, root = settings, path = ""): Settings {
|
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}`;
|
const setPath = `${path}${path && "."}${p}`;
|
||||||
for (const subscription of subscriptions) {
|
for (const subscription of subscriptions) {
|
||||||
if (!subscription._path || subscription._path === setPath) {
|
if (!subscription._path || subscription._path === setPath) {
|
||||||
subscription(v);
|
subscription(v, setPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(root, null, 4));
|
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}` ?
|
P extends `${infer Pre}.${infer Suf}` ?
|
||||||
Pre extends keyof T ? ResolvePropDeep<T[Pre], Suf> : never : P extends keyof T ? T[P] : never;
|
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;
|
* Add a settings listener that will be invoked whenever the desired setting is updated
|
||||||
export function addSettingsListener(path: string, onUpdate: (newValue: any) => void) {
|
* @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;
|
(onUpdate as SubscriptionCallback)._path = path;
|
||||||
subscriptions.add(onUpdate);
|
subscriptions.add(onUpdate);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue