Files
sim/blocks/utils.ts
Emir Karabeg 76dbc4a52f Feat/db sync (#94)
* feat(db-sync): added general sync file and implemented environment sync

* improvement(workflows-store): structured workflows store system better and added getter for values across stores

* fix(stores): deleted workflows/types since unused

* improvement(db-sync): added workflow event syncs and debounce

* improvement(db-sync): clean and upgraded db-sync system; environment sync implemented

* improvement(db-sync): added batch sync with registry; init bug needs fixing

* improvement(db-sync): finalized sync system and implemented for workflow

* fix(db-sync): fixed client-side rendering

* improvement(db-sync): created backwards sync system; environment implemented

* improvement(db-sync): added colors to db

* fix(db-sync): color sync with db

* improvement(db-sync): added workflow backwards sync; fixing color bug and race condition

* fix(db-stores): color sync

* feature(db-sync): db-sync complete; need to sync history

* improvement(db-sync): added scheduling

* fix(db-sync): environment sync to db
2025-03-03 19:43:39 -08:00

50 lines
1.5 KiB
TypeScript

import { SubBlockState } from '@/stores/workflows/workflow/types'
import { BlockOutput, OutputConfig } from '@/blocks/types'
interface CodeLine {
id: string
content: string
}
function isEmptyValue(value: SubBlockState['value']): boolean {
if (value === null || value === undefined) return true
if (typeof value === 'string') return value.trim() === ''
if (typeof value === 'number') return false
if (Array.isArray(value)) {
// Handle code editor's array of lines format
if (value.length === 0) return true
if (isCodeEditorValue(value)) {
return value.every((line: any) => !line.content.trim())
}
return value.length === 0
}
return false
}
function isCodeEditorValue(value: any[]): value is CodeLine[] {
return value.length > 0 && 'id' in value[0] && 'content' in value[0]
}
export function resolveOutputType(
outputs: Record<string, OutputConfig>,
subBlocks: Record<string, SubBlockState>
): Record<string, BlockOutput> {
const resolvedOutputs: Record<string, BlockOutput> = {}
for (const [key, outputConfig] of Object.entries(outputs)) {
// If no dependencies, use the type directly
if (!outputConfig.dependsOn) {
resolvedOutputs[key] = outputConfig.type
continue
}
// Handle dependent output types
const subBlock = subBlocks[outputConfig.dependsOn.subBlockId]
resolvedOutputs[key] = isEmptyValue(subBlock?.value)
? outputConfig.dependsOn.condition.whenEmpty
: outputConfig.dependsOn.condition.whenFilled
}
return resolvedOutputs
}