| 222 | } |
| 223 | |
| 224 | const executePropsCodeBlocks = async (executable: FrameExecutable, results: ResultStore, contexts: FrameContexts, api: API): Promise<ResultStore> => { |
| 225 | const { id} = executable.node |
| 226 | const codeBlockStore = executable.execProps ?? {} |
| 227 | // Sort keys based on dependencies. |
| 228 | |
| 229 | |
| 230 | // Prepare an environment for executing code blocks. |
| 231 | const environment = results.state; |
| 232 | environment[id] = { |
| 233 | props: results.state[id]?.props ?? {}, |
| 234 | actions: results.state[id]?.actions ?? {}, |
| 235 | styles: results.state[id]?.styles ?? {}, |
| 236 | } |
| 237 | environment.$contexts = contexts, |
| 238 | environment.$api = api |
| 239 | for (const {name: key, isConst} of executable.execPropsOptions.props) { |
| 240 | // Execute the code block. |
| 241 | |
| 242 | try { |
| 243 | let result; |
| 244 | if (key in (results.newState?.[id]?.['props'] || {}) && isConst) { |
| 245 | result = results.newState[id]['props'][key] |
| 246 | } else { |
| 247 | result = codeBlockStore[key]?.call(environment); |
| 248 | } |
| 249 | // Store the result. |
| 250 | if (result !== null) { |
| 251 | environment[id]['props'][key] = result; |
| 252 | results.state[id]['props'][key] = result; |
| 253 | if (results.newState) { |
| 254 | //update newstate so dependencies are updated |
| 255 | results.newState[id] = results.newState[id] ?? {props: {}, styles: {}, actions: {}} |
| 256 | results.newState[id]['props'][key] = result; |
| 257 | } |
| 258 | } else { |
| 259 | delete environment[id]['props'][key] |
| 260 | delete results.state[id]['props'][key] |
| 261 | if (results.newState?.[id]) { |
| 262 | delete results.newState[id]['props'][key]; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | } catch (error) { |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return results; |
| 271 | } |
| 272 | |
| 273 | function executeCodeBlocks(node: FrameNode, type: 'actions' | 'styles', codeBlockStore: FrameExecProp, results: ResultStore): ResultStore { |
| 274 | // Sort keys based on dependencies. |