| 26 | const FOR_EACH_LOOP_CONTEXT_FIELDS = ['index', 'currentItem', 'items'] as const |
| 27 | |
| 28 | export class LoopResolver implements Resolver { |
| 29 | private loopNameToId: Map<string, string> |
| 30 | |
| 31 | constructor( |
| 32 | private workflow: SerializedWorkflow, |
| 33 | private navigatePathAsync?: AsyncPathNavigator |
| 34 | ) { |
| 35 | this.loopNameToId = new Map() |
| 36 | for (const block of workflow.blocks) { |
| 37 | if (workflow.loops[block.id] && block.metadata?.name) { |
| 38 | this.loopNameToId.set(normalizeName(block.metadata.name), block.id) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | private static OUTPUT_PROPERTIES = new Set(['result', 'results']) |
| 44 | private static KNOWN_PROPERTIES = new Set(['iteration', 'index', 'item', 'currentItem', 'items']) |
| 45 | |
| 46 | canResolve(reference: string): boolean { |
| 47 | if (!isReference(reference)) { |
| 48 | return false |
| 49 | } |
| 50 | const parts = parseReferencePath(reference) |
| 51 | if (parts.length === 0) { |
| 52 | return false |
| 53 | } |
| 54 | const [type] = parts |
| 55 | return type === REFERENCE.PREFIX.LOOP || this.loopNameToId.has(type) |
| 56 | } |
| 57 | |
| 58 | resolve(reference: string, context: ResolutionContext): any { |
| 59 | return this.resolveInternal(reference, context, false) |
| 60 | } |
| 61 | |
| 62 | async resolveAsync(reference: string, context: ResolutionContext): Promise<any> { |
| 63 | if (!this.navigatePathAsync) { |
| 64 | return this.resolve(reference, context) |
| 65 | } |
| 66 | return this.resolveInternal(reference, context, true) |
| 67 | } |
| 68 | |
| 69 | private async resolveInternal( |
| 70 | reference: string, |
| 71 | context: ResolutionContext, |
| 72 | useAsyncPath: true |
| 73 | ): Promise<any> |
| 74 | private resolveInternal(reference: string, context: ResolutionContext, useAsyncPath: false): any |
| 75 | private resolveInternal( |
| 76 | reference: string, |
| 77 | context: ResolutionContext, |
| 78 | useAsyncPath: boolean |
| 79 | ): any | Promise<any> { |
| 80 | const parts = parseReferencePath(reference) |
| 81 | if (parts.length === 0) { |
| 82 | logger.warn('Invalid loop reference', { reference }) |
| 83 | return undefined |
| 84 | } |
| 85 |
nothing calls this directly
no outgoing calls
no test coverage detected