Skip to content

Commit 63e9d72

Browse files
author
Evan You
committed
wrap setTimeout in JavaScript effect functions
1 parent bda8e76 commit 63e9d72

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/transition.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ var endEvents = sniffEndEvents(),
33
// batch enter animations so we only force the layout once
44
Batcher = require('./batcher'),
55
batcher = new Batcher(),
6+
// cache timer functions
7+
setTO = window.setTimeout,
8+
clearTO = window.clearTimeout,
69
// exit codes for testing
710
codes = {
811
CSS_E : 1,
@@ -164,21 +167,42 @@ function applyTransitionFunctions (el, stage, changeState, effectId, compiler) {
164167
}
165168

166169
var enter = funcs.enter,
167-
leave = funcs.leave
170+
leave = funcs.leave,
171+
timeouts = el.vue_timeouts
172+
173+
// clear previous timeouts
174+
if (timeouts) {
175+
var i = timeouts.length
176+
while (i--) {
177+
clearTO(timeouts[i])
178+
}
179+
}
180+
181+
timeouts = el.vue_timeouts = []
182+
function timeout (cb, delay) {
183+
var id = setTO(function () {
184+
cb()
185+
timeouts.splice(timeouts.indexOf(id), 1)
186+
if (!timeouts.length) {
187+
el.vue_timeouts = null
188+
}
189+
}, delay)
190+
timeouts.push(id)
191+
}
168192

169193
if (stage > 0) { // enter
170194
if (typeof enter !== 'function') {
171195
changeState()
172196
return codes.JS_SKIP_E
173197
}
174-
enter(el, changeState)
198+
enter(el, changeState, timeout)
175199
return codes.JS_E
176200
} else { // leave
177201
if (typeof leave !== 'function') {
178202
changeState()
179203
return codes.JS_SKIP_L
180204
}
181-
leave(el, changeState)
205+
leave(el, changeState, timeout)
182206
return codes.JS_L
183207
}
184208

test/unit/specs/transition.js

+29
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,35 @@ describe('UNIT: Transition', function () {
292292

293293
})
294294

295+
describe('wrapped timeout', function () {
296+
297+
var el = mockEl('js'),
298+
c = mockChange(),
299+
timerFired = false,
300+
def = {
301+
enter: function (el, change, timeout) {
302+
change()
303+
timeout(function () {
304+
timerFired = true
305+
}, 0)
306+
},
307+
leave: function () {}
308+
},
309+
compiler = mockCompiler(def)
310+
311+
it('should cancel previous unfired timers', function (done) {
312+
transition(el, 1, c.change, compiler)
313+
assert.strictEqual(el.vue_timeouts.length, 1)
314+
transition(el, -1, c.change, compiler)
315+
assert.strictEqual(el.vue_timeouts.length, 0)
316+
setTimeout(function () {
317+
assert.notOk(timerFired)
318+
done()
319+
}, 0)
320+
})
321+
322+
})
323+
295324
})
296325

297326
function mockChange (change) {

0 commit comments

Comments
 (0)