Skip to content

Commit 8ed9632

Browse files
committed
adjust
1 parent f71de39 commit 8ed9632

File tree

5 files changed

+73
-16
lines changed

5 files changed

+73
-16
lines changed
File renamed without changes.
File renamed without changes.

public/bucket.excalidraw.png

26.6 KB
Loading
51.5 KB
Loading

slides.md

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function update() {
9393

9494
然后我们定义一些新术语:
9595

96-
- `update()`: 产生副作用的函数,简称副作用函数,它会修改了 sum 的状态
96+
- `update()`: 产生副作用的函数,简称副作用函数,它会修改 sum 的状态
9797
- `a``b`: 副作用函数的依赖
9898

9999
同时,假设还存在一个魔术方法 `whenDepsChange`,当依赖 a 和 b 发生变化时,重新执行 update 方法
@@ -126,8 +126,6 @@ a = 10 // 修改 a 的值, 我们期待会重新执行 update 方法,并且 su
126126

127127
当然,接下来我们来了解下如何实现响应式数据。
128128

129-
---
130-
layout: default
131129
---
132130

133131
## 如何实现响应式数据
@@ -188,8 +186,13 @@ obj.text = 'Hello Vue3!' // 赋值操作
188186
```
189187

190188
<arrow v-click="[1, 2]" x1="330" y1="170" x2="630" y2="280" color="#564" width="3" arrowSize="1" />
191-
<img v-click="[1, 2]" alt="桶占位" />
192-
<img v-click="[2, 3]" alt="桶占位" />
189+
<arrow v-click="[2, 3]" x1="230" y1="230" x2="630" y2="280" color="#564" width="3" arrowSize="1" />
190+
<img
191+
v-click="[1,3]"
192+
src="public/bucket.excalidraw.png"
193+
alt="桶占位"
194+
style="margin-left: 600px; height: 300px"
195+
/>
193196

194197
---
195198
---
@@ -407,13 +410,14 @@ WeakMap<target, Map<key, Set<effectFn>>>
407410
```
408411

409412
<img
410-
src="public/1.excalidraw.png"
413+
src="public/data-structure.excalidraw.png"
411414
alt="the-structure-of-reactivity-data-and-effect-fn"
412415
/>
413416

414417
---
415418

416419
最后,再将代码封装一下:
420+
417421
```js
418422
const obj = reactive(data)
419423

@@ -436,7 +440,7 @@ function track(target, prop) {/* ... */}
436440
function trigger(target, prop) {/* ... */}
437441
```
438442

439-
`demo: 04-design-a-full-reactivity-system.html`
443+
demo: `04-design-a-full-reactivity-system.html`
440444

441445
---
442446
---
@@ -564,7 +568,8 @@ function effect(fn) {
564568
activeEffect = effectStack[effectStack.length - 1]
565569
}
566570
```
567-
demo: 06-nested-effect.html
571+
572+
demo: `06-nested-effect.html`
568573

569574
---
570575

@@ -629,7 +634,7 @@ function effect(fn, options = {}) {
629634
}
630635
```
631636

632-
```js {2-9}
637+
```js
633638
function trigger(target, prop) {
634639
effects.forEach(effectFn => {
635640
if (effectFn.options.schedular) {
@@ -663,7 +668,8 @@ console.log('结束了')
663668
'结束了'
664669
2
665670
```
666-
demo: 07-schedular.html
671+
672+
`demo: 07-schedular.html`
667673

668674
---
669675

@@ -753,7 +759,7 @@ function computed(getter) {
753759

754760
测试一下代码:
755761

756-
demo: 08-computed.html
762+
demo: `08-computed.html`
757763

758764
```js
759765
const data = { a: 1, b: 2 }
@@ -833,7 +839,8 @@ computed(obj)
833839
└── effectFn
834840
`
835841
```
836-
demo: 09-computed-2.html
842+
843+
demo: `09-computed-2.html`
837844

838845
---
839846

@@ -872,7 +879,7 @@ obj.a = 100 // 修改
872879

873880
<div grid="~ cols-2 gap-4">
874881

875-
第二版: (demo: 11-watch-2.html)
882+
第二版: (demo: `11-watch-2.html`)
876883

877884
```js
878885
function watch(source, cb) {
@@ -979,7 +986,7 @@ const obj = reactive(wrapper) // 使用 reactive 将其转为响应式数据
979986
```
980987
再封装成 ref 函数:
981988
```js
982-
// demo: 14.ref.html
989+
// demo: 14-ref.html
983990
function ref(val) {
984991
const wrapper = {
985992
value: val
@@ -1118,7 +1125,7 @@ function toRefs(obj) {
11181125
测试一下:
11191126

11201127
```js
1121-
// demo: 15.ref-2.html
1128+
// demo: 15-ref-2.html
11221129
const newObj = { ...toRefs(obj) }
11231130
effect(() => {
11241131
console.log(newObj.foo.value)
@@ -1247,7 +1254,7 @@ const ast = {
12471254
{
12481255
type: 'Interpolation',
12491256
content: {
1250-
type: 'Expression',
1257+
type: 'SimpleExpression',
12511258
content: String,
12521259
},
12531260
}
@@ -1583,6 +1590,7 @@ function createTransformContext(
15831590
```
15841591

15851592
```js
1593+
// 插拔式的插件预设
15861594
{
15871595
nodeTransforms: [
15881596
transformElement,
@@ -1727,6 +1735,55 @@ function processExpression(node) {
17271735

17281736
## codegen 代码生成
17291737

1738+
上文实现了 `transform`,接下来进入 `compile` 最后的 `codegen` 阶段。
1739+
1740+
### 一些准备工作
1741+
1742+
`codegen` 阶段会根据 `AST` 生成 `render` 函数的代码字符串,而渲染函数的执行会生成虚拟 DOM。
1743+
1744+
假设模板如下:
1745+
1746+
```js
1747+
<div>{{ msg }}</div>
1748+
```
1749+
经过 codegen 代码生成后,会生成如下代码:
1750+
```js
1751+
`
1752+
function render(_ctx) {
1753+
return h('div', null, _toDisplayString(_ctx.msg))
1754+
}
1755+
`
1756+
```
1757+
1758+
---
1759+
1760+
我们看到,代码字符串中有 `h` 函数,`h` 函数实际上就是对 `createVNode` 的封装,它们都是用于创建 `VNode` 的。
1761+
1762+
```js
1763+
// 源码位置: packages/runtime-core/src/h.ts
1764+
function h(tag, props, children) {
1765+
// 我们这里就简单的返回一个对象,实际源码中会复杂很多
1766+
return {
1767+
tag,
1768+
props,
1769+
children,
1770+
}
1771+
}
1772+
```
1773+
1774+
`_toDisplayString` 函数是 `toDisplayString` 的别名,它用于将插值表达式转换为字符串:
1775+
1776+
```js
1777+
const toDisplayString = (val) => {
1778+
return String(val)
1779+
}
1780+
```
1781+
1782+
---
1783+
1784+
### 代码实现
1785+
1786+
17301787
`codegen` 主入口函数:
17311788

17321789
```js

0 commit comments

Comments
 (0)