|
1 | 1 | import type { DefineExpectedValchecker, DefineStepMethod, DefineStepMethodMeta, ExecutionIssue, ExecutionResult, InferAsync, InferIssue, InferOutput, MessageHandler, Next, TStepPluginDef, Use, Valchecker } from '../../core' |
2 | 2 | import type { IsExactlyAnyOrUnknown } from '../../shared' |
3 | 3 | import { implStepPlugin } from '../../core' |
4 | | -import { Pipe } from '../../shared' |
5 | 4 |
|
6 | 5 | type Meta = DefineStepMethodMeta<{ |
7 | 6 | Name: 'array' |
@@ -72,30 +71,48 @@ export const array = implStepPlugin<PluginDef>({ |
72 | 71 | }) |
73 | 72 | } |
74 | 73 |
|
75 | | - const pipe = new Pipe<void>() |
| 74 | + // Optimized: Direct processing without Pipe overhead |
76 | 75 | const issues: ExecutionIssue[] = [] |
77 | | - const output = [...value] |
| 76 | + const len = value.length |
| 77 | + const output = Array.from({ length: len }) |
| 78 | + |
78 | 79 | const processItemResult = (result: ExecutionResult, i: number) => { |
79 | | - if (isFailure(result)) |
80 | | - issues.push(...result.issues.map(issue => prependIssuePath(issue, [i]))) |
81 | | - else |
| 80 | + if (isFailure(result)) { |
| 81 | + // Optimize: Avoid spread and map by using direct loop |
| 82 | + for (const issue of result.issues) { |
| 83 | + issues.push(prependIssuePath(issue, [i])) |
| 84 | + } |
| 85 | + } |
| 86 | + else { |
82 | 87 | output[i] = result.value |
| 88 | + } |
83 | 89 | } |
84 | | - for (let i = 0; i < value.length; i++) { |
| 90 | + |
| 91 | + // Process items synchronously until we hit async |
| 92 | + for (let i = 0; i < len; i++) { |
85 | 93 | const itemValue = value[i]! |
86 | | - pipe.add(() => { |
87 | | - const itemResult = item['~execute'](itemValue) |
88 | | - return itemResult instanceof Promise |
89 | | - ? itemResult.then(r => processItemResult(r, i)) |
90 | | - : processItemResult(itemResult, i) |
91 | | - }) |
| 94 | + const itemResult = item['~execute'](itemValue) |
| 95 | + |
| 96 | + if (itemResult instanceof Promise) { |
| 97 | + // Hit async, chain remaining items |
| 98 | + let chain = itemResult.then(r => processItemResult(r, i)) |
| 99 | + for (let j = i + 1; j < len; j++) { |
| 100 | + const jValue = value[j]! |
| 101 | + const jIndex = j |
| 102 | + chain = chain.then(() => { |
| 103 | + const jResult = item['~execute'](jValue) |
| 104 | + return jResult instanceof Promise |
| 105 | + ? jResult.then(r => processItemResult(r, jIndex)) |
| 106 | + : (processItemResult(jResult, jIndex), undefined) |
| 107 | + }) |
| 108 | + } |
| 109 | + return chain.then(() => issues.length > 0 ? failure(issues) : success(output)) |
| 110 | + } |
| 111 | + |
| 112 | + processItemResult(itemResult, i) |
92 | 113 | } |
93 | 114 |
|
94 | | - const processResult = () => issues.length > 0 ? failure(issues) : success(output) |
95 | | - const result = pipe.exec() |
96 | | - return result instanceof Promise |
97 | | - ? result.then(processResult) |
98 | | - : processResult() |
| 115 | + return issues.length > 0 ? failure(issues) : success(output) |
99 | 116 | }) |
100 | 117 | }, |
101 | 118 | }) |
0 commit comments