Skip to content

Commit 5095600

Browse files
committed
Add development notes for reactive implementation
Added comprehensive documentation of the design and implementation process: 1. **Discussion Summary** (01-discussion-summary.md) - Initial requirements and goals - Naming evolution (MVVC → MVVM → reactive) - Key discussion points and decisions - Implementation phases - Future enhancement possibilities 2. **Design Decisions** (02-design-decisions.md) - Why develop ldreactive as independent library - API parameter design rationale - Exclusion mechanism choices - Dependency tracking granularity - Event system design - Internal state organization - Proxy caching strategy - Array handling approach - ctx function handling - ldview integration strategy - Backward compatibility guarantees 3. **Implementation Notes** (03-implementation-notes.md) - File structure and organization - Core implementation details - ldview integration modifications - Test implementation (23 tests total) - Build system updates - Performance considerations - Known limitations - Development timeline - Future expansion possibilities These notes serve as: - Reference for future development - Documentation of design rationale - Knowledge transfer for team members - Historical record of the implementation process 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 0be8a11 commit 5095600

File tree

3 files changed

+1055
-0
lines changed

3 files changed

+1055
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# 討論摘要 - Reactive 機制實作
2+
3+
**日期**: 2025-10-21
4+
**主題**: 為 ldview 實現響應式數據綁定機制
5+
**分支**: `claude/implement-mvvc-mechanism-011CUKbTLJ4r7Fz5duho22PU`
6+
7+
## 需求起源
8+
9+
使用者希望在 ldview 中實現類似 MVVM 的機制,讓各個 ldselector 對應的 render handler 可以在其所使用的資料變更時自動更新。
10+
11+
## 討論流程
12+
13+
### 1. 理解現有架構 (第一階段)
14+
15+
- **任務**: 深入了解 ldview 的程式碼結構和設計邏輯
16+
- **方法**: 使用 Task/Explore agent 全面分析 codebase
17+
- **成果**: 建立完整的架構文檔,理解:
18+
- ldview 的核心概念(ld selector、handler、context)
19+
- 現有的手動更新流程
20+
- 節點追蹤和渲染機制
21+
- 已有的基礎設施(事件系統、Promise 支援等)
22+
23+
### 2. 命名演變
24+
25+
- **初始**: MVVC (誤植)
26+
- **修正**: MVVM (Model-View-ViewModel)
27+
- **最終**: **reactive** (響應式)
28+
- 理由:更直觀、符合現代前端框架慣用術語
29+
30+
### 3. 設計方向討論 (第二階段)
31+
32+
#### 關於 ctx 的彈性問題
33+
34+
使用者提出重要觀察:
35+
- ctx 可能是物件、函式、甚至純量
36+
- 不應該污染使用者的資料結構
37+
- 需要提供開關 reactive 機制的彈性
38+
39+
#### 初步方案探討
40+
41+
討論了幾種方案:
42+
1. **自動偵測** - 對最終得到的物件做 reactive
43+
2. **顯式標記** - 使用特殊欄位標記(被否決,會污染資料)
44+
3. **包裝物件** - 使用特殊建構子包裝(最終採用)
45+
46+
### 4. API 設計確定 (第三階段)
47+
48+
#### 核心決策
49+
50+
1. **獨立套件**: 開發獨立的 ldreactive,但先寄生在 ldview 專案中
51+
2. **命名約定**:
52+
- 使用 `ldview.reactive` 而非 `ldview.reactive()`
53+
- 支援 `new ldreactive()``ldreactive.create()`
54+
3. **參數設計**:
55+
- 雙參數形式:`ldreactive(data, options)`
56+
- 第一參數永遠是資料
57+
- 第二參數是選項物件(exclude 等)
58+
- 支援空建構,之後用 `set()` 設定
59+
60+
#### 重要設計原則
61+
62+
1. **不污染資料**:
63+
- 不顯式標記屬性
64+
- 改用排除名單機制
65+
66+
2. **遵循現有風格**:
67+
- LiveScript + prototype(不用 class)
68+
- 事件處理用固定寫法:
69+
```livescript
70+
on: (n, cb) -> (if Array.isArray(n) => n else [n]).map (n) ~> @evthdr.[][n].push cb
71+
fire: (n, ...v) -> for cb in (@evthdr[n] or []) => cb.apply @, v
72+
```
73+
- 內部變數統一存在 `@_` 下
74+
75+
3. **深度追蹤**:
76+
- 追蹤完整路徑(如 `user.profile.name`)
77+
- 而非只追蹤頂層(`user`)
78+
79+
4. **函式處理**:
80+
- 函式屬性自動不追蹤
81+
- 函式回傳值的追蹤問題留待未來處理(需要更深層的 reactive 系統)
82+
83+
### 5. 實作執行 (第四階段)
84+
85+
按照以下順序實作:
86+
1. ✅ 建立 `src/ldreactive.ls` - 獨立 reactive 庫
87+
2. ✅ 整合到 `src/ldview.ls`
88+
3. ✅ 更新 build 腳本
89+
4. ✅ 編寫測試(13 個單元測試 + 10 個整合測試)
90+
5. ✅ 撰寫文檔(REACTIVE.md)
91+
6. ✅ 提交並推送代碼
92+
93+
## 關鍵技術點
94+
95+
1. **ES6 Proxy**: 用於攔截屬性存取和修改
96+
2. **WeakMap**: 快取 proxy 物件,避免重複建立
97+
3. **依賴追蹤**: 在 handler 執行時記錄存取的屬性
98+
4. **批次更新**: 避免頻繁觸發渲染
99+
5. **深度代理**: 遞迴建立 nested object 的 proxy
100+
101+
## 成果
102+
103+
- **ldreactive**: 完整的獨立響應式庫(208 行 LiveScript)
104+
- **ldview 整合**: 無縫整合,向後兼容
105+
- **測試覆蓋**: 23 個測試全部通過
106+
- **文檔**: 完整的中文使用手冊
107+
- **範例**: HTML 互動式測試頁面
108+
109+
## 討論特色
110+
111+
1. **迭代式設計**: 通過多輪討論不斷優化 API
112+
2. **尊重現有慣例**: 完全遵循 LiveScript 和專案風格
113+
3. **實用主義**: 優先實現核心功能,複雜功能留待未來
114+
4. **完整交付**: 包含代碼、測試、文檔、範例
115+
116+
## 未來可能的擴展
117+
118+
1. **獨立發布**: 將 ldreactive 發布為 `@loadingio/ldreactive`
119+
2. **Computed 屬性**: 支援計算屬性
120+
3. **Deep watch**: 深度監聽整個物件樹
121+
4. **函式追蹤**: 追蹤函式內部的屬性存取
122+
5. **更好的 diff**: 優化 ld-each 的更新效能

0 commit comments

Comments
 (0)