Skip to content

Reduce complexity in resolveWiresAsync #73

@aarne

Description

@aarne

The resolveWiresAsync function handles multiple layers of logic (Execution, Falsy Gate, Nullish Gate, Overdefinition, Catch) in a single massive try/catch loop. This makes the cognitive load very high and testing individual fallback layers difficult.

Extract the fallback gates into a pure reducer or a pipeline pattern. By isolating the evaluation of Layer 2a (Falsy) and Layer 2b (Nullish) into helper functions, you can unit test the gate logic independently of the wire execution.

Something like this would already be a good start

async function resolveWiresAsync(
  ctx: TreeContext,
  wires: Wire[],
  pullChain?: Set<string>,
): Promise<unknown> {
  let lastError: unknown;

  for (const w of wires) {
    // 1. Pre-flight checks
    if (ctx.signal?.aborted) throw new BridgeAbortError();
    if ("value" in w) return coerceConstant(w.value);

    try {
      // 2. The Resolution Pipeline
      let value = await evaluateExecutionLayer(ctx, w, pullChain);
      
      value = await applyFalsyGate(ctx, w, value, pullChain);
      value = await applyNullishGate(ctx, w, value, pullChain);

      // 3. Overdefinition Boundary
      if (value != null) return value; 

    } catch (err: unknown) {
      // 4. Catch Gate Pipeline
      if (isFatalError(err)) throw err;
      
      const recoveredValue = await applyCatchGate(ctx, w, err, pullChain);
      if (recoveredValue != null) return recoveredValue;
      
      lastError = err;
    }
  }

  if (lastError) throw lastError;
  return undefined;
}

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions