Skip to content

Commit 271ac42

Browse files
author
daniel-lundin
committed
Added gc of finished animation on detached nodes
1 parent abd4e7c commit 271ac42

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

docs/snabbt.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/engine.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
'use strict';
22
/* global document, window */
3-
var stateFromOptions = require('./state.js').stateFromOptions;
4-
var Animation = require('./animation.js');
5-
var createState = require('./state.js').createState;
3+
const stateFromOptions = require('./state.js').stateFromOptions;
4+
const Animation = require('./animation.js');
5+
const createState = require('./state.js').createState;
6+
const utils = require('./utils.js');
67

7-
var Engine = {
8+
const Engine = {
89
runningAnimations: [],
910
completedAnimations: [],
1011
transformProperty: 'transform',
1112
rAFScheduled: false,
1213
init() {
1314
if (typeof window !== undefined) return;
14-
var styles = window.getComputedStyle(document.documentElement, '');
15-
var vendorPrefix = (Array.prototype.slice
15+
const styles = window.getComputedStyle(document.documentElement, '');
16+
const vendorPrefix = (Array.prototype.slice
1617
.call(styles)
1718
.join('')
1819
.match(/-(moz|webkit|ms)-/) || styles.OLink === '' && ['', 'o']
@@ -34,8 +35,8 @@ var Engine = {
3435

3536
stepAnimations(time) {
3637
this.runningAnimations.forEach((runningAnimation) => {
37-
var element = runningAnimation[0];
38-
var animation = runningAnimation[1];
38+
const element = runningAnimation[0];
39+
const animation = runningAnimation[1];
3940
this.stepAnimation(element, animation, time);
4041
});
4142

@@ -53,12 +54,12 @@ var Engine = {
5354
},
5455

5556
archiveCompletedAnimations() {
56-
var unFinished = this.runningAnimations.filter((animation) => !animation[1].completed());
57-
var finished = this.runningAnimations.filter((animation) => animation[1].completed());
57+
const unFinished = this.runningAnimations.filter((animation) => !animation[1].completed());
58+
const finished = this.runningAnimations.filter((animation) => animation[1].completed());
5859

59-
var queuedAnimations = this.createQueuedAnimations(finished);
60+
const queuedAnimations = this.createQueuedAnimations(finished);
6061
// Finished and not queued
61-
var completed = finished.filter((finishedAnimation) => {
62+
const completed = finished.filter((finishedAnimation) => {
6263
return !queuedAnimations.find((queuedAnimation) => {
6364
return queuedAnimation[0] !== finishedAnimation[0];
6465
});
@@ -79,11 +80,11 @@ var Engine = {
7980

8081
// Call complete callback
8182
finished.forEach((animation) => {
82-
var completeCallback = animation[1].options.complete;
83+
const completeCallback = animation[1].options.complete;
8384
if (completeCallback)
8485
completeCallback();
8586
});
86-
87+
//this.clearOphanedEndStates();
8788
},
8889

8990
createQueuedAnimations(finished) {
@@ -155,6 +156,12 @@ var Engine = {
155156
if (match) {
156157
return match[1].endState();
157158
}
159+
},
160+
161+
clearOphanedEndStates() {
162+
this.completedAnimations = this.completedAnimations.filter((animation) => {
163+
return utils.findUltimateAncestor(animation[0].body);
164+
});
158165
}
159166
};
160167

src/tests/engineTests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ const expect = require('chai').expect;
55
const Engine = require('../engine.js');
66
const Animation = require('../animation.js');
77
const createState = require('../state.js').createState;
8+
const utils = require('../utils.js');
89

910

1011
describe('Engine', () => {
12+
beforeEach(() => {
13+
const ultimateAncestor = {
14+
body: 'body'
15+
};
16+
sinon.stub(utils, 'findUltimateAncestor').returns(ultimateAncestor);
17+
});
18+
19+
afterEach(() => utils.findUltimateAncestor.restore());
1120

1221
describe('createAnimation', () => {
1322
const previousState = createState({ position: [1, 2, 3] });

src/utils.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ function cloneObject(object) {
3939
return clone;
4040
}
4141

42+
function findUltimateAncestor(node) {
43+
var ancestor = node;
44+
while (ancestor.parentNode) {
45+
ancestor = ancestor.parentNode;
46+
}
47+
return ancestor;
48+
}
49+
4250
module.exports = {
4351
optionOrDefault: optionOrDefault,
4452
updateElementTransform: updateElementTransform,
4553
updateElementProperties: updateElementProperties,
4654
isFunction: isFunction,
47-
cloneObject: cloneObject
55+
cloneObject: cloneObject,
56+
findUltimateAncestor: findUltimateAncestor
4857
};

0 commit comments

Comments
 (0)