feat(preferences): update preferences structure and enhance shortcut management
- Updated the preferences configuration with new shortcut definitions and types for better management. - Introduced a method to get and subscribe to preference changes in the PreferenceService, improving reactivity. - Refactored SelectionService to utilize the new preference management system, enhancing code clarity and maintainability. - Updated SelectionAssistantSettings to use the new preference hooks for managing selection settings. - Adjusted the generated preferences file to reflect the latest configuration changes.
This commit is contained in:
@@ -94,6 +94,20 @@ export class PreferenceService {
|
||||
return this.cache[key] ?? DefaultPreferences.default[key]
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single preference value from memory cache and subscribe to changes
|
||||
* @param key - The preference key to get
|
||||
* @param callback - The callback function to call when the preference changes
|
||||
* @returns The current value of the preference
|
||||
*/
|
||||
public getAndSubscribeChange<K extends PreferenceKeyType>(
|
||||
key: K,
|
||||
callback: (newValue: PreferenceDefaultScopeType[K], oldValue?: PreferenceDefaultScopeType[K]) => void
|
||||
): PreferenceDefaultScopeType[K] {
|
||||
const value = this.get(key)
|
||||
this.subscribeChange(key, callback)
|
||||
return value
|
||||
}
|
||||
/**
|
||||
* Set a single preference value
|
||||
* Updates both database and memory cache, then broadcasts changes to all listeners
|
||||
@@ -261,11 +275,11 @@ export class PreferenceService {
|
||||
*/
|
||||
public subscribeChange<K extends PreferenceKeyType>(
|
||||
key: K,
|
||||
callback: (newValue: PreferenceDefaultScopeType[K], oldValue: PreferenceDefaultScopeType[K]) => void
|
||||
callback: (newValue: PreferenceDefaultScopeType[K], oldValue?: PreferenceDefaultScopeType[K]) => void
|
||||
): () => void {
|
||||
const listener = (changedKey: string, newValue: any, oldValue: any) => {
|
||||
if (changedKey === key) {
|
||||
callback(newValue, oldValue)
|
||||
callback(newValue as PreferenceDefaultScopeType[K], oldValue as PreferenceDefaultScopeType[K])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user