( pluginId: string, pluginName?: string, )
| 28 | }; |
| 29 | |
| 30 | export const createPluginSettingsHost = ( |
| 31 | pluginId: string, |
| 32 | pluginName?: string, |
| 33 | ): SettingsHost => { |
| 34 | const pluginSource: SettingSource = { type: 'plugin', pluginId, pluginName }; |
| 35 | return { |
| 36 | register: async (definitions: SettingDefinition[]) => { |
| 37 | const registeredIds = useSettingsStore |
| 38 | .getState() |
| 39 | .register(definitions, pluginSource); |
| 40 | return { registered: registeredIds }; |
| 41 | }, |
| 42 | get: async <T extends SettingValue = SettingValue>(id: string) => { |
| 43 | const fullyQualifiedId = normalizeId(pluginSource, id); |
| 44 | const currentValue = useSettingsStore |
| 45 | .getState() |
| 46 | .getValue(fullyQualifiedId); |
| 47 | return currentValue as T | undefined; |
| 48 | }, |
| 49 | set: async (id: string, value: SettingValue) => { |
| 50 | const fullyQualifiedId = normalizeId(pluginSource, id); |
| 51 | await useSettingsStore.getState().setValue(fullyQualifiedId, value); |
| 52 | }, |
| 53 | getGlobal: getGlobalSetting, |
| 54 | setGlobal: setGlobalSetting, |
| 55 | subscribe: <T extends SettingValue = SettingValue>( |
| 56 | id: string, |
| 57 | listener: (value: T | undefined) => void, |
| 58 | ) => { |
| 59 | const fullyQualifiedId = normalizeId(pluginSource, id); |
| 60 | let previousValue = useSettingsStore |
| 61 | .getState() |
| 62 | .getValue(fullyQualifiedId) as T | undefined; |
| 63 | const unsubscribe = useSettingsStore.subscribe((state) => { |
| 64 | const nextValue = state.getValue(fullyQualifiedId) as T | undefined; |
| 65 | if (nextValue !== previousValue) { |
| 66 | previousValue = nextValue; |
| 67 | listener(nextValue); |
| 68 | } |
| 69 | }); |
| 70 | return unsubscribe; |
| 71 | }, |
| 72 | }; |
| 73 | }; |
| 74 | |
| 75 | export const createCoreSettingsHost = (): SettingsHost => { |
| 76 | const coreSource: SettingSource = { type: 'core' }; |
no test coverage detected