transformInstructionPlan

function transformInstructionPlan(instructionPlan, fn): InstructionPlan;

Transforms an instruction plan tree using a bottom-up approach.

This function recursively traverses the instruction plan tree, applying the transformation function to each plan. The transformation is applied bottom-up, meaning nested plans are transformed first, then the parent plans receive the already-transformed children before being transformed themselves.

All transformed plans are frozen using Object.freeze to ensure immutability.

Parameters

ParameterTypeDescription
instructionPlanInstructionPlanThe instruction plan tree to transform.
fn(plan) => InstructionPlanA function that transforms each plan and returns a new plan.

Returns

InstructionPlan

A new transformed instruction plan tree.

Examples

Making all sequential plans non-divisible to ensure atomicity.

const plan = sequentialInstructionPlan([instructionA, instructionB]);
 
const transformed = transformInstructionPlan(plan, (p) => {
  if (p.kind === 'sequential' && p.divisible) {
    return nonDivisibleSequentialInstructionPlan(p.plans);
  }
  return p;
});

Filtering out debug instructions before production execution.

const plan = sequentialInstructionPlan([instructionA, debugInstruction, instructionB]);
 
const transformed = transformInstructionPlan(plan, (p) => {
  if (p.kind === 'sequential' || p.kind === 'parallel') {
    return { ...p, plans: p.plans.filter((p) => !isDebugInstruction(p)) };
  }
  return p;
});

See

On this page