UNPKG

@builder.io/mitosis

Version:

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

47 lines (46 loc) 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseAction = void 0; const astring_1 = require("astring"); const string_1 = require("../helpers/string"); function parseAction(json, nodeReference, attribute) { var _a; const methodName = attribute.name; let parameters = ''; if (['Identifier', 'ObjectExpression'].includes((_a = attribute.expression) === null || _a === void 0 ? void 0 : _a.type)) { parameters = (0, astring_1.generate)(attribute.expression); } const actionHandler = (0, string_1.uniqueName)(Object.keys(json.state), 'actionHandler'); json.state[actionHandler] = { code: 'null', type: 'property', propertyType: 'normal', }; const initHandler = `if (${nodeReference}) { ${actionHandler} = ${methodName}(${nodeReference}, ${parameters}); };\n`; json.hooks.onMount.push({ code: initHandler, }); // Handle Destroy / Re-Mount const onReferenceUpdate = ` if (!${nodeReference} && ${actionHandler}) { ${actionHandler}?.destroy(); ${actionHandler} = null; } else if (${nodeReference} && !${actionHandler}) { ${initHandler} };\n `; json.hooks.onUpdate = json.hooks.onUpdate || []; json.hooks.onUpdate.push({ code: onReferenceUpdate, deps: `[${nodeReference}]`, }); // Handle Update if (parameters) { const onUpdate = `${actionHandler}?.update(${parameters})\n`; json.hooks.onUpdate.push({ code: onUpdate, deps: `[${parameters}]`, }); } } exports.parseAction = parseAction;