|
| 1 | +# Final Simplification Summary |
| 2 | + |
| 3 | +## The Core Problem |
| 4 | + |
| 5 | +The current code has **complex configuration-based behavior** that makes it hard to understand and maintain: |
| 6 | + |
| 7 | +```typescript |
| 8 | +// Current: Confusing branching logic |
| 9 | +if (this.visibilityDetector.config.enabled) { |
| 10 | + if (this.taskScheduler.config.enabled) { |
| 11 | + // Path 1: async with batch |
| 12 | + } else { |
| 13 | + // Path 2: sync with batch |
| 14 | + } |
| 15 | +} else { |
| 16 | + if (this.taskScheduler.config.enabled) { |
| 17 | + // Path 3: async with chunks |
| 18 | + } else { |
| 19 | + // Path 4: sync with chunks |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +## The Solution: Explicit Async/Sync Methods |
| 25 | + |
| 26 | +Instead of configuration flags, use clear method names: |
| 27 | + |
| 28 | +```typescript |
| 29 | +// Proposed: Clear, explicit API |
| 30 | +await pangu.spacingPage(); // Always async (uses requestIdleCallback) |
| 31 | +pangu.spacingPageSync(); // Always sync (immediate processing) |
| 32 | +``` |
| 33 | + |
| 34 | +## Benefits of Simplification |
| 35 | + |
| 36 | +### 1. **Remove Configuration Complexity** |
| 37 | +- Delete `taskScheduler.config.enabled` entirely |
| 38 | +- Behavior is determined by method name, not runtime config |
| 39 | +- No more guessing what `pangu.spacingPage()` does |
| 40 | + |
| 41 | +### 2. **Simplify Implementation** |
| 42 | +```typescript |
| 43 | +// Before: Complex branching |
| 44 | +spacingNode(node: Node) { |
| 45 | + const textNodes = collectTextNodes(node); |
| 46 | + if (this.taskScheduler.config.enabled) { |
| 47 | + this.spacingTextNodesInQueue(textNodes); |
| 48 | + } else { |
| 49 | + this.spacingTextNodes(textNodes); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// After: Two clear methods |
| 54 | +async spacingNode(node: Node): Promise<void> { |
| 55 | + const textNodes = collectTextNodes(node); |
| 56 | + await this.processAsync(textNodes); |
| 57 | +} |
| 58 | + |
| 59 | +spacingNodeSync(node: Node): void { |
| 60 | + const textNodes = collectTextNodes(node); |
| 61 | + this.processSync(textNodes); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### 3. **Better TypeScript Support** |
| 66 | +```typescript |
| 67 | +// TypeScript knows exactly what each method returns |
| 68 | +const result1 = await pangu.spacingPage(); // Promise<void> |
| 69 | +const result2 = pangu.spacingPageSync(); // void |
| 70 | + |
| 71 | +// Error handling is clearer |
| 72 | +try { |
| 73 | + await pangu.spacingPage(); |
| 74 | +} catch (error) { |
| 75 | + // Handle async errors properly |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### 4. **Simpler Mental Model** |
| 80 | + |
| 81 | +Current approach requires understanding: |
| 82 | +- What `taskScheduler.config.enabled` does |
| 83 | +- What `visibilityDetector.config.enabled` does |
| 84 | +- How they interact (4 different paths!) |
| 85 | + |
| 86 | +New approach: |
| 87 | +- `spacingPage()` = async (non-blocking) |
| 88 | +- `spacingPageSync()` = sync (blocking) |
| 89 | +- That's it! |
| 90 | + |
| 91 | +## Implementation Strategy |
| 92 | + |
| 93 | +### Phase 1: Add New Methods |
| 94 | +```typescript |
| 95 | +export class BrowserPangu extends Pangu { |
| 96 | + // Async versions (use requestIdleCallback) |
| 97 | + async spacingPage(): Promise<void> |
| 98 | + async spacingNode(node: Node): Promise<void> |
| 99 | + async autoSpacingPage(config?: Config): Promise<void> |
| 100 | + |
| 101 | + // Sync versions (immediate processing) |
| 102 | + spacingPageSync(): void |
| 103 | + spacingNodeSync(node: Node): void |
| 104 | + autoSpacingPageSync(config?: Config): void |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Phase 2: Simplify Internals |
| 109 | +- Remove `spacingTextNodesInQueue()` complexity |
| 110 | +- Remove `TaskScheduler` configuration |
| 111 | +- Use simple Promise-based idle processing |
| 112 | + |
| 113 | +### Phase 3: Update Public API |
| 114 | +```typescript |
| 115 | +// All the convenience methods get async/sync versions |
| 116 | +async spacingElementById(id: string): Promise<void> |
| 117 | +spacingElementByIdSync(id: string): void |
| 118 | + |
| 119 | +async spacingElementsByClassName(className: string): Promise<void> |
| 120 | +spacingElementsByClassNameSync(className: string): void |
| 121 | +``` |
| 122 | + |
| 123 | +## Real-World Usage |
| 124 | + |
| 125 | +```typescript |
| 126 | +// Chrome Extension - Use async to avoid blocking UI |
| 127 | +button.addEventListener('click', async () => { |
| 128 | + button.disabled = true; |
| 129 | + try { |
| 130 | + await pangu.spacingPage(); |
| 131 | + showSuccessMessage(); |
| 132 | + } finally { |
| 133 | + button.disabled = false; |
| 134 | + } |
| 135 | +}); |
| 136 | + |
| 137 | +// Real-time editor - Use sync for immediate feedback |
| 138 | +editor.addEventListener('input', () => { |
| 139 | + pangu.spacingNodeSync(editor); |
| 140 | +}); |
| 141 | + |
| 142 | +// Initial page load - Use async |
| 143 | +window.addEventListener('load', async () => { |
| 144 | + await pangu.autoSpacingPage(); |
| 145 | +}); |
| 146 | +``` |
| 147 | + |
| 148 | +## Code Reduction Estimate |
| 149 | + |
| 150 | +- **Remove**: ~200 lines of configuration logic |
| 151 | +- **Remove**: ~100 lines of branching conditions |
| 152 | +- **Remove**: Complex callback management |
| 153 | +- **Add**: ~50 lines for explicit async/sync methods |
| 154 | +- **Net reduction**: ~250 lines (>20% of codebase) |
| 155 | + |
| 156 | +## Summary |
| 157 | + |
| 158 | +By separating async and sync methods explicitly, we: |
| 159 | +1. Make the API self-documenting |
| 160 | +2. Remove complex configuration dependencies |
| 161 | +3. Simplify the implementation dramatically |
| 162 | +4. Improve type safety and error handling |
| 163 | +5. Make the library easier to understand and maintain |
| 164 | + |
| 165 | +The key insight: **Configuration flags that change fundamental behavior should be replaced with explicit methods**. |
0 commit comments