@mondaydotcomorg/atp-compiler
Version:
Production-ready compiler for transforming async iteration patterns into resumable operations with checkpoint-based state management
77 lines (64 loc) • 2.13 kB
text/typescript
import * as t from '@babel/types';
import { BatchOptimizer } from './batch-optimizer.js';
import { BatchParallelDetector } from './batch-detector.js';
import { getArrayMethodName, canUseBatchParallel } from './array-transformer-utils.js';
import { transformToBatchParallel } from './array-transformer-batch.js';
import { transformToSequential } from './array-transformer-sequential.js';
export class ArrayTransformer {
private transformCount = 0;
private batchOptimizer: BatchOptimizer;
private batchDetector: BatchParallelDetector;
private batchSizeThreshold: number;
constructor(batchSizeThreshold: number = 10) {
this.batchOptimizer = new BatchOptimizer();
this.batchDetector = new BatchParallelDetector();
this.batchSizeThreshold = batchSizeThreshold;
}
transformArrayMethod(path: any): boolean {
const node = path.node as t.CallExpression;
const methodName = getArrayMethodName(node);
if (!methodName) {
return false;
}
const callback = node.arguments[0];
if (!callback || !t.isFunction(callback) || !callback.async) {
return false;
}
const batchResult = this.batchOptimizer.canBatchArrayMethod(callback);
if (batchResult.canBatch && canUseBatchParallel(methodName)) {
const array = (node.callee as t.MemberExpression).object;
const decision = this.batchOptimizer.makeSmartBatchDecision(
methodName,
batchResult,
array,
this.batchSizeThreshold
);
if (decision.shouldBatch) {
return transformToBatchParallel(
path,
node,
methodName,
callback,
this.batchDetector,
() => this.transformCount++,
() => this.doTransformToSequential(path, node, methodName, callback)
);
}
}
return this.doTransformToSequential(path, node, methodName, callback);
}
private doTransformToSequential(
path: any,
node: t.CallExpression,
methodName: string,
callback: t.Function
): boolean {
return transformToSequential(path, node, methodName, callback, () => this.transformCount++);
}
getTransformCount(): number {
return this.transformCount;
}
resetTransformCount(): void {
this.transformCount = 0;
}
}