forked from cuixiaorui/mini-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaf025e
commit 9f16475
Showing
3 changed files
with
40 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ReactiveEffect } from "@mini-vue/reactivity"; | ||
import { queuePreFlushCb } from "./scheduler"; | ||
|
||
// Simple effect. | ||
export function watchEffect(effect) { | ||
doWatch(effect); | ||
} | ||
|
||
function doWatch(source) { | ||
// 把 job 添加到 pre flush 里面 | ||
// 也就是在视图更新完成之前进行渲染(待确认?) | ||
// 当逻辑执行到这里的时候 就已经触发了 watchEffect | ||
const job = () => { | ||
effect.run(); | ||
}; | ||
|
||
// 这里用 scheduler 的目的就是在更新的时候 | ||
// 让回调可以在 render 前执行 变成一个异步的行为(这里也可以通过 flush 来改变) | ||
const scheduler = () => queuePreFlushCb(job); | ||
|
||
const getter = () => { | ||
source(); | ||
}; | ||
|
||
const effect = new ReactiveEffect(getter, scheduler); | ||
|
||
// 这里执行的就是 getter | ||
effect.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters