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

Function getStoreCode

packages/core/src/generators/solid/state/store.ts:30–97  ·  view source on GitHub ↗
({
  json: component,
  options,
  state,
}: {
  json: MitosisComponent;
  options: ToSolidOptions;
  state: MitosisState;
})

Source from the content-addressed store, hash-verified

28};
29
30export const getStoreCode = ({
31 json: component,
32 options,
33 state,
34}: {
35 json: MitosisComponent;
36 options: ToSolidOptions;
37 state: MitosisState;
38}) => {
39 const mapValue = updateStateCode({ options, component });
40
41 const stateUpdater = ([key, stateVal]: [
42 key: string,
43 stateVal: StateValue | undefined,
44 ]): string => {
45 if (!stateVal) {
46 return '';
47 }
48
49 const getCreateStoreStr = (initialValue: string) =>
50 `const [${key}, ${getStateSetterName(key)}] = createStore(${initialValue})`;
51
52 const getDefaultCase = () => pipe(value, mapValue, getCreateStoreStr);
53
54 const value = stateVal.code;
55 const type = stateVal.type;
56 if (typeof value === 'string') {
57 switch (type) {
58 case 'getter':
59 const getterValueAsFunction = replaceGetterWithFunction(value);
60 const { stateUsed, propsUsed } =
61 collectUsedStateAndPropsInFunction(getterValueAsFunction);
62
63 const fnValueWithMappedRefs = mapValue(getterValueAsFunction);
64
65 const FUNCTION_NAME = `update${capitalize(key)}`;
66 const deps = [
67 ...Array.from(stateUsed).map(
68 updateStateCode({
69 options,
70 component,
71 // there are no setters in deps
72 updateSetters: false,
73 }),
74 ),
75 ...Array.from(propsUsed),
76 ].join(', ');
77 return `
78 const ${FUNCTION_NAME} = ${fnValueWithMappedRefs}
79 ${getCreateStoreStr(`${FUNCTION_NAME}()`)}
80 createEffect(on(() => [${deps}], () => ${getStateSetterName(
81 key,
82 )}(reconcile(${FUNCTION_NAME}()))))
83 `;
84 case 'function':
85 return mapValue(value);
86 case 'method':
87 return pipe(value, prefixWithFunction, mapValue);

Callers 1

getStateFunction · 0.90

Calls 1

updateStateCodeFunction · 0.90

Tested by

no test coverage detected