UNPKG

@builder.io/mitosis

Version:

Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io

56 lines (55 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getState = void 0; const function_1 = require("fp-ts/lib/function"); const get_state_object_string_1 = require("../../../helpers/get-state-object-string"); const state_1 = require("../../../helpers/state"); const helpers_1 = require("./helpers"); const signals_1 = require("./signals"); const store_1 = require("./store"); const getState = ({ json, options, }) => { const hasState = (0, state_1.checkHasState)(json); if (!hasState) { return undefined; } // unbundle state in case the user provides a type override of one of the state values const { mutable, signal, store } = Object.entries(json.state).reduce((acc, [key, value]) => { const stateType = (0, helpers_1.getStateTypeForValue)({ value: key, component: json, options }); switch (stateType) { case 'mutable': return { ...acc, mutable: { ...acc.mutable, [key]: value } }; case 'signals': return { ...acc, signal: { ...acc.signal, [key]: value } }; case 'store': return { ...acc, store: { ...acc.store, [key]: value } }; } }, { mutable: {}, signal: {}, store: {} }); const hasMutableState = Object.keys(mutable).length > 0; const hasSignalState = Object.keys(signal).length > 0; const hasStoreState = Object.keys(store).length > 0; const mutableStateStr = hasMutableState ? (0, function_1.pipe)(mutable, get_state_object_string_1.getMemberObjectString, (str) => `const state = createMutable(${str});`) : ''; const signalStateStr = hasSignalState ? (0, signals_1.getSignalsCode)({ json, options, state: signal }) : ''; const storeStateStr = hasStoreState ? (0, store_1.getStoreCode)({ json, options, state: store }) : ''; const stateStr = ` ${mutableStateStr} ${signalStateStr} ${storeStateStr} `; const importObj = { store: [ ...(hasMutableState ? ['createMutable'] : []), ...(hasStoreState ? ['createStore', 'reconcile'] : []), ], solidjs: [ ...(hasSignalState ? ['createSignal', 'createMemo'] : []), ...(hasStoreState ? ['createEffect', 'on'] : []), ], }; return { str: stateStr, import: importObj, }; }; exports.getState = getState;