Skip to content

Commit 90bd41b

Browse files
feat: update
1 parent 5fcc37a commit 90bd41b

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

docs/.vuepress/config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,19 @@ module.exports = {
163163
['/algorithm/动态规划/最大子序和', '最大子序和'],
164164
['/algorithm/动态规划/最长公共前缀', '最长公共前缀'],
165165
['/algorithm/动态规划/最长回文子串', '最长回文子串'],
166-
['/algorithm/动态规划/最长公共子序列', '最长公共子序列'],
167-
['/algorithm/动态规划/300.最长上升子序列', '最长上升子序列'],
168166
['/algorithm/动态规划/打家劫舍', '打家劫舍'],
169167
['/algorithm/动态规划/打家劫舍II', '打家劫舍 II'],
170168
['/algorithm/动态规划/打家劫舍III', '打家劫舍 III'],
169+
['/algorithm/动态规划/经典/1143.最长公共子序列', '最长公共子序列'],
170+
['/algorithm/动态规划/经典/300.最长上升子序列', '最长上升子序列'],
171+
['/algorithm/动态规划/经典/1143.最长公共子序列', '最长公共子序列'],
172+
['/algorithm/动态规划/经典/72.编辑距离', '72.编辑距离'],
173+
[
174+
'/algorithm/动态规划/经典/120.三角形的最小路径和',
175+
'120.三角形的最小路径和',
176+
],
177+
['/algorithm/动态规划/经典/322.零钱兑换', '零钱兑换'],
178+
['/algorithm/动态规划/经典/152.乘积最大子数组', '152.乘积最大子数组'],
171179
],
172180
},
173181
],
@@ -185,6 +193,7 @@ module.exports = {
185193
['/jsCode/实现一个iterator', '实现一个iterator'],
186194
['/jsCode/setTimeout实现setInterval', 'setTimeout实现setInterval'],
187195
['/jsCode/发布-订阅模式的实现', '实现一个 EventEmitter'],
196+
['/jsCode/洋葱模型compose', 'koa 洋葱模型实现'],
188197
],
189198
},
190199
{
@@ -222,6 +231,7 @@ module.exports = {
222231
['/jsCode/实现一个vue自定义指令-懒加载', '实现一个vue自定义指令-懒加载'],
223232
['/jsCode/实现一个轮播图', '实现一个轮播图'],
224233
['/jsCode/放大镜效果', '放大镜效果'],
234+
['/jsCode/沙箱', '实现一个沙箱'],
225235
],
226236
},
227237
],

docs/jsCode/洋葱模型compose.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,68 @@ app.compose();
2424

2525
核心:中间件管理和 next 实现,其中 next 是巧妙的使用了 Promise 特性。洋葱模型,本质上是 Promise.resolve()的递归。
2626

27+
实现一
28+
29+
```js
30+
function compose(middleware) {
31+
return function(context, next) {
32+
// last called middleware #
33+
let index = -1;
34+
return dispatch(0);
35+
36+
function dispatch(i) {
37+
if (i <= index) return Promise.reject(new Error('next() called multiple times'));
38+
index = i;
39+
let fn = middleware[i];
40+
if (i === middleware.length) fn = next;
41+
if (!fn) return Promise.resolve();
42+
try {
43+
// 核心代码:返回Promise
44+
// next时,交给下一个dispatch(下一个中间件方法)
45+
// 同时,当前同步代码挂起,直到中间件全部完成后继续
46+
return Promise.resolve(
47+
fn(context, function next() {
48+
return dispatch(i + 1);
49+
})
50+
);
51+
} catch (err) {
52+
return Promise.reject(err);
53+
}
54+
}
55+
};
56+
}
57+
```
58+
59+
实现二
60+
61+
```js
62+
// 文件:app.js
63+
app.compose = function() {
64+
// 自执行 async 函数返回 Promise
65+
return (async function() {
66+
// 定义默认的 next,最后一个中间件内执行的 next
67+
let next = async () => Promise.resolve();
68+
69+
// middleware 为每一个中间件函数,oldNext 为每个中间件函数中的 next
70+
// 函数返回一个 async 作为新的 next,async 执行返回 Promise,解决异步问题
71+
function createNext(middleware, oldNext) {
72+
return async () => {
73+
await middleware(oldNext);
74+
};
75+
}
76+
77+
// 反向遍历中间件数组,先把 next 传给最后一个中间件函数
78+
// 将新的中间件函数存入 next 变量
79+
// 调用下一个中间件函数,将新生成的 next 传入
80+
for (let i = app.middlewares.length - 1; i >= 0; i--) {
81+
next = createNext(app.middlewares[i], next);
82+
}
83+
84+
await next();
85+
})();
86+
};
87+
```
88+
2789
## 参考
2890

2991
- [Koa 洋葱模型原理分析](https://lq782655835.github.io/blogs/node/koa-compose-modal.html)

0 commit comments

Comments
 (0)