@e7w/easy-model 0.1.7 → 0.1.9
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/LICENSE.md +9 -9
- package/README.md +298 -530
- package/dist/index.d.ts +5 -6
- package/dist/index.es.js +345 -274
- package/dist/index.umd.js +4 -4
- package/package.json +32 -22
package/README.md
CHANGED
|
@@ -1,530 +1,298 @@
|
|
|
1
|
-
# easy-model
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
- `
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
-
|
|
244
|
-
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
-
|
|
253
|
-
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
**返回值:** 方法装饰器
|
|
300
|
-
|
|
301
|
-
#### `loader.once()`
|
|
302
|
-
|
|
303
|
-
确保异步方法只执行一次的装饰器。
|
|
304
|
-
|
|
305
|
-
**返回值:** 方法装饰器
|
|
306
|
-
|
|
307
|
-
#### `loader.isLoading(target: Function): boolean`
|
|
308
|
-
|
|
309
|
-
检查指定方法是否正在加载中。
|
|
310
|
-
|
|
311
|
-
**参数:**
|
|
312
|
-
|
|
313
|
-
- `target: Function` - 要检查的方法
|
|
314
|
-
|
|
315
|
-
**返回值:** 是否正在加载
|
|
316
|
-
|
|
317
|
-
#### `loader.isGlobalLoading: boolean`
|
|
318
|
-
|
|
319
|
-
获取全局加载状态。
|
|
320
|
-
|
|
321
|
-
**返回值:** 是否有全局加载正在进行
|
|
322
|
-
|
|
323
|
-
#### `loader.globalLoading: number`
|
|
324
|
-
|
|
325
|
-
获取当前全局加载的数量。
|
|
326
|
-
|
|
327
|
-
**返回值:** 正在进行的全局加载数量
|
|
328
|
-
|
|
329
|
-
#### `finalizationRegistry(model: object)`
|
|
330
|
-
|
|
331
|
-
为模型注册垃圾回收回调。
|
|
332
|
-
|
|
333
|
-
**参数:**
|
|
334
|
-
|
|
335
|
-
- `model: object` - 要监听垃圾回收的对象
|
|
336
|
-
|
|
337
|
-
**返回值:** 包含 `register` 和 `unregister` 方法的对象
|
|
338
|
-
|
|
339
|
-
## 高级用法
|
|
340
|
-
|
|
341
|
-
### 组件间通信
|
|
342
|
-
|
|
343
|
-
```tsx
|
|
344
|
-
// 全局状态模型
|
|
345
|
-
class AppState {
|
|
346
|
-
user: User | null = null;
|
|
347
|
-
theme: "light" | "dark" = "light";
|
|
348
|
-
|
|
349
|
-
setUser(user: User) {
|
|
350
|
-
this.user = user;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
toggleTheme() {
|
|
354
|
-
this.theme = this.theme === "light" ? "dark" : "light";
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
// 在任何组件中使用
|
|
359
|
-
function Header() {
|
|
360
|
-
const { user, theme, toggleTheme } = useModel(AppState, []);
|
|
361
|
-
|
|
362
|
-
return (
|
|
363
|
-
<header className={theme}>
|
|
364
|
-
<span>欢迎, {user?.name}</span>
|
|
365
|
-
<button onClick={toggleTheme}>切换主题</button>
|
|
366
|
-
</header>
|
|
367
|
-
);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
function Sidebar() {
|
|
371
|
-
const { user } = useModel(AppState, []); // 同一个实例
|
|
372
|
-
|
|
373
|
-
return <aside>{user && <UserProfile user={user} />}</aside>;
|
|
374
|
-
}
|
|
375
|
-
```
|
|
376
|
-
|
|
377
|
-
### 嵌套模型
|
|
378
|
-
|
|
379
|
-
```tsx
|
|
380
|
-
class UserModel {
|
|
381
|
-
constructor(public id: string) {}
|
|
382
|
-
|
|
383
|
-
name = "";
|
|
384
|
-
email = "";
|
|
385
|
-
|
|
386
|
-
updateProfile(data: Partial<UserModel>) {
|
|
387
|
-
Object.assign(this, data);
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
const User = provide(UserModel);
|
|
392
|
-
|
|
393
|
-
class TeamModel {
|
|
394
|
-
members: UserModel[] = [];
|
|
395
|
-
|
|
396
|
-
addMember(userId: string) {
|
|
397
|
-
const user = User(userId);
|
|
398
|
-
this.members.push(user);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
removeMember(userId: string) {
|
|
402
|
-
this.members = this.members.filter(m => m.id !== userId);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
function TeamManagement() {
|
|
407
|
-
const team = useModel(TeamModel, ["team-1"]);
|
|
408
|
-
|
|
409
|
-
return (
|
|
410
|
-
<div>
|
|
411
|
-
<h2>团队成员</h2>
|
|
412
|
-
{team.members.map(member => (
|
|
413
|
-
<UserCard key={member.id} id={member.id} />
|
|
414
|
-
))}
|
|
415
|
-
</div>
|
|
416
|
-
);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
function UserCard({ id }: { id: UserModel["id"] }) {
|
|
420
|
-
const observedUser = useModel(User, [id]);
|
|
421
|
-
|
|
422
|
-
return (
|
|
423
|
-
<div>
|
|
424
|
-
<span>{observedUser.name}</span>
|
|
425
|
-
<span>{observedUser.email}</span>
|
|
426
|
-
</div>
|
|
427
|
-
);
|
|
428
|
-
}
|
|
429
|
-
```
|
|
430
|
-
|
|
431
|
-
### 命名空间隔离
|
|
432
|
-
|
|
433
|
-
```tsx
|
|
434
|
-
function App() {
|
|
435
|
-
return (
|
|
436
|
-
<div>
|
|
437
|
-
<Container namespace="dev">
|
|
438
|
-
<VInjection schema={configSchema} val={devConfig} />
|
|
439
|
-
</Container>
|
|
440
|
-
|
|
441
|
-
<Container namespace="prod">
|
|
442
|
-
<VInjection schema={configSchema} val={prodConfig} />
|
|
443
|
-
</Container>
|
|
444
|
-
</div>
|
|
445
|
-
);
|
|
446
|
-
}
|
|
447
|
-
```
|
|
448
|
-
|
|
449
|
-
## 最佳实践
|
|
450
|
-
|
|
451
|
-
### 1. 模型设计
|
|
452
|
-
|
|
453
|
-
- **单一职责**: 每个模型类应该只负责一个特定的业务领域
|
|
454
|
-
- **类型安全**: 充分利用 TypeScript 的类型系统
|
|
455
|
-
|
|
456
|
-
```tsx
|
|
457
|
-
// ✅ 好的实践
|
|
458
|
-
class UserModel {
|
|
459
|
-
constructor(public id: string) {}
|
|
460
|
-
|
|
461
|
-
private _profile: UserProfile | null = null;
|
|
462
|
-
|
|
463
|
-
get profile() {
|
|
464
|
-
return this._profile;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
updateProfile(updates: Partial<UserProfile>) {
|
|
468
|
-
this._profile = { ...this._profile, ...updates };
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
// ❌ 避免的实践
|
|
473
|
-
class BadModel {
|
|
474
|
-
// 避免在模型中直接操作 DOM
|
|
475
|
-
updateUI() {
|
|
476
|
-
document.getElementById("user-name")!.textContent = this.name;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
```
|
|
480
|
-
|
|
481
|
-
### 2. 依赖注入
|
|
482
|
-
|
|
483
|
-
- **明确依赖**: 使用 Zod schema 明确定义依赖的类型
|
|
484
|
-
- **命名空间**: 使用命名空间隔离不同环境的配置
|
|
485
|
-
- **懒加载**: 只在需要时注入依赖
|
|
486
|
-
|
|
487
|
-
### 3. 性能优化
|
|
488
|
-
|
|
489
|
-
- **合理使用参数**: 相同参数会返回相同实例,利用这一点进行优化
|
|
490
|
-
- **避免过度观察**: 不要观察不必要的对象
|
|
491
|
-
- **及时清理**: 使用 `finalizationRegistry` 进行资源清理
|
|
492
|
-
|
|
493
|
-
## 类型定义
|
|
494
|
-
|
|
495
|
-
```typescript
|
|
496
|
-
// 主要导出的类型
|
|
497
|
-
export interface WatchCallback {
|
|
498
|
-
(path: Array<string | symbol>, oldValue: any, newValue: any): void;
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
export interface FinalizationRegistry {
|
|
502
|
-
register(callback: () => void): void;
|
|
503
|
-
unregister(): void;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
export interface LoaderInstance {
|
|
507
|
-
loading: Record<symbol, [Function, Promise<unknown>]>;
|
|
508
|
-
globalLoading: number;
|
|
509
|
-
load(isGlobal?: boolean): MethodDecorator;
|
|
510
|
-
once(): MethodDecorator;
|
|
511
|
-
}
|
|
512
|
-
```
|
|
513
|
-
|
|
514
|
-
## 兼容性
|
|
515
|
-
|
|
516
|
-
- **React**: >= 17.0.0
|
|
517
|
-
- **TypeScript**: >= 4.5.0
|
|
518
|
-
- **Zod**: ^4.1.5
|
|
519
|
-
|
|
520
|
-
## 许可证
|
|
521
|
-
|
|
522
|
-
MIT License - 详见 [LICENSE.md](./LICENSE.md)
|
|
523
|
-
|
|
524
|
-
## 贡献
|
|
525
|
-
|
|
526
|
-
欢迎提交 Issue 和 Pull Request!
|
|
527
|
-
|
|
528
|
-
## 更新日志
|
|
529
|
-
|
|
530
|
-
详见 [CHANGELOG.md](./CHANGELOG.md)
|
|
1
|
+
# easy-model
|
|
2
|
+
|
|
3
|
+
**easy-model** 是一个围绕「类模型(Model Class)+ 依赖注入 + 精细化变更监听」构建的 React 状态管理与 IoC 工具集。你可以用普通的 TypeScript 类描述业务模型,通过少量 API 即可:
|
|
4
|
+
|
|
5
|
+
- **在函数组件中直接创建 / 注入模型实例**(`useModel` / `useInstance`)
|
|
6
|
+
- **跨组件共享同一实例**,支持按参数分组实例缓存(`provide`)
|
|
7
|
+
- **监听模型及其嵌套属性的变化**(`watch` / `useWatcher`)
|
|
8
|
+
- **用装饰器和 IoC 容器做依赖注入**(`Container` / `CInjection` / `VInjection` / `inject`)
|
|
9
|
+
- **统一管理异步调用的加载状态**(`loader` / `useLoader`)
|
|
10
|
+
|
|
11
|
+
相比 Redux / MobX / Zustand,easy-model 的目标是:**保持接近类 OOP 的心智模型,同时提供较好的性能和类型体验,并内建 IoC 能力。**
|
|
12
|
+
|
|
13
|
+
### 核心特性一览
|
|
14
|
+
|
|
15
|
+
- **类模型驱动(Class-based Model)**
|
|
16
|
+
直接用 TypeScript 类描述业务:字段即状态,方法即业务逻辑,没有额外的 action / reducer ceremony。
|
|
17
|
+
|
|
18
|
+
- **按参数缓存实例(Instance by arguments)**
|
|
19
|
+
使用 `provide` 包装后,相同参数获取的是同一实例,不同参数获取不同实例,天然支持「按业务 key」分区的状态。
|
|
20
|
+
|
|
21
|
+
- **深层变更监听(Deep watch)**
|
|
22
|
+
`watch` / `useWatcher` 可以监听到:
|
|
23
|
+
- 模型自身字段的变化;
|
|
24
|
+
- 嵌套对象属性变化;
|
|
25
|
+
- 实例之间的嵌套 / 引用关系变化;
|
|
26
|
+
- getter 返回的衍生实例的变化。
|
|
27
|
+
|
|
28
|
+
- **React Hooks 友好**
|
|
29
|
+
- `useModel`:在组件中创建并订阅模型;
|
|
30
|
+
- `useInstance`:在组件中订阅已有实例;
|
|
31
|
+
- `useWatcher`:在函数组件中挂载监听回调;
|
|
32
|
+
- `useLoader`:统一获取全局 loading 状态及单个方法的 loading 状态。
|
|
33
|
+
|
|
34
|
+
- **IoC 容器与依赖注入**
|
|
35
|
+
- 使用 `Container` / `CInjection` / `VInjection` / `config` 配置注入;
|
|
36
|
+
- 使用 `inject` 装饰器在类中按 schema 声明依赖;
|
|
37
|
+
- 支持 namespace 隔离与 `clearNamespace` 清理。
|
|
38
|
+
|
|
39
|
+
### 示例(example/)
|
|
40
|
+
|
|
41
|
+
`example/` 目录包含运行时示例,可帮助快速理解 API。下面是几个「直给」的核心用法片段。
|
|
42
|
+
|
|
43
|
+
- **基础计数器:`useModel` / `useWatcher`**(见 `example/index.tsx`)
|
|
44
|
+
使用 `CounterModel` 展示如何在函数组件中创建模型实例、读取 / 更新字段,并监听变更:
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
import { useModel, useWatcher } from "easy-model";
|
|
48
|
+
|
|
49
|
+
class CounterModel {
|
|
50
|
+
count = 0;
|
|
51
|
+
label: string;
|
|
52
|
+
constructor(initial = 0, label = "计数器") {
|
|
53
|
+
this.count = initial;
|
|
54
|
+
this.label = label;
|
|
55
|
+
}
|
|
56
|
+
increment() {
|
|
57
|
+
this.count += 1;
|
|
58
|
+
}
|
|
59
|
+
decrement() {
|
|
60
|
+
this.count -= 1;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function Counter() {
|
|
65
|
+
const counter = useModel(CounterModel, [0, "示例"]);
|
|
66
|
+
|
|
67
|
+
useWatcher(counter, (keys, prev, next) => {
|
|
68
|
+
console.log("changed:", keys.join("."), prev, "->", next);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<div>
|
|
73
|
+
<h2>{counter.label}</h2>
|
|
74
|
+
<div>{counter.count}</div>
|
|
75
|
+
<button onClick={() => counter.decrement()}>-</button>
|
|
76
|
+
<button onClick={() => counter.increment()}>+</button>
|
|
77
|
+
</div>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
- **跨组件通信:`useModel` + `useInstance`**
|
|
83
|
+
通过 `CommunicateModel` + `provide`,让多个组件共享同一实例(按参数分组):
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import { provide, useModel, useInstance } from "easy-model";
|
|
87
|
+
|
|
88
|
+
class CommunicateModel {
|
|
89
|
+
constructor(public name: string) {}
|
|
90
|
+
value = 0;
|
|
91
|
+
random() {
|
|
92
|
+
this.value = Math.random();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const CommunicateProvider = provide(CommunicateModel);
|
|
97
|
+
|
|
98
|
+
function CommunicateA() {
|
|
99
|
+
const { value, random } = useModel(CommunicateModel, ["channel"]);
|
|
100
|
+
return (
|
|
101
|
+
<div>
|
|
102
|
+
<span>组件 A:{value}</span>
|
|
103
|
+
<button onClick={random}>改变数值</button>
|
|
104
|
+
</div>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function CommunicateB() {
|
|
109
|
+
const { value } = useInstance(CommunicateProvider("channel"));
|
|
110
|
+
return <div>组件 B:{value}</div>;
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
- **独立监听:`watch`**
|
|
115
|
+
在 React 外部或普通函数中监听某个实例,将变更记录到日志列表中:
|
|
116
|
+
|
|
117
|
+
```tsx
|
|
118
|
+
import { provide, watch } from "easy-model";
|
|
119
|
+
|
|
120
|
+
class WatchModel {
|
|
121
|
+
constructor(public name: string) {}
|
|
122
|
+
value = 0;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const WatchProvider = provide(WatchModel);
|
|
126
|
+
const inst = WatchProvider("watch-demo");
|
|
127
|
+
|
|
128
|
+
const stop = watch(inst, (keys, prev, next) => {
|
|
129
|
+
console.log(`${keys.join(".")}: ${prev} -> ${next}`);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
inst.value += 1;
|
|
133
|
+
// 不再需要时取消监听
|
|
134
|
+
stop();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
- **异步加载与全局 Loading:`loader` / `useLoader`**
|
|
138
|
+
通过装饰器标记异步方法,并在组件中读取全局 / 单方法 loading 状态:
|
|
139
|
+
|
|
140
|
+
```tsx
|
|
141
|
+
import { loader, useLoader, useModel } from "easy-model";
|
|
142
|
+
|
|
143
|
+
class LoaderModel {
|
|
144
|
+
constructor(public name: string) {}
|
|
145
|
+
|
|
146
|
+
@loader.load(true)
|
|
147
|
+
async fetch() {
|
|
148
|
+
return new Promise<number>(resolve =>
|
|
149
|
+
setTimeout(() => resolve(42), 1000)
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function LoaderDemo() {
|
|
155
|
+
const { isGlobalLoading, isLoading } = useLoader();
|
|
156
|
+
const inst = useModel(LoaderModel, ["loader-demo"]);
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<div>
|
|
160
|
+
<div>全局加载状态:{String(isGlobalLoading)}</div>
|
|
161
|
+
<div>当前加载状态:{String(isLoading(inst.fetch))}</div>
|
|
162
|
+
<button onClick={() => inst.fetch()} disabled={isGlobalLoading}>
|
|
163
|
+
触发一次异步加载
|
|
164
|
+
</button>
|
|
165
|
+
</div>
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
- **依赖注入示例:`example/inject.tsx`**
|
|
171
|
+
展示了如何用 zod schema 描述依赖,并通过容器完成注入:
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
import {
|
|
175
|
+
CInjection,
|
|
176
|
+
Container,
|
|
177
|
+
VInjection,
|
|
178
|
+
config,
|
|
179
|
+
inject,
|
|
180
|
+
} from "easy-model";
|
|
181
|
+
import { object, number } from "zod";
|
|
182
|
+
|
|
183
|
+
const schema = object({ number: number() }).describe("测试用schema");
|
|
184
|
+
|
|
185
|
+
class Test {
|
|
186
|
+
xxx = 1;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
class MFoo {
|
|
190
|
+
@inject(schema)
|
|
191
|
+
bar?: { number: number };
|
|
192
|
+
baz?: number;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
config(
|
|
196
|
+
<Container>
|
|
197
|
+
<CInjection schema={schema} ctor={Test} />
|
|
198
|
+
<VInjection schema={schema} val={{ number: 100 }} />
|
|
199
|
+
</Container>
|
|
200
|
+
);
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
- **Benchmark 示例:`example/benchmark.tsx`**
|
|
204
|
+
提供 easy-model / Redux / MobX / Zustand 在同一场景下的**粗略性能对比面板**,下文有详细说明。
|
|
205
|
+
|
|
206
|
+
### 测试(test/)
|
|
207
|
+
|
|
208
|
+
`test/` 目录使用 Vitest + React Testing Library 覆盖了核心行为:
|
|
209
|
+
|
|
210
|
+
- **provide 与实例缓存**
|
|
211
|
+
- 相同参数返回同一实例;
|
|
212
|
+
- 不同参数返回不同实例。
|
|
213
|
+
|
|
214
|
+
- **watch 的深层监听能力**
|
|
215
|
+
- 监听简单字段变更;
|
|
216
|
+
- 监听嵌套对象属性变更;
|
|
217
|
+
- 处理实例之间的嵌套引用关系;
|
|
218
|
+
- 支持 getter 返回实例(如 `child2`)的变更路径监听。
|
|
219
|
+
|
|
220
|
+
- **IoC 配置与命名空间**
|
|
221
|
+
- 通过 `config` + `Container` + `CInjection` / `VInjection` 注册依赖;
|
|
222
|
+
- `isRegistered` 判断指定 schema 是否已在某个 namespace 注册;
|
|
223
|
+
- `clearNamespace` 清理命名空间中的注册项。
|
|
224
|
+
|
|
225
|
+
- **Hooks 行为**
|
|
226
|
+
- `useModel` + `useInstance` 在两个组件间共享状态并同步更新 UI;
|
|
227
|
+
- `useWatcher` 在函数组件中监听模型变化;
|
|
228
|
+
- `loader` + `useLoader` 正确反映全局 / 单个方法的 loading 状态。
|
|
229
|
+
|
|
230
|
+
### 与 Redux / MobX / Zustand 的对比
|
|
231
|
+
|
|
232
|
+
下表是从「心智模型 / 用法复杂度 / 性能 & 能力边界」等角度对比 easy-model 与常见方案:
|
|
233
|
+
|
|
234
|
+
| 方案 | 编程模型 | 典型心智负担 | 内建 IoC / DI | 性能特征(本项目场景) |
|
|
235
|
+
| -------------- | ------------------------ | ------------------------------------------------------------------ | ---------------------- | --------------------------------------- |
|
|
236
|
+
| **easy-model** | 类模型 + Hooks + IoC | 写类 + 写方法即可,少量 API(`provide` / `useModel` / `watch` 等) | 是 | 在极端批量更新下仍为**个位数毫秒** |
|
|
237
|
+
| **Redux** | 不可变 state + reducer | 需要 action / reducer / dispatch 等模板代码 | 否 | 在同场景下为**数十毫秒级** |
|
|
238
|
+
| **MobX** | 可观察对象 + 装饰器 | 对响应式系统有一定学习成本,隐藏的依赖追踪 | 否(偏响应式而非 IoC) | 性能优于 Redux,但仍是**十几毫秒级** |
|
|
239
|
+
| **Zustand** | Hooks store + 函数式更新 | API 简洁,偏轻量,适合局部状态 | 否 | 在本场景下是**最快**,但不提供 IoC 能力 |
|
|
240
|
+
|
|
241
|
+
从项目角度看,easy-model 的特点在于:
|
|
242
|
+
|
|
243
|
+
- **对比 Redux**:
|
|
244
|
+
- 不需要拆分 action / reducer / selector,业务逻辑直接写在模型方法里;
|
|
245
|
+
- 避免大量模板代码,类型推断更直接(基于类字段和方法签名);
|
|
246
|
+
- 自动处理实例缓存与变更订阅,无需手动 connect / useSelector。
|
|
247
|
+
|
|
248
|
+
- **对比 MobX**:
|
|
249
|
+
- 保留类模型的直觉优势,同时用显式 API(`watch` / `useWatcher`)暴露依赖关系;
|
|
250
|
+
- 依赖注入、命名空间、清理等能力是 easy-model 的一等公民,而非额外工具。
|
|
251
|
+
|
|
252
|
+
- **对比 Zustand**:
|
|
253
|
+
- 性能接近(在本项目 benchmark 中 easy-model 仍处于个位数毫秒),但提供更完整的 IoC / DI / deep watch 组合能力;
|
|
254
|
+
- 更适合中大型、需要明确领域模型和依赖关系的项目,而不仅是「轻量局部状态 store」。
|
|
255
|
+
|
|
256
|
+
### Benchmark 场景说明(与 Redux / MobX / Zustand 的粗略性能对比)
|
|
257
|
+
|
|
258
|
+
在 `example/benchmark.tsx` 中,项目提供了一个**简单且偏极端**的 benchmark,用来在同一台机器上粗略比较不同状态管理方案在「大量同步写入」场景下的表现。核心场景为:
|
|
259
|
+
|
|
260
|
+
1. **初始化一个包含 10,000 个数字的数组**;
|
|
261
|
+
2. 点击按钮后,对所有元素做 **5 轮自增**;
|
|
262
|
+
3. 使用 `performance.now()` 统计这段**同步计算与状态写入**时间;
|
|
263
|
+
4. **不计入 React 首屏渲染时间**,仅关注每次点击触发的计算 + 状态写入耗时。
|
|
264
|
+
|
|
265
|
+
在一台常规开发机上的一次测试结果(单位:ms,取单次运行的代表值)大致如下(数值可能因环境而异,仅供参考):
|
|
266
|
+
|
|
267
|
+
| 实现 | 耗时(ms) |
|
|
268
|
+
| -------------- | ---------- |
|
|
269
|
+
| **easy-model** | ≈ 3.1 |
|
|
270
|
+
| **Redux** | ≈ 51.5 |
|
|
271
|
+
| **MobX** | ≈ 16.9 |
|
|
272
|
+
| **Zustand** | ≈ 0.6 |
|
|
273
|
+
|
|
274
|
+
需要特别强调:
|
|
275
|
+
|
|
276
|
+
- **这是一个刻意放大的「批量更新」场景**,主要目的是放大不同实现之间在「大量同步写入 + 通知」路径上的差异;
|
|
277
|
+
- 结果会受到:浏览器 / Node 版本、硬件性能、打包模式等多种因素影响,因此这里只能作为**趋势性的参考**,而非严谨的性能报告;
|
|
278
|
+
- Zustand 在这个场景下表现最好,这是符合其「极简 store + 函数式更新」定位的;
|
|
279
|
+
- easy-model 虽然在这类极端场景下略慢于 Zustand,但**仍明显快于 Redux / MobX**,同时提供:
|
|
280
|
+
- 类模型 + IoC + 深度监听等高级能力;
|
|
281
|
+
- 更适合中大型业务的结构化编码体验。
|
|
282
|
+
|
|
283
|
+
### 适用场景
|
|
284
|
+
|
|
285
|
+
easy-model 更适合以下类型的项目:
|
|
286
|
+
|
|
287
|
+
- 领域模型清晰、希望用类来承载业务状态与方法;
|
|
288
|
+
- 需要在各处优雅地做依赖注入(如仓储、服务、配置、schema 等);
|
|
289
|
+
- 对「监听某个模型 / 某个嵌套字段的变化」有较强需求;
|
|
290
|
+
- 希望在保证结构化代码与可维护性的同时,获得接近轻量状态库的性能。
|
|
291
|
+
|
|
292
|
+
如果你目前在使用 Redux / MobX / Zustand,想要:
|
|
293
|
+
|
|
294
|
+
- 减少心智负担和模板代码;
|
|
295
|
+
- 获得更自然的类模型与 IoC 能力;
|
|
296
|
+
- 又不希望在性能上明显吃亏;
|
|
297
|
+
|
|
298
|
+
那么可以尝试将部分模块迁移到 easy-model,并先在 `example/benchmark.tsx` 中实际运行一次 benchmark,观察在你的机器和真实业务数据规模下的表现。
|