Skip to content

Commit 6c2b3ba

Browse files
committed
improve v-effect + expose $nextTick in expressions
- v-effect support arbitrary statements including `if` conditions - v-effect now fire after DOM updates
1 parent 71aefbf commit 6c2b3ba

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

examples/todomvc.html

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</style>
1010

1111
<script type="module">
12-
import { createApp, reactive } from '../src'
12+
import { createApp } from '../src'
1313

1414
const STORAGE_KEY = 'todos-petite-vue'
1515
const todoStorage = {
@@ -42,7 +42,7 @@
4242
}
4343
}
4444

45-
const state = reactive({
45+
createApp({
4646
todos: todoStorage.fetch(),
4747
newTodo: '',
4848
editedTodo: null,
@@ -129,15 +129,7 @@
129129
pluralize(n) {
130130
return n === 1 ? 'item' : 'items'
131131
}
132-
})
133-
134-
createApp(state)
135-
.directive('todo-focus', ({ el, get, effect }) => {
136-
effect(() => {
137-
if (get()) el.focus()
138-
})
139-
})
140-
.mount('#app')
132+
}).mount('#app')
141133
</script>
142134

143135
<div id="app" @mounted="setupRouting" v-effect="save()" v-cloak>
@@ -177,7 +169,7 @@ <h1>todos</h1>
177169
class="edit"
178170
type="text"
179171
v-model="todo.title"
180-
v-todo-focus="todo === editedTodo"
172+
v-effect="if (todo === editedTodo) $el.focus()"
181173
@blur="doneEdit(todo)"
182174
@keyup.enter="doneEdit(todo)"
183175
@keyup.escape="cancelEdit(todo)"

src/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Block } from './block'
33
import { Directive } from './directives'
44
import { createContext } from './walk'
55
import { toDisplayString } from './directives/text'
6+
import { nextTick } from './scheduler'
67

78
export const createApp = (initialData?: any) => {
89
// root context
@@ -13,6 +14,7 @@ export const createApp = (initialData?: any) => {
1314

1415
// global internal helpers
1516
ctx.scope.$s = toDisplayString
17+
ctx.scope.$nextTick = nextTick
1618

1719
let rootBlocks: Block[]
1820

src/directives/effect.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Directive } from '.'
2+
import { execute } from '../eval'
3+
import { nextTick } from '../scheduler'
24

3-
export const effect: Directive = ({ get, effect }) => {
4-
effect(get)
5+
export const effect: Directive = ({ el, ctx, exp, effect }) => {
6+
nextTick(() => effect(() => execute(ctx.scope, exp, el)))
57
}

src/eval.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const evalCache: Record<string, Function> = Object.create(null)
22

3-
export const evaluate = (scope: any, exp: string, el?: Node) => {
3+
export const evaluate = (scope: any, exp: string, el?: Node) =>
4+
execute(scope, `return(${exp})`, el)
5+
6+
export const execute = (scope: any, exp: string, el?: Node) => {
47
const fn = evalCache[exp] || (evalCache[exp] = toFunction(exp))
58
try {
69
return fn(scope, el)
@@ -14,7 +17,7 @@ export const evaluate = (scope: any, exp: string, el?: Node) => {
1417

1518
const toFunction = (exp: string): Function => {
1619
try {
17-
return new Function(`$data`, `$el`, `with ($data) { return (${exp}) }`)
20+
return new Function(`$data`, `$el`, `with($data){${exp}}`)
1821
} catch (e) {
1922
console.error(`${e.message} in expression: ${exp}`)
2023
return () => {}

src/walk.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,15 @@ const applyDirective = (
183183
modifiers?: Record<string, true>
184184
) => {
185185
const get = (e = exp) => evaluate(ctx.scope, e, el)
186-
const cleanup = dir({ el, get, effect: ctx.effect, ctx, exp, arg, modifiers })
186+
const cleanup = dir({
187+
el,
188+
get,
189+
effect: ctx.effect,
190+
ctx,
191+
exp,
192+
arg,
193+
modifiers
194+
})
187195
if (cleanup) {
188196
ctx.cleanups.push(cleanup)
189197
}

0 commit comments

Comments
 (0)