Comfy-Org / Comfy-Org/ComfyUI_frontend

Refactor `pruneLinearData` in appModeStore: separate normalization from pruning

Open
#11,430 0 comments 0 reactions 1 assignee Claimed by @christian-byrne View on GitHub
developer experience
Dominant language
TypeScript
Stars
2k
Forks
699
Avg merge
1d 7h
Merged PRs (30d)
490

Description

## Summary

The `pruneLinearData` function in `src/stores/appModeStore.ts` conflates two distinct responsibilities:

1. **Normalization / coercion** – it accepts `Partial | undefined` and converts missing or `null` properties (e.g. `data.inputs`, `data.outputs`) to empty arrays, returning a fully-shaped `LinearData` object.
2. **Pruning** – it filters out entries that reference nodes no longer present in the graph.

This dual responsibility creates several maintenance risks:

- **Null-vs-not-null semantics confusion** – downstream callers may need to distinguish between "no inputs were ever set" (`null`/`undefined`) and "inputs were set but are now empty" (`[]`). Silently coercing both to `[]` inside a function named "prune" hides this distinction.
- **Type inference coupled to pruning** – if a future refactor removes the pruning step from a code path, callers that relied on `pruneLinearData` to get a `LinearData` (non-Partial) type will surface a type error. The fix path may be non-obvious and could lead to re-introducing the coercion in an ad-hoc way.
- **Misleading name** – the name implies only removal/filtering; a reader has no reason to expect that calling the function changes the shape of the data.

## Suggested Refactor

Split into two focused utilities:

```ts
/** Coerces a partial LinearData into a full LinearData, filling missing fields with empty arrays. */
function normalizeLinearData(data: Partial | undefined): LinearData {
return {
inputs: data?.inputs ?? [],
outputs: data?.outputs ?? []
}
}

/** Removes entries that reference nodes no longer present in the graph. */
function pruneLinearData(data: LinearData): LinearData {
if (!app.rootGraph || ChangeTracker.isLoadingGraph) return data
return {
inputs: data.inputs.filter(([nodeId]) => resolveNode(nodeId)),
outputs: data.outputs.filter((nodeId) => resolveNode(nodeId))
}
}
```

Call sites can then compose them explicitly:
```ts
const normalized = normalizeLinearData(source)
const pruned = pruneLinearData(normalized)
```

This makes the intent clear at each call site and removes the implicit coupling between type coercion and graph-state validation.

## References

- File: `src/stores/appModeStore.ts` (lines 49–62)
- Raised in: https://github.com/Comfy-Org/ComfyUI_frontend/pull/11422#discussion_r3107631170
- Requested by: @christian-byrne

┆Issue is synchronized with this [Notion page](https://www.notion.so/Issue-11430-Refactor-pruneLinearData-in-appModeStore-separate-normalization-from-pruning-3476d73d3650813ca8ecc1c957deac51) by [Unito](https://www.unito.io)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.