Skip to content

Commit 5b3027f

Browse files
authored
feat(runtime-vapor): watchEffect/watch and onEffectCleanup (#69)
1 parent 597eae4 commit 5b3027f

20 files changed

+1168
-81
lines changed

packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`compile > bindings 1`] = `
4-
"import { template as _template, children as _children, createTextNode as _createTextNode, insert as _insert, effect as _effect, setText as _setText } from 'vue/vapor';
4+
"import { template as _template, children as _children, createTextNode as _createTextNode, insert as _insert, watchEffect as _watchEffect, setText as _setText } from 'vue/vapor';
55
66
export function render(_ctx) {
77
const t0 = _template("<div>count is <!>.</div>")
88
const n0 = t0()
99
const { 0: [n3, { 1: [n2],}],} = _children(n0)
1010
const n1 = _createTextNode(_ctx.count)
1111
_insert(n1, n3, n2)
12-
_effect(() => {
12+
_watchEffect(() => {
1313
_setText(n1, undefined, _ctx.count)
1414
})
1515
return n0
@@ -121,45 +121,45 @@ export function render(_ctx) {
121121
`;
122122

123123
exports[`compile > directives > v-pre > self-closing v-pre 1`] = `
124-
"import { template as _template, children as _children, createTextNode as _createTextNode, append as _append, effect as _effect, setText as _setText, setAttr as _setAttr } from 'vue/vapor';
124+
"import { template as _template, children as _children, createTextNode as _createTextNode, append as _append, watchEffect as _watchEffect, setText as _setText, setAttr as _setAttr } from 'vue/vapor';
125125
126126
export function render(_ctx) {
127127
const t0 = _template("<div></div><div><Comp></Comp></div>")
128128
const n0 = t0()
129129
const { 1: [n2],} = _children(n0)
130130
const n1 = _createTextNode(_ctx.bar)
131131
_append(n2, n1)
132-
_effect(() => {
132+
_watchEffect(() => {
133133
_setText(n1, undefined, _ctx.bar)
134134
})
135-
_effect(() => {
135+
_watchEffect(() => {
136136
_setAttr(n2, "id", undefined, _ctx.foo)
137137
})
138138
return n0
139139
}"
140140
`;
141141

142142
exports[`compile > directives > v-pre > should not affect siblings after it 1`] = `
143-
"import { template as _template, children as _children, createTextNode as _createTextNode, append as _append, effect as _effect, setText as _setText, setAttr as _setAttr } from 'vue/vapor';
143+
"import { template as _template, children as _children, createTextNode as _createTextNode, append as _append, watchEffect as _watchEffect, setText as _setText, setAttr as _setAttr } from 'vue/vapor';
144144
145145
export function render(_ctx) {
146146
const t0 = _template("<div :id=\\"foo\\"><Comp></Comp>{{ bar }}</div><div><Comp></Comp></div>")
147147
const n0 = t0()
148148
const { 1: [n2],} = _children(n0)
149149
const n1 = _createTextNode(_ctx.bar)
150150
_append(n2, n1)
151-
_effect(() => {
151+
_watchEffect(() => {
152152
_setText(n1, undefined, _ctx.bar)
153153
})
154-
_effect(() => {
154+
_watchEffect(() => {
155155
_setAttr(n2, "id", undefined, _ctx.foo)
156156
})
157157
return n0
158158
}"
159159
`;
160160

161161
exports[`compile > dynamic root 1`] = `
162-
"import { fragment as _fragment, createTextNode as _createTextNode, append as _append, effect as _effect, setText as _setText } from 'vue/vapor';
162+
"import { fragment as _fragment, createTextNode as _createTextNode, append as _append, watchEffect as _watchEffect, setText as _setText } from 'vue/vapor';
163163
164164
export function render(_ctx) {
165165
const t0 = _fragment()
@@ -168,18 +168,18 @@ export function render(_ctx) {
168168
const n1 = _createTextNode(1)
169169
const n2 = _createTextNode(2)
170170
_append(n0, n1, n2)
171-
_effect(() => {
171+
_watchEffect(() => {
172172
_setText(n1, undefined, 1)
173173
})
174-
_effect(() => {
174+
_watchEffect(() => {
175175
_setText(n2, undefined, 2)
176176
})
177177
return n0
178178
}"
179179
`;
180180

181181
exports[`compile > dynamic root nodes and interpolation 1`] = `
182-
"import { template as _template, children as _children, createTextNode as _createTextNode, prepend as _prepend, insert as _insert, append as _append, on as _on, effect as _effect, setText as _setText, setAttr as _setAttr } from 'vue/vapor';
182+
"import { template as _template, children as _children, createTextNode as _createTextNode, prepend as _prepend, insert as _insert, append as _append, on as _on, watchEffect as _watchEffect, setText as _setText, setAttr as _setAttr } from 'vue/vapor';
183183
184184
export function render(_ctx) {
185185
const t0 = _template("<button>foo<!>foo</button>")
@@ -192,7 +192,7 @@ export function render(_ctx) {
192192
_insert(n2, n4, n5)
193193
_append(n4, n3)
194194
_on(n4, "click", (...args) => (_ctx.handleClick && _ctx.handleClick(...args)))
195-
_effect(() => {
195+
_watchEffect(() => {
196196
_setText(n1, undefined, _ctx.count)
197197
_setText(n2, undefined, _ctx.count)
198198
_setText(n3, undefined, _ctx.count)
@@ -207,7 +207,7 @@ exports[`compile > expression parsing > interpolation 1`] = `
207207
const t0 = _fragment()
208208
209209
const n0 = t0()
210-
_effect(() => {
210+
_watchEffect(() => {
211211
_setText(n0, undefined, a + b.value)
212212
})
213213
return n0
@@ -219,7 +219,7 @@ exports[`compile > expression parsing > v-bind 1`] = `
219219
const t0 = _template("<div></div>")
220220
const n0 = t0()
221221
const { 0: [n1],} = _children(n0)
222-
_effect(() => {
222+
_watchEffect(() => {
223223
_setAttr(n1, key.value+1, undefined, _unref(foo)[key.value+1]())
224224
})
225225
return n0
@@ -237,7 +237,7 @@ export function render(_ctx) {
237237
`;
238238

239239
exports[`compile > static + dynamic root 1`] = `
240-
"import { template as _template, children as _children, createTextNode as _createTextNode, prepend as _prepend, insert as _insert, append as _append, effect as _effect, setText as _setText } from 'vue/vapor';
240+
"import { template as _template, children as _children, createTextNode as _createTextNode, prepend as _prepend, insert as _insert, append as _append, watchEffect as _watchEffect, setText as _setText } from 'vue/vapor';
241241
242242
export function render(_ctx) {
243243
const t0 = _template("3<!>6<!>9")
@@ -255,28 +255,28 @@ export function render(_ctx) {
255255
_insert([n3, n4], n0, n9)
256256
_insert([n5, n6], n0, n10)
257257
_append(n0, n7, n8)
258-
_effect(() => {
258+
_watchEffect(() => {
259259
_setText(n1, undefined, 1)
260260
})
261-
_effect(() => {
261+
_watchEffect(() => {
262262
_setText(n2, undefined, 2)
263263
})
264-
_effect(() => {
264+
_watchEffect(() => {
265265
_setText(n3, undefined, 4)
266266
})
267-
_effect(() => {
267+
_watchEffect(() => {
268268
_setText(n4, undefined, 5)
269269
})
270-
_effect(() => {
270+
_watchEffect(() => {
271271
_setText(n5, undefined, 7)
272272
})
273-
_effect(() => {
273+
_watchEffect(() => {
274274
_setText(n6, undefined, 8)
275275
})
276-
_effect(() => {
276+
_watchEffect(() => {
277277
_setText(n7, undefined, 'A')
278278
})
279-
_effect(() => {
279+
_watchEffect(() => {
280280
_setText(n8, undefined, 'B')
281281
})
282282
return n0

packages/compiler-vapor/__tests__/transforms/__snapshots__/vBind.spec.ts.snap

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`compiler v-bind > .camel modifier 1`] = `
4-
"import { template as _template, children as _children, effect as _effect, setAttr as _setAttr } from 'vue/vapor';
4+
"import { template as _template, children as _children, watchEffect as _watchEffect, setAttr as _setAttr } from 'vue/vapor';
55
66
export function render(_ctx) {
77
const t0 = _template("<div></div>")
88
const n0 = t0()
99
const { 0: [n1],} = _children(n0)
10-
_effect(() => {
10+
_watchEffect(() => {
1111
_setAttr(n1, "fooBar", undefined, _ctx.id)
1212
})
1313
return n0
@@ -21,77 +21,77 @@ export function render(_ctx) {
2121
const t0 = _template("<div></div>")
2222
const n0 = t0()
2323
const { 0: [n1],} = _children(n0)
24-
_effect(() => {
24+
_watchEffect(() => {
2525
_setAttr(n1, _camelize(_ctx.foo), undefined, _ctx.id)
2626
})
2727
return n0
2828
}"
2929
`;
3030

3131
exports[`compiler v-bind > .camel modifier w/ no expression 1`] = `
32-
"import { template as _template, children as _children, effect as _effect, setAttr as _setAttr } from 'vue/vapor';
32+
"import { template as _template, children as _children, watchEffect as _watchEffect, setAttr as _setAttr } from 'vue/vapor';
3333
3434
export function render(_ctx) {
3535
const t0 = _template("<div></div>")
3636
const n0 = t0()
3737
const { 0: [n1],} = _children(n0)
38-
_effect(() => {
38+
_watchEffect(() => {
3939
_setAttr(n1, "fooBar", undefined, _ctx.fooBar)
4040
})
4141
return n0
4242
}"
4343
`;
4444

4545
exports[`compiler v-bind > basic 1`] = `
46-
"import { template as _template, children as _children, effect as _effect, setAttr as _setAttr } from 'vue/vapor';
46+
"import { template as _template, children as _children, watchEffect as _watchEffect, setAttr as _setAttr } from 'vue/vapor';
4747
4848
export function render(_ctx) {
4949
const t0 = _template("<div></div>")
5050
const n0 = t0()
5151
const { 0: [n1],} = _children(n0)
52-
_effect(() => {
52+
_watchEffect(() => {
5353
_setAttr(n1, "id", undefined, _ctx.id)
5454
})
5555
return n0
5656
}"
5757
`;
5858

5959
exports[`compiler v-bind > dynamic arg 1`] = `
60-
"import { template as _template, children as _children, effect as _effect, setAttr as _setAttr } from 'vue/vapor';
60+
"import { template as _template, children as _children, watchEffect as _watchEffect, setAttr as _setAttr } from 'vue/vapor';
6161
6262
export function render(_ctx) {
6363
const t0 = _template("<div></div>")
6464
const n0 = t0()
6565
const { 0: [n1],} = _children(n0)
66-
_effect(() => {
66+
_watchEffect(() => {
6767
_setAttr(n1, _ctx.id, undefined, _ctx.id)
6868
})
6969
return n0
7070
}"
7171
`;
7272

7373
exports[`compiler v-bind > no expression (shorthand) 1`] = `
74-
"import { template as _template, children as _children, effect as _effect, setAttr as _setAttr } from 'vue/vapor';
74+
"import { template as _template, children as _children, watchEffect as _watchEffect, setAttr as _setAttr } from 'vue/vapor';
7575
7676
export function render(_ctx) {
7777
const t0 = _template("<div></div>")
7878
const n0 = t0()
7979
const { 0: [n1],} = _children(n0)
80-
_effect(() => {
80+
_watchEffect(() => {
8181
_setAttr(n1, "camel-case", undefined, _ctx.camelCase)
8282
})
8383
return n0
8484
}"
8585
`;
8686

8787
exports[`compiler v-bind > no expression 1`] = `
88-
"import { template as _template, children as _children, effect as _effect, setAttr as _setAttr } from 'vue/vapor';
88+
"import { template as _template, children as _children, watchEffect as _watchEffect, setAttr as _setAttr } from 'vue/vapor';
8989
9090
export function render(_ctx) {
9191
const t0 = _template("<div></div>")
9292
const n0 = t0()
9393
const { 0: [n1],} = _children(n0)
94-
_effect(() => {
94+
_watchEffect(() => {
9595
_setAttr(n1, "id", undefined, _ctx.id)
9696
})
9797
return n0

packages/compiler-vapor/__tests__/transforms/__snapshots__/vHtml.spec.ts.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`v-html > should convert v-html to innerHTML 1`] = `
4-
"import { template as _template, children as _children, effect as _effect, setHtml as _setHtml } from 'vue/vapor';
4+
"import { template as _template, children as _children, watchEffect as _watchEffect, setHtml as _setHtml } from 'vue/vapor';
55
66
export function render(_ctx) {
77
const t0 = _template("<div></div>")
88
const n0 = t0()
99
const { 0: [n1],} = _children(n0)
10-
_effect(() => {
10+
_watchEffect(() => {
1111
_setHtml(n1, undefined, _ctx.code)
1212
})
1313
return n0
1414
}"
1515
`;
1616

1717
exports[`v-html > should raise error and ignore children when v-html is present 1`] = `
18-
"import { template as _template, children as _children, effect as _effect, setHtml as _setHtml } from 'vue/vapor';
18+
"import { template as _template, children as _children, watchEffect as _watchEffect, setHtml as _setHtml } from 'vue/vapor';
1919
2020
export function render(_ctx) {
2121
const t0 = _template("<div></div>")
2222
const n0 = t0()
2323
const { 0: [n1],} = _children(n0)
24-
_effect(() => {
24+
_watchEffect(() => {
2525
_setHtml(n1, undefined, _ctx.test)
2626
})
2727
return n0

packages/compiler-vapor/__tests__/transforms/__snapshots__/vOn.spec.ts.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export function render(_ctx) {
1313
`;
1414

1515
exports[`v-on > dynamic arg 1`] = `
16-
"import { template as _template, children as _children, effect as _effect, on as _on } from 'vue/vapor';
16+
"import { template as _template, children as _children, watchEffect as _watchEffect, on as _on } from 'vue/vapor';
1717
1818
export function render(_ctx) {
1919
const t0 = _template("<div></div>")
2020
const n0 = t0()
2121
const { 0: [n1],} = _children(n0)
22-
_effect(() => {
22+
_watchEffect(() => {
2323
_on(n1, _ctx.event, (...args) => (_ctx.handler && _ctx.handler(...args)))
2424
})
2525
return n0
@@ -109,13 +109,13 @@ export function render(_ctx) {
109109
`;
110110

111111
exports[`v-on > should transform click.middle 2`] = `
112-
"import { template as _template, children as _children, effect as _effect, on as _on, withModifiers as _withModifiers } from 'vue/vapor';
112+
"import { template as _template, children as _children, watchEffect as _watchEffect, on as _on, withModifiers as _withModifiers } from 'vue/vapor';
113113
114114
export function render(_ctx) {
115115
const t0 = _template("<div></div>")
116116
const n0 = t0()
117117
const { 0: [n1],} = _children(n0)
118-
_effect(() => {
118+
_watchEffect(() => {
119119
_on(n1, (_ctx.event) === "click" ? "mouseup" : (_ctx.event), _withModifiers((...args) => (_ctx.test && _ctx.test(...args)), ["middle"]))
120120
})
121121
return n0
@@ -135,13 +135,13 @@ export function render(_ctx) {
135135
`;
136136

137137
exports[`v-on > should transform click.right 2`] = `
138-
"import { template as _template, children as _children, effect as _effect, on as _on, withKeys as _withKeys, withModifiers as _withModifiers } from 'vue/vapor';
138+
"import { template as _template, children as _children, watchEffect as _watchEffect, on as _on, withKeys as _withKeys, withModifiers as _withModifiers } from 'vue/vapor';
139139
140140
export function render(_ctx) {
141141
const t0 = _template("<div></div>")
142142
const n0 = t0()
143143
const { 0: [n1],} = _children(n0)
144-
_effect(() => {
144+
_watchEffect(() => {
145145
_on(n1, (_ctx.event) === "click" ? "contextmenu" : (_ctx.event), _withKeys(_withModifiers((...args) => (_ctx.test && _ctx.test(...args)), ["right"]), ["right"]))
146146
})
147147
return n0

packages/compiler-vapor/__tests__/transforms/__snapshots__/vText.spec.ts.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`v-text > should convert v-text to textContent 1`] = `
4-
"import { template as _template, children as _children, effect as _effect, setText as _setText } from 'vue/vapor';
4+
"import { template as _template, children as _children, watchEffect as _watchEffect, setText as _setText } from 'vue/vapor';
55
66
export function render(_ctx) {
77
const t0 = _template("<div></div>")
88
const n0 = t0()
99
const { 0: [n1],} = _children(n0)
10-
_effect(() => {
10+
_watchEffect(() => {
1111
_setText(n1, undefined, _ctx.str)
1212
})
1313
return n0
1414
}"
1515
`;
1616

1717
exports[`v-text > should raise error and ignore children when v-text is present 1`] = `
18-
"import { template as _template, children as _children, effect as _effect, setText as _setText } from 'vue/vapor';
18+
"import { template as _template, children as _children, watchEffect as _watchEffect, setText as _setText } from 'vue/vapor';
1919
2020
export function render(_ctx) {
2121
const t0 = _template("<div></div>")
2222
const n0 = t0()
2323
const { 0: [n1],} = _children(n0)
24-
_effect(() => {
24+
_watchEffect(() => {
2525
_setText(n1, undefined, _ctx.test)
2626
})
2727
return n0

0 commit comments

Comments
 (0)