robustify transition code a bit

pull/1971/head
Richard Harris 7 years ago
parent 699234ac41
commit f72e152316

@ -43,13 +43,15 @@ export function create_rule({ a, b, d, duration }, ease, fn) {
export function delete_rule(node, name) { export function delete_rule(node, name) {
node.style.animation = node.style.animation node.style.animation = node.style.animation
.split(', ') .split(', ')
.filter(anim => anim && anim.indexOf(name) === -1) .filter(anim => anim.indexOf(name) < 0)
.join(', '); .join(', ');
console.log({ active });
if (--active <= 0) clear_rules(); if (--active <= 0) clear_rules();
} }
export function clear_rules() { export function clear_rules() {
console.log(`clear rules`, active);
let i = stylesheet.cssRules.length; let i = stylesheet.cssRules.length;
while (i--) stylesheet.deleteRule(i); while (i--) stylesheet.deleteRule(i);
current_rules = {}; current_rules = {};

@ -34,6 +34,12 @@ export function create_transition(node, fn, params, intro) {
let running = false; let running = false;
let running_program = null; let running_program = null;
let pending_program = null; let pending_program = null;
let animation_name = null;
function clear_animation() {
if (animation_name) delete_rule(node, animation_name);
animation_name = null;
}
function start(program, delay, duration, easing) { function start(program, delay, duration, easing) {
node.dispatchEvent(new window.CustomEvent(`${program.b ? 'intro' : 'outro'}start`)); node.dispatchEvent(new window.CustomEvent(`${program.b ? 'intro' : 'outro'}start`));
@ -46,13 +52,10 @@ export function create_transition(node, fn, params, intro) {
if (config.css) { if (config.css) {
if (delay) node.style.cssText = cssText; if (delay) node.style.cssText = cssText;
program.name = create_rule(program, easing, config.css); clear_animation();
animation_name = create_rule(program, easing, config.css);
node.style.animation = (node.style.animation || '') node.style.animation = (node.style.animation ? ', ' : '') + `${animation_name} ${program.duration}ms linear 1 forwards`;
.split(', ')
.filter(anim => anim && (program.d < 0 || !/__svelte/.test(anim)))
.concat(`${program.name} ${program.duration}ms linear 1 forwards`)
.join(', ');
} }
running_program = program; running_program = program;
@ -72,14 +75,14 @@ export function create_transition(node, fn, params, intro) {
if (!program.b && !program.invalidated) { if (!program.b && !program.invalidated) {
program.group.callbacks.push(() => { program.group.callbacks.push(() => {
program.callback(); program.callback();
if (config.css) delete_rule(node, program.name); clear_animation();
}); });
if (--program.group.remaining === 0) { if (--program.group.remaining === 0) {
program.group.callbacks.forEach(run); program.group.callbacks.forEach(run);
} }
} else { } else {
if (config.css) delete_rule(node, program.name); clear_animation();
} }
running = !!pending_program; running = !!pending_program;
@ -157,16 +160,22 @@ export function create_transition(node, fn, params, intro) {
}, },
abort(reset) { abort(reset) {
if (reset && config.tick) config.tick(1, 0); if (reset) {
// if an outro was aborted by an intro, we need
if (running_program) { // to reset the node to its initial state
if (config.css) delete_rule(node, running_program.name); if (config.tick) config.tick(1, 0);
running_program = pending_program = null;
running = false;
} }
clear_animation();
running_program = pending_program = null;
running = false;
}, },
invalidate() { invalidate() {
// invalidation happens when a (bidirectional) outro is interrupted by an
// intro — callbacks should not fire, as that would cause the nodes to
// be removed from the DOM
if (running_program) { if (running_program) {
running_program.invalidated = true; running_program.invalidated = true;
} }

Loading…
Cancel
Save