Closed
Description
Context
From PR #246 code review - the outdentNode() operation has high cyclomatic complexity and would benefit from extracting helper functions to improve maintainability.
Current State (Updated Assessment)
The outdentNode() operation in reactive-node-service.svelte.ts (lines 827-1009) is 183 lines. After architectural changes since this issue was created:
- The function is functionally correct with all tests passing
- Code is well-commented with clear phase separation
- Backend now handles ordering via fractional ordering (simplification)
- ReactiveStructureTree is the single source of truth for hierarchy
Revised Approach: Light Refactoring
Instead of aggressive extraction (which adds indirection risk), we'll do a targeted refactoring that:
- Extracts meaningful, reusable helpers - Only where there's clear benefit
- Improves type safety - Add result types for validation
- Maintains readability - Keep related logic together
- Preserves behavior - No functional changes
Helpers to Extract
1. Validation Helper
type OutdentValidationResult = {
node: Node;
oldParentId: string;
newParentId: string | null;
siblingsBelow: string[];
newDepth: number;
};
function validateOutdent(nodeId: string): OutdentValidationResult | nullConsolidates all pre-condition checks and data gathering into one place.
2. Rollback Helper
function rollbackOutdentChanges(
nodeId: string,
originalUIState: NodeUIState,
originalRootNodeIds: string[],
siblingsBelow: string[],
oldParentId: string,
newParentId: string | null
): voidIsolates rollback logic for reuse and clarity.
What We're NOT Extracting
- Optimistic UI updates - Tightly coupled to local state, extracting adds complexity
- Backend operation - Already in an IIFE, extracting gains nothing
- Sibling transfer loop - Clear inline, extracting fragments the logic
Acceptance Criteria (Revised)
- Extract validation helper (
validateOutdent) - Extract rollback helper (
rollbackOutdentChanges) - Add
OutdentValidationResulttype for type safety - Main
outdentNode()function reduced to ~100-120 lines (realistic target) - All existing tests continue to pass
- No functional changes - pure refactoring
Benefits
- Better testability: Validation logic can be unit tested independently
- Cleaner error handling: Rollback is isolated and reusable
- Type safety: Result type documents the validation contract
- Maintainability: Clear separation without over-abstraction
Implementation Notes
- Preserve Pattern A: All helpers use declarative dependencies
- No behavioral changes: Functionality remains identical
- Keep DEV assertions: Maintain existing runtime validation
Related
- PR Fix Sibling Chain Integrity for Remaining Operations (deleteNode, indentNode, outdentNode) #246 - Sibling chain integrity improvements
- Issue Fix Sibling Chain Integrity for Remaining Operations (deleteNode, indentNode, outdentNode) #246 - Original sibling chain integrity work
Priority
Low - Code quality improvement, not blocking. Light touch approach.
Activity
malibio commentedon Dec 4, 2025
Test Baseline:
Starting refactoring work.
Refactor outdentNode() with validation and rollback helpers (#256)
Refactor outdentNode() with validation and rollback helpers (#731)
Refactor outdentNode() with validation and rollback helpers (#731)
Refactor outdentNode() with validation and rollback helpers (#731)
Refactor outdentNode() with validation and rollback helpers (#731)