MCPcopy
hub / github.com/nukeop/nuclear / createPluginSettingsHost

Function createPluginSettingsHost

packages/player/src/services/settingsHost.ts:30–73  ·  view source on GitHub ↗
(
  pluginId: string,
  pluginName?: string,
)

Source from the content-addressed store, hash-verified

28};
29
30export 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
75export const createCoreSettingsHost = (): SettingsHost => {
76 const coreSource: SettingSource = { type: 'core' };

Callers 4

settings.test.tsxFile · 0.90
useSettingsHostFunction · 0.90
createPluginAPIFunction · 0.90

Calls 4

getStateMethod · 0.80
normalizeIdFunction · 0.70
registerMethod · 0.45
subscribeMethod · 0.45

Tested by

no test coverage detected