@mondaydotcomorg/atp-compiler
Version:
Production-ready compiler for transforming async iteration patterns into resumable operations with checkpoint-based state management
47 lines • 2.08 kB
JavaScript
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 {
transformCount = 0;
batchOptimizer;
batchDetector;
batchSizeThreshold;
constructor(batchSizeThreshold = 10) {
this.batchOptimizer = new BatchOptimizer();
this.batchDetector = new BatchParallelDetector();
this.batchSizeThreshold = batchSizeThreshold;
}
transformArrayMethod(path) {
const node = path.node;
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.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);
}
doTransformToSequential(path, node, methodName, callback) {
return transformToSequential(path, node, methodName, callback, () => this.transformCount++);
}
getTransformCount() {
return this.transformCount;
}
resetTransformCount() {
this.transformCount = 0;
}
}
//# sourceMappingURL=array-transformer.js.map