MCPcopy
hub / github.com/BuilderIO/mitosis / getState

Function getState

packages/core/src/generators/solid/state/state.ts:18–83  ·  view source on GitHub ↗
({
  json,
  options,
}: {
  json: MitosisComponent;
  options: ToSolidOptions;
})

Source from the content-addressed store, hash-verified

16};
17
18export const getState = ({
19 json,
20 options,
21}: {
22 json: MitosisComponent;
23 options: ToSolidOptions;
24}): State | undefined => {
25 const hasState = checkHasState(json);
26
27 if (!hasState) {
28 return undefined;
29 }
30
31 // unbundle state in case the user provides a type override of one of the state values
32 const { mutable, signal, store } = Object.entries(json.state).reduce(
33 (acc, [key, value]) => {
34 const stateType = getStateTypeForValue({ value: key, component: json, options });
35
36 switch (stateType) {
37 case 'mutable':
38 return { ...acc, mutable: { ...acc.mutable, [key]: value } };
39 case 'signals':
40 return { ...acc, signal: { ...acc.signal, [key]: value } };
41 case 'store':
42 return { ...acc, store: { ...acc.store, [key]: value } };
43 }
44 },
45 { mutable: {}, signal: {}, store: {} } as {
46 mutable: MitosisState;
47 signal: MitosisState;
48 store: MitosisState;
49 },
50 );
51
52 const hasMutableState = Object.keys(mutable).length > 0;
53 const hasSignalState = Object.keys(signal).length > 0;
54 const hasStoreState = Object.keys(store).length > 0;
55
56 const mutableStateStr = hasMutableState
57 ? pipe(mutable, getMemberObjectString, (str) => `const state = createMutable(${str});`)
58 : '';
59 const signalStateStr = hasSignalState ? getSignalsCode({ json, options, state: signal }) : '';
60 const storeStateStr = hasStoreState ? getStoreCode({ json, options, state: store }) : '';
61
62 const stateStr = `
63 ${mutableStateStr}
64 ${signalStateStr}
65 ${storeStateStr}
66 `;
67
68 const importObj: State['import'] = {
69 store: [
70 ...(hasMutableState ? ['createMutable'] : []),
71 ...(hasStoreState ? ['createStore', 'reconcile'] : []),
72 ],
73 solidjs: [
74 ...(hasSignalState ? ['createSignal', 'createMemo'] : []),
75 ...(hasStoreState ? ['createEffect', 'on'] : []),

Callers 1

componentToSolidFunction · 0.90

Calls 4

checkHasStateFunction · 0.90
getStateTypeForValueFunction · 0.90
getSignalsCodeFunction · 0.90
getStoreCodeFunction · 0.90

Tested by

no test coverage detected