()
| 73 | }; |
| 74 | |
| 75 | export const createCoreSettingsHost = (): SettingsHost => { |
| 76 | const coreSource: SettingSource = { type: 'core' }; |
| 77 | return { |
| 78 | register: async (definitions: SettingDefinition[]) => { |
| 79 | const registeredIds = useSettingsStore |
| 80 | .getState() |
| 81 | .register(definitions, coreSource); |
| 82 | return { registered: registeredIds }; |
| 83 | }, |
| 84 | get: async <T extends SettingValue = SettingValue>(id: string) => { |
| 85 | const fullyQualifiedId = normalizeId(coreSource, id); |
| 86 | const currentValue = useSettingsStore |
| 87 | .getState() |
| 88 | .getValue(fullyQualifiedId); |
| 89 | return currentValue as T | undefined; |
| 90 | }, |
| 91 | set: async (id: string, value: SettingValue) => { |
| 92 | const fullyQualifiedId = normalizeId(coreSource, id); |
| 93 | await useSettingsStore.getState().setValue(fullyQualifiedId, value); |
| 94 | }, |
| 95 | getGlobal: getGlobalSetting, |
| 96 | setGlobal: setGlobalSetting, |
| 97 | subscribe: <T extends SettingValue = SettingValue>( |
| 98 | id: string, |
| 99 | listener: (value: T | undefined) => void, |
| 100 | ) => { |
| 101 | const fullyQualifiedId = normalizeId(coreSource, id); |
| 102 | let previousValue = useSettingsStore |
| 103 | .getState() |
| 104 | .getValue(fullyQualifiedId) as T | undefined; |
| 105 | const unsubscribe = useSettingsStore.subscribe((state) => { |
| 106 | const nextValue = state.getValue(fullyQualifiedId) as T | undefined; |
| 107 | if (nextValue !== previousValue) { |
| 108 | previousValue = nextValue; |
| 109 | listener(nextValue); |
| 110 | } |
| 111 | }); |
| 112 | return unsubscribe; |
| 113 | }, |
| 114 | }; |
| 115 | }; |
| 116 | |
| 117 | export const coreSettingsHost: SettingsHost = createCoreSettingsHost(); |
no test coverage detected