Skip to content

Commit e01f994

Browse files
committed
v-effect
1 parent 1992ad6 commit e01f994

File tree

6 files changed

+25
-10
lines changed

6 files changed

+25
-10
lines changed

demos/effect.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script type="module">
2+
import { createApp } from '../src'
3+
createApp().mount()
4+
</script>
5+
6+
<div v-scope="{ count: 0 }">
7+
<div v-effect="$el.textContent = count"></div>
8+
<button @click="count++">++</button>
9+
</div>

index.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
</style>
66
<ul>
77
<li><a href="/demos/data.html">v-scope</a></li>
8+
<li><a href="/demos/effect.html">v-effect</a></li>
89
<li><a href="/demos/bind.html">v-bind</a></li>
910
<li><a href="/demos/on.html">v-on</a></li>
1011
<li><a href="/demos/if.html">v-if</a></li>
1112
<li><a href="/demos/for.html">v-for</a></li>
1213
<li><a href="/demos/model.html">v-model</a></li>
1314
<li><a href="/demos/reuse.html">Logic Reuse</a></li>
1415
</ul>
15-
16-
<div v-scope="{ count: 0 }">
17-
{{ count }}
18-
<button @click="count++">inc</button>
19-
</div>

src/directives/effect.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Directive } from '.'
2+
3+
export const effect: Directive = ({ get, effect }) => {
4+
effect(get)
5+
}

src/directives/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Context } from '../walk'
2-
import { effect } from '@vue/reactivity'
2+
import { effect as rawEffect } from '@vue/reactivity'
33
import { bind } from './bind'
44
import { on } from './on'
55
import { show } from './show'
66
import { text } from './text'
77
import { html } from './html'
88
import { model } from './model'
9+
import { effect } from './effect'
910

1011
export interface Directive<T = Element> {
1112
(ctx: DirectiveContext<T>): (() => void) | void
@@ -14,7 +15,7 @@ export interface Directive<T = Element> {
1415
export interface DirectiveContext<T = Element> {
1516
el: T
1617
get: (exp?: string) => any
17-
effect: typeof effect
18+
effect: typeof rawEffect
1819
exp: string
1920
arg?: string
2021
modifiers?: Record<string, true>
@@ -27,5 +28,6 @@ export const builtInDirectives: Record<string, Directive<any>> = {
2728
show,
2829
text,
2930
html,
30-
model
31+
model,
32+
effect
3133
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { createApp } from './app'
2-
export { reactive, effect } from '@vue/reactivity'
2+
export { nextTick } from './scheduler'
3+
export { reactive } from '@vue/reactivity'
34

45
import { createApp } from './app'
56

src/scheduler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ let queued = false
22
const queue: Function[] = []
33
const p = Promise.resolve()
44

5+
export const nextTick = (fn: () => void) => p.then(fn)
6+
57
export const queueJob = (job: Function) => {
68
if (!queue.includes(job)) queue.push(job)
79
if (!queued) {
810
queued = true
9-
p.then(flushJobs)
11+
nextTick(flushJobs)
1012
}
1113
}
1214

0 commit comments

Comments
 (0)