Skip to content

Commit 4f695cf

Browse files
author
Evan You
committed
animation use batcher - existing tests pass
1 parent 8fff82e commit 4f695cf

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

src/batcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function Batcher () {
77
var BatcherProto = Batcher.prototype
88

99
BatcherProto.push = function (job) {
10-
if (!this.has[job.id]) {
10+
if (!job.id || !this.has[job.id]) {
1111
this.queue.push(job)
1212
this.has[job.id] = job
1313
if (!this.waiting) {

src/binding.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var Batcher = require('./batcher'),
22
bindingBatcher = new Batcher(),
3-
bindingId = 0
3+
bindingId = 1
44

55
/**
66
* Binding class.

src/transition.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var endEvents = sniffEndEvents(),
22
config = require('./config'),
3+
// batch enter animations so we only force the layout once
4+
Batcher = require('./batcher'),
5+
batcher = new Batcher(),
36
// exit codes for testing
47
codes = {
58
CSS_E : 1,
@@ -14,6 +17,12 @@ var endEvents = sniffEndEvents(),
1417
SKIP : -6
1518
}
1619

20+
// force a layout so transition can be triggered
21+
batcher._preFlush = function () {
22+
/* jshint unused: false */
23+
var forceLayout = document.body.clientHeight
24+
}
25+
1726
/**
1827
* stage:
1928
* 1 = enter
@@ -70,19 +79,19 @@ function applyTransitionClass (el, stage, changeState, animation) {
7079

7180
// if the browser supports transition,
7281
// it must have classList...
73-
var onEnd,
74-
classList = el.classList,
75-
lastLeaveCallback = el.vue_trans_cb,
76-
enterClass = config.enterClass,
77-
leaveClass = config.leaveClass,
78-
isAnimation = animation === '',
82+
var onEnd, job,
83+
classList = el.classList,
84+
existingCallback = el.vue_trans_cb,
85+
enterClass = config.enterClass,
86+
leaveClass = config.leaveClass,
87+
isAnimation = animation === '',
7988
endEvent = isAnimation
8089
? endEvents.anim
8190
: endEvents.trans
8291

83-
// cancel unfinished leave transition
84-
if (lastLeaveCallback) {
85-
el.removeEventListener(endEvent, lastLeaveCallback)
92+
// cancel unfinished callbacks and jobs
93+
if (existingCallback) {
94+
el.removeEventListener(endEvent, existingCallback)
8695
classList.remove(enterClass)
8796
classList.remove(leaveClass)
8897
el.vue_trans_cb = null
@@ -96,24 +105,27 @@ function applyTransitionClass (el, stage, changeState, animation) {
96105
}
97106
// append
98107
changeState()
99-
// force a layout so transition can be triggered
100-
/* jshint unused: false */
101-
var forceLayout = el.clientHeight
108+
job = {}
102109
// trigger transition
103110
if (!isAnimation) {
104-
classList.remove(enterClass)
111+
job.execute = function () {
112+
classList.remove(enterClass)
113+
}
105114
} else {
106-
classList.add(enterClass)
107115
onEnd = function (e) {
108116
if (e.target === el) {
109117
el.removeEventListener(endEvent, onEnd)
110118
el.vue_trans_cb = null
111119
classList.remove(enterClass)
112120
}
113121
}
114-
el.addEventListener(endEvent, onEnd)
115-
el.vue_trans_cb = onEnd
122+
job.execute = function () {
123+
classList.add(enterClass)
124+
el.addEventListener(endEvent, onEnd)
125+
el.vue_trans_cb = onEnd
126+
}
116127
}
128+
batcher.push(job)
117129
return codes.CSS_E
118130

119131
} else { // leave

src/viewmodel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var Compiler = require('./compiler'),
88

99
// batch $watch callbacks
1010
watcherBatcher = new Batcher(),
11-
watcherId = 0
11+
watcherId = 1
1212

1313
/**
1414
* ViewModel exposed to the user that holds data,

test/functional/fixtures/animation.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script src="../dist/vue.js"></script>
1+
<script src="../../../dist/vue.js"></script>
22
<style type="text/css">
33
h1 {
44
display: inline-block;
@@ -70,4 +70,10 @@ <h1 v-animation v-if="ok">Hahahah</h1>
7070
ok: true
7171
}
7272
})
73+
setTimeout(function () {
74+
test.ok = !test.ok
75+
setTimeout(function () {
76+
test.ok = !test.ok
77+
}, 100)
78+
}, 100)
7379
</script>

test/unit/specs/transition.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ describe('UNIT: Transition', function () {
8484
assert.notOk(el.classList.contains(leaveClass))
8585
})
8686

87-
it('should remove the class afterwards', function () {
88-
assert.notOk(el.classList.contains(enterClass))
87+
it('should remove the class afterwards', function (done) {
88+
Vue.nextTick(function () {
89+
assert.notOk(el.classList.contains(enterClass))
90+
done()
91+
})
8992
})
9093

9194
it('should return correct code', function () {

0 commit comments

Comments
 (0)