/
h0tnanny
/
IotPlatform
Обзор
Документация
Войти
/
h0tnanny
/
IotPlatform
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/workflow/nodes/LoopNode.ts
95 строк
3 KB
h0tnanny
исправление багов
07 фев 2026, 00:39
07 фев 2026, 00:39
a2638cd
Код
Авторство
О чём код?
import { BaseFunction, ExecutionContext } from '../BaseFunction'; import { Field } from '../../entities/Field'; import { ComparisonOperator } from './ConditionNode'; /** * LoopNode (While) - проверяет условие цикла * Если true - возвращает ID тела цикла, если false - ID узла после цикла */ export class LoopNode extends BaseFunction { private iterationCount = 0; public readonly maxIterations: number; constructor( id: string, name: string, public readonly fieldName: string, public readonly operator: ComparisonOperator, public readonly compareValue: unknown, public readonly bodyNodeId: string, public readonly exitNodeId: string | null, maxIterations: number = 1000 ) { super(id, name); this.maxIterations = maxIterations; } execute(context: ExecutionContext): string | null { if (this.iterationCount >= this.maxIterations) { this.iterationCount = 0; return this.exitNodeId; } const field = context.variableList.get(this.fieldName); if (!field) { return this.exitNodeId; } // Резолвим compareValue: если это имя переменной — берём её значение const resolvedValue = this.resolveCompareValue(context); const conditionResult = this.compare(field, resolvedValue); if (conditionResult) { this.iterationCount++; return this.bodyNodeId; } else { this.iterationCount = 0; return this.exitNodeId; } } /** * Резолвит compareValue: если строка совпадает с именем переменной — возвращает значение переменной, * иначе возвращает compareValue как есть (статическое значение) */ private resolveCompareValue(context: ExecutionContext): unknown { if (typeof this.compareValue === 'string') { const compareField = context.variableList.get(this.compareValue); if (compareField) { return compareField.value; } } return this.compareValue; } /** * Выполняет сравнение значения поля с эталонным значением */ private compare(field: Field, compareValue: unknown): boolean { const fieldValue = field.value; switch (this.operator) { case '==': return fieldValue == compareValue; case '!=': return fieldValue != compareValue; case '>': return Number(fieldValue) > Number(compareValue); case '<': return Number(fieldValue) < Number(compareValue); case '>=': return Number(fieldValue) >= Number(compareValue); case '<=': return Number(fieldValue) <= Number(compareValue); default: return false; } } /** * Сбрасывает счетчик итераций (вызывается при новом запуске workflow) */ reset(): void { this.iterationCount = 0; } }