|
| 1 | +import { Branches, Definition, Sequence, Step } from '../model'; |
| 2 | +import { defaultResolvers } from './default-resolvers'; |
| 3 | + |
| 4 | +export interface StepChildren { |
| 5 | + type: StepChildrenType; |
| 6 | + items: Sequence | Branches; |
| 7 | +} |
| 8 | + |
| 9 | +export enum StepChildrenType { |
| 10 | + sequence = 1, |
| 11 | + branches = 2 |
| 12 | +} |
| 13 | + |
| 14 | +export type StepOrName = Step | string; |
| 15 | + |
| 16 | +export interface StepWithParentSequence { |
| 17 | + step: Step; |
| 18 | + /** |
| 19 | + * Index of the step in the parent sequence. |
| 20 | + */ |
| 21 | + index: number; |
| 22 | + parentSequence: Sequence; |
| 23 | +} |
| 24 | + |
| 25 | +type StepWithParentSequenceOrName = StepWithParentSequence | string; |
| 26 | + |
| 27 | +export type StepChildrenResolver = (step: Step) => StepChildren | null; |
| 28 | + |
| 29 | +export class DefinitionWalker { |
| 30 | + private readonly resolvers: StepChildrenResolver[]; |
| 31 | + |
| 32 | + public constructor(resolvers?: StepChildrenResolver[]) { |
| 33 | + this.resolvers = resolvers ? resolvers.concat(defaultResolvers) : defaultResolvers; |
| 34 | + } |
| 35 | + |
| 36 | + public getChildren(step: Step): StepChildren | null { |
| 37 | + const count = this.resolvers.length; |
| 38 | + for (let i = 0; i < count; i++) { |
| 39 | + const result = this.resolvers[i](step); |
| 40 | + if (result) { |
| 41 | + return result; |
| 42 | + } |
| 43 | + } |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + public getParents(definition: Definition, needle: Sequence | Step): StepOrName[] { |
| 48 | + const result: StepWithParentSequenceOrName[] = []; |
| 49 | + const searchSequence = Array.isArray(needle) ? needle : null; |
| 50 | + const searchStepId = !searchSequence ? (needle as Step).id : null; |
| 51 | + |
| 52 | + if (this.find(definition.sequence, searchSequence, searchStepId, result)) { |
| 53 | + result.reverse(); |
| 54 | + return result.map(item => { |
| 55 | + return typeof item === 'string' ? item : item.step; |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + throw new Error(searchStepId ? `Cannot get parents of step: ${searchStepId}` : 'Cannot get parents of sequence'); |
| 60 | + } |
| 61 | + |
| 62 | + public findParentSequence(definition: Definition, stepId: string): StepWithParentSequence | null { |
| 63 | + const result: StepWithParentSequenceOrName[] = []; |
| 64 | + if (this.find(definition.sequence, null, stepId, result)) { |
| 65 | + return result[0] as StepWithParentSequence; |
| 66 | + } |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + public getParentSequence(definition: Definition, stepId: string): StepWithParentSequence { |
| 71 | + const result = this.findParentSequence(definition, stepId); |
| 72 | + if (!result) { |
| 73 | + throw new Error(`Cannot find step by id: ${stepId}`); |
| 74 | + } |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + public findById(definition: Definition, stepId: string): Step | null { |
| 79 | + const result = this.findParentSequence(definition, stepId); |
| 80 | + return result ? result.step : null; |
| 81 | + } |
| 82 | + |
| 83 | + public getById(definition: Definition, stepId: string): Step { |
| 84 | + return this.getParentSequence(definition, stepId).step; |
| 85 | + } |
| 86 | + |
| 87 | + private find( |
| 88 | + sequence: Sequence, |
| 89 | + needSequence: Sequence | null, |
| 90 | + needStepId: string | null, |
| 91 | + result: StepWithParentSequenceOrName[] |
| 92 | + ): boolean { |
| 93 | + if (needSequence && sequence === needSequence) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + const count = sequence.length; |
| 97 | + for (let index = 0; index < count; index++) { |
| 98 | + const step = sequence[index]; |
| 99 | + if (needStepId && step.id === needStepId) { |
| 100 | + result.push({ step, index, parentSequence: sequence }); |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + const children = this.getChildren(step); |
| 105 | + if (children) { |
| 106 | + switch (children.type) { |
| 107 | + case StepChildrenType.sequence: |
| 108 | + { |
| 109 | + const parentSequence = children.items as Sequence; |
| 110 | + if (this.find(parentSequence, needSequence, needStepId, result)) { |
| 111 | + result.push({ step, index, parentSequence }); |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + break; |
| 116 | + |
| 117 | + case StepChildrenType.branches: |
| 118 | + { |
| 119 | + const branches = children.items as Branches; |
| 120 | + const branchNames = Object.keys(branches); |
| 121 | + for (const branchName of branchNames) { |
| 122 | + const parentSequence = branches[branchName]; |
| 123 | + if (this.find(parentSequence, needSequence, needStepId, result)) { |
| 124 | + result.push(branchName); |
| 125 | + result.push({ step, index, parentSequence }); |
| 126 | + return true; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + break; |
| 131 | + |
| 132 | + default: |
| 133 | + throw new Error(`Step children type ${children.type} is not supported`); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + return false; |
| 138 | + } |
| 139 | +} |
0 commit comments