@lytjs/reactivity 4.0.5 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# @lytjs/reactivity
|
|
2
|
+
|
|
3
|
+
Lyt.js 响应式系统 - 提供响应式数据、计算属性、侦听器等核心响应式能力。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lytjs/reactivity
|
|
9
|
+
|
|
10
|
+
# 或使用 pnpm
|
|
11
|
+
pnpm add @lytjs/reactivity
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 特性
|
|
15
|
+
|
|
16
|
+
- 🚀 Proxy 代理实现,性能优异
|
|
17
|
+
- 🔄 Signal 细粒度响应式 API
|
|
18
|
+
- 📦 完全兼容 Vue 3 响应式 API
|
|
19
|
+
- 🎯 零运行时依赖,体积仅 2.86KB (gzip)
|
|
20
|
+
|
|
21
|
+
## 快速开始
|
|
22
|
+
|
|
23
|
+
### ref - 响应式引用
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
import { ref } from '@lytjs/reactivity';
|
|
27
|
+
|
|
28
|
+
const count = ref(0);
|
|
29
|
+
console.log(count.value); // 0
|
|
30
|
+
|
|
31
|
+
count.value++;
|
|
32
|
+
console.log(count.value); // 1
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### reactive - 响应式对象
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
import { reactive } from '@lytjs/reactivity';
|
|
39
|
+
|
|
40
|
+
const state = reactive({
|
|
41
|
+
count: 0,
|
|
42
|
+
name: 'Lyt.js'
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
console.log(state.count); // 0
|
|
46
|
+
state.count++;
|
|
47
|
+
console.log(state.count); // 1
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### computed - 计算属性
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
import { ref, computed } from '@lytjs/reactivity';
|
|
54
|
+
|
|
55
|
+
const count = ref(0);
|
|
56
|
+
const doubled = computed(() => count.value * 2);
|
|
57
|
+
|
|
58
|
+
console.log(doubled.value); // 0
|
|
59
|
+
|
|
60
|
+
count.value++;
|
|
61
|
+
console.log(doubled.value); // 2
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### watch - 数据侦听
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import { ref, watch } from '@lytjs/reactivity';
|
|
68
|
+
|
|
69
|
+
const count = ref(0);
|
|
70
|
+
|
|
71
|
+
watch(count, (newValue, oldValue) => {
|
|
72
|
+
console.log(`count 变化: ${oldValue} -> ${newValue}`);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
count.value++; // count 变化: 0 -> 1
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### effect - 副作用
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
import { ref, effect } from '@lytjs/reactivity';
|
|
82
|
+
|
|
83
|
+
const count = ref(0);
|
|
84
|
+
|
|
85
|
+
effect(() => {
|
|
86
|
+
console.log(`count 当前值: ${count.value}`);
|
|
87
|
+
});
|
|
88
|
+
// count 当前值: 0
|
|
89
|
+
|
|
90
|
+
count.value++; // count 当前值: 1
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Signal API
|
|
94
|
+
|
|
95
|
+
Signal 提供细粒度响应式能力,性能更优:
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
import { signal, computed } from '@lytjs/reactivity/signal';
|
|
99
|
+
|
|
100
|
+
const count = signal(0);
|
|
101
|
+
const doubled = computed(() => count() * 2);
|
|
102
|
+
|
|
103
|
+
console.log(count()); // 0
|
|
104
|
+
console.log(doubled()); // 0
|
|
105
|
+
|
|
106
|
+
count.set(1);
|
|
107
|
+
console.log(count()); // 1
|
|
108
|
+
console.log(doubled()); // 2
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## API 参考
|
|
112
|
+
|
|
113
|
+
### 响应式 API
|
|
114
|
+
|
|
115
|
+
| API | 说明 |
|
|
116
|
+
|------|------|
|
|
117
|
+
| `ref(value)` | 创建响应式引用 |
|
|
118
|
+
| `reactive(obj)` | 创建响应式对象 |
|
|
119
|
+
| `shallowRef(value)` | 创建浅层响应式引用 |
|
|
120
|
+
| `readonly(obj)` | 创建只读响应式对象 |
|
|
121
|
+
| `toRef(obj, key)` | 将对象属性转为 ref |
|
|
122
|
+
| `toRefs(obj)` | 将对象所有属性转为 ref |
|
|
123
|
+
|
|
124
|
+
### 计算与侦听
|
|
125
|
+
|
|
126
|
+
| API | 说明 |
|
|
127
|
+
|------|------|
|
|
128
|
+
| `computed(fn)` | 创建计算属性 |
|
|
129
|
+
| `watch(source, fn, options)` | 数据变化时执行回调 |
|
|
130
|
+
| `watchEffect(fn)` | 立即执行并追踪响应式依赖 |
|
|
131
|
+
| `effect(fn, options)` | 创建副作用 |
|
|
132
|
+
| `stop(effect)` | 停止副作用 |
|
|
133
|
+
|
|
134
|
+
### 工具函数
|
|
135
|
+
|
|
136
|
+
| API | 说明 |
|
|
137
|
+
|------|------|
|
|
138
|
+
| `isRef(v)` | 判断是否为 ref |
|
|
139
|
+
| `isReactive(v)` | 判断是否为 reactive |
|
|
140
|
+
| `isReadonly(v)` | 判断是否为 readonly |
|
|
141
|
+
| `unref(v)` | 解包 ref |
|
|
142
|
+
| `nextTick(fn)` | 在下一次 DOM 更新后执行 |
|
|
143
|
+
|
|
144
|
+
## 示例
|
|
145
|
+
|
|
146
|
+
### 表单验证
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
import { reactive, computed } from '@lytjs/reactivity';
|
|
150
|
+
|
|
151
|
+
const form = reactive({
|
|
152
|
+
email: '',
|
|
153
|
+
password: ''
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const isValidEmail = computed(() => {
|
|
157
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const isValidPassword = computed(() => {
|
|
161
|
+
return form.password.length >= 6;
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const isFormValid = computed(() => {
|
|
165
|
+
return isValidEmail.value && isValidPassword.value;
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### 计数器
|
|
170
|
+
|
|
171
|
+
```javascript
|
|
172
|
+
import { ref, computed } from '@lytjs/reactivity';
|
|
173
|
+
|
|
174
|
+
const count = ref(0);
|
|
175
|
+
const doubled = computed(() => count.value * 2);
|
|
176
|
+
|
|
177
|
+
function increment() {
|
|
178
|
+
count.value++;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function decrement() {
|
|
182
|
+
count.value--;
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## 性能
|
|
187
|
+
|
|
188
|
+
- 体积:2.86 KB (ESM gzip)
|
|
189
|
+
- 零运行时依赖
|
|
190
|
+
- 原生 Proxy 实现,性能优异
|
|
191
|
+
|
|
192
|
+
## 兼容性
|
|
193
|
+
|
|
194
|
+
- Node.js >= 18.0.0
|
|
195
|
+
- Chrome 64+
|
|
196
|
+
- Firefox 63+
|
|
197
|
+
- Safari 12+
|
|
198
|
+
- Edge 79+
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
MIT
|