之前做内部项目,后台用koa搭建,router处理那里是由各个细分路由组合起来,在每个路由的回调里处理业务代码,将数据返回给前台。这几天看koa的书,里面讲到在koa里运用MVC将各个模块抽出来,实现代码解耦,记录一下。
原代码结构
|— src
|— router/
|—index.js
|—test.js
|— app.js
列表数据大概300条左右,请求一次性请求所有数据,在操作之后到页面上显示表格数据大概有3秒卡顿,想到用chrome performance面板进行分析。
对performance运用比较陌生,看了两篇performance调试文章,链接贴出来
前端性能测试
全新Chrome Devtool Performance使用指南
结果如下:
首先是对applyMiddleware函数的重载,重载了七个函数,主要是传入参数个数的区别。1
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
29export default function applyMiddleware(): StoreEnhancer
export default function applyMiddleware<Ext1, S>(
middleware1: Middleware<Ext1, S, any>
): StoreEnhancer<{ dispatch: Ext1 }>
export default function applyMiddleware<Ext1, Ext2, S>(
middleware1: Middleware<Ext1, S, any>,
middleware2: Middleware<Ext2, S, any>
): StoreEnhancer<{ dispatch: Ext1 & Ext2 }>
export default function applyMiddleware<Ext1, Ext2, Ext3, S>(
middleware1: Middleware<Ext1, S, any>,
middleware2: Middleware<Ext2, S, any>,
middleware3: Middleware<Ext3, S, any>
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 }>
export default function applyMiddleware<Ext1, Ext2, Ext3, Ext4, S>(
middleware1: Middleware<Ext1, S, any>,
middleware2: Middleware<Ext2, S, any>,
middleware3: Middleware<Ext3, S, any>,
middleware4: Middleware<Ext4, S, any>
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 }>
export default function applyMiddleware<Ext1, Ext2, Ext3, Ext4, Ext5, S>(
middleware1: Middleware<Ext1, S, any>,
middleware2: Middleware<Ext2, S, any>,
middleware3: Middleware<Ext3, S, any>,
middleware4: Middleware<Ext4, S, any>,
middleware5: Middleware<Ext5, S, any>
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 & Ext5 }>
export default function applyMiddleware<Ext, S = any>(
...middlewares: Middleware<any, S, any>[]
): StoreEnhancer<{ dispatch: Ext }>
首先是对combineReducers函数的重载,重载了三个函数,主要区别是传参reducers类型的不同。
1 | // 重载函数 |
然后看看combineReducers函数的结构。
从右到左来组合多个函数,是reduce函数的一个应用实现。
首先仍然是重载了多个参数的函数声明,区别主要是传入参数个数。1
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
30export default function compose<F extends Function>(f: F): F
/* two functions */
export default function compose<A, T extends any[], R>(
f1: (a: A) => R,
f2: Func<T, A>
): Func<T, R>
/* three functions */
export default function compose<A, B, T extends any[], R>(
f1: (b: B) => R,
f2: (a: A) => B,
f3: Func<T, A>
): Func<T, R>
/* four functions */
export default function compose<A, B, C, T extends any[], R>(
f1: (c: C) => R,
f2: (b: B) => C,
f3: (a: A) => B,
f4: Func<T, A>
): Func<T, R>
/* rest */
export default function compose<R>(
f1: (a: any) => R,
...funcs: Function[]
): (...args: any[]) => R
export default function compose<R>(...funcs: Function[]): (...args: any[]) => R
看完 Redux文档,对Redux的内部运作不是很了解,大致问题记录如下:
带着这些问题打开Redux源码](https://github.com/reduxjs/redux
),发现才一年的时间没做前端,Redux源码都从js换成ts了,屁颠屁颠跑去看TS文档,没咋看懂,但是应该能上源码了。
首先看看文件夹目录结构
几个常用的api都在src/下,再下一级目录就是封装的一些类型,这些后面再详细描述,先开始上源码!