Skip to content

Commit 084ad6e

Browse files
committed
Merge pull request #247 from dstockwell/newidle
Players should be constructed in idle state
2 parents 4da7bc0 + 0200e97 commit 084ad6e

11 files changed

+58
-36
lines changed

src/animation-constructor.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
else
3939
this.effect = new KeyframeEffect(effect);
4040
this._effect = effect;
41-
this._internalPlayer = null;
4241
this.activeDuration = shared.calculateActiveDuration(this._timing);
4342
return this;
4443
};

src/group-constructors.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
if (this._timing.duration === 'auto')
2323
this._timing.duration = this.activeDuration;
24-
this._internalPlayer = null;
2524
}
2625

2726
window.AnimationSequence = function() {

src/maxifill-player.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
this._childPlayers = [];
2525
this._callback = null;
2626
this._rebuildUnderlyingPlayer();
27+
// Players are constructed in the idle state.
28+
this._player.cancel();
2729
};
2830

2931
// TODO: add a source getter/setter
@@ -34,18 +36,16 @@
3436
this._player = null;
3537
}
3638

37-
if (!this.source) {
38-
return;
39-
}
40-
41-
if (this.source instanceof window.Animation) {
39+
if (!this.source || this.source instanceof window.Animation) {
4240
this._player = scope.newUnderlyingPlayerForAnimation(this.source);
4341
scope.bindPlayerForAnimation(this);
4442
}
4543
if (this.source instanceof window.AnimationSequence || this.source instanceof window.AnimationGroup) {
4644
this._player = scope.newUnderlyingPlayerForGroup(this.source);
4745
scope.bindPlayerForGroup(this);
4846
}
47+
48+
// FIXME: move existing currentTime/startTime/playState to new player
4949
},
5050
get paused() {
5151
return this._player.paused;
@@ -118,11 +118,7 @@
118118
},
119119
cancel: function() {
120120
this._player.cancel();
121-
if (this._callback) {
122-
this._register();
123-
this._callback._player = null;
124-
}
125-
this.source = null;
121+
this._register();
126122
this._removePlayers();
127123
},
128124
reverse: function() {

src/player.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@
7171
newTime = +newTime;
7272
if (isNaN(newTime))
7373
return;
74-
if (scope.restart())
75-
this._startTime = null;
74+
scope.restart();
7675
if (!this.paused && this._startTime != null) {
7776
this._startTime = this._timeline.currentTime - newTime / this._playbackRate;
7877
}
@@ -116,14 +115,11 @@
116115
this.paused = false;
117116
if (this.finished || this._idle) {
118117
this._currentTime = this._playbackRate > 0 ? 0 : this._totalDuration;
118+
this._startTime = null;
119119
scope.invalidateEffects();
120120
}
121121
this._finishedFlag = false;
122-
if (!scope.restart()) {
123-
this._startTime = this._timeline.currentTime - this._currentTime / this._playbackRate;
124-
}
125-
else
126-
this._startTime = null;
122+
scope.restart();
127123
this._idle = false;
128124
this._ensureAlive();
129125
},
@@ -149,6 +145,7 @@
149145
},
150146
reverse: function() {
151147
this._playbackRate *= -1;
148+
this._startTime = null;
152149
this.play();
153150
},
154151
addEventListener: function(type, handler) {

src/timeline.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
// TODO: Does this need to be sorted?
2626
// TODO: Do we need to consider needsRetick?
2727
getAnimationPlayers: function() {
28-
this.filterPlayers();
28+
this._discardPlayers();
2929
return this._players.slice();
3030
},
31-
filterPlayers: function() {
31+
_discardPlayers: function() {
3232
this._players = this._players.filter(function(player) {
3333
return player.playState != 'finished' && player.playState != 'idle';
3434
});
@@ -37,6 +37,7 @@
3737
var player = new scope.Player(source);
3838
this._players.push(player);
3939
scope.restartMaxifillTick();
40+
player.play();
4041
return player;
4142
},
4243
};
@@ -53,7 +54,7 @@
5354
function maxifillTick(t) {
5455
var timeline = window.document.timeline;
5556
timeline.currentTime = t;
56-
timeline.filterPlayers();
57+
timeline._discardPlayers();
5758
if (timeline._players.length == 0)
5859
ticking = false;
5960
else

test/js/animation-constructor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
suite('animation-constructor', function() {
22
setup(function() {
3-
document.timeline._players = [];
3+
document.timeline.getAnimationPlayers().forEach(function(player) {
4+
player.cancel();
5+
});
46
});
57

68
test('Playing an Animation makes a Player', function() {

test/js/group-constructors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ suite('group-constructors', function() {
1515

1616
test('player getter for children in groups, and __internalPlayer, work as expected', function() {
1717
var p = document.timeline.play(simpleAnimationGroup());
18+
tick(0);
1819
tick(100);
1920
assert.equal(p.source.player, p);
2021
assert.equal(p._childPlayers[0].source.player, p);

test/js/group-player-finish-event.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ suite('group-player-finish-event', function() {
4141
test('fire when reversed player completes', function(done) {
4242
this.player.onfinish = function(event) {
4343
assert.equal(event.currentTime, 0);
44-
assert.equal(event.timelineTime, 1000);
44+
assert.equal(event.timelineTime, 1001);
4545
done();
4646
};
4747
tick(0);
4848
tick(500);
4949
this.player.reverse();
50-
tick(1000);
50+
tick(501);
51+
tick(1001);
5152
});
5253

5354
test('fire after player is cancelled', function(done) {

test/js/player-finish-event.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ suite('player-finish-event', function() {
3333
test('fire when reversed player completes', function(done) {
3434
this.player.onfinish = function(event) {
3535
assert.equal(event.currentTime, 0);
36-
assert.equal(event.timelineTime, 1000);
36+
assert.equal(event.timelineTime, 1001);
3737
done();
3838
};
3939
tick(0);
4040
tick(500);
4141
this.player.reverse();
42-
tick(1000);
42+
tick(501);
43+
tick(1001);
4344
});
4445

4546
test('fire after player is cancelled', function(done) {

test/js/player.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ suite('player', function() {
3838
assert.equal(p.currentTime, null);
3939
tick(700);
4040
p.play();
41+
tick(700);
4142
assert.equal(p.currentTime, 1000);
4243
tick(800);
4344
assert.equal(p.currentTime, 1100);
@@ -75,6 +76,7 @@ suite('player', function() {
7576
assert.equal(p.currentTime, 300);
7677
assert.equal(p.playbackRate, 1);
7778
p.reverse();
79+
tick(600);
7880
assert.equal(p.startTime, 900);
7981
assert.equal(p.currentTime, 300);
8082
assert.equal(p.playbackRate, -1);
@@ -90,8 +92,8 @@ suite('player', function() {
9092
p.reverse();
9193
tick(601);
9294
tick(700);
93-
assert.equal(p.startTime, 1100);
94-
assert.equal(p.currentTime, 400);
95+
assert.equal(p.startTime, 1101);
96+
assert.equal(p.currentTime, 401);
9597
});
9698
test('reversing after finishing works as expected', function() {
9799
tick(90);
@@ -172,7 +174,8 @@ suite('player', function() {
172174
assert.equal(p.playbackRate, 1);
173175
setTicking(true);
174176
p.play();
175-
assert.equal(p.startTime, 2600);
177+
tick(2700);
178+
assert.equal(p.startTime, 2700);
176179
assert.equal(p.currentTime, 0);
177180
assert.equal(p.finished, false);
178181
assert.equal(p.playbackRate, 1);
@@ -183,14 +186,16 @@ suite('player', function() {
183186
tick(600);
184187
tick(700);
185188
p.reverse();
189+
tick(701);
186190
tick(900);
187-
assert.equal(p.startTime, 800);
191+
assert.equal(p.startTime, 801);
188192
assert.equal(p.currentTime, 0);
189193
assert.equal(p.finished, true);
190194
assert.equal(p.playbackRate, -1);
191195
setTicking(true);
192196
p.play();
193-
assert.equal(p.startTime, 3900);
197+
tick(1000);
198+
assert.equal(p.startTime, 4000);
194199
assert.equal(p.currentTime, 3000);
195200
assert.equal(p.finished, false);
196201
assert.equal(p.playbackRate, -1);
@@ -205,10 +210,11 @@ suite('player', function() {
205210
assert.equal(p.currentTime, 600);
206211
assert.equal(p.startTime, 300);
207212
p.reverse();
208-
assert.equal(p.startTime, 1500);
213+
tick(1000);
214+
assert.equal(p.startTime, 1600);
209215
p.currentTime = 300;
210216
assert.equal(p.currentTime, 300);
211-
assert.equal(p.startTime, 1200);
217+
assert.equal(p.startTime, 1300);
212218
});
213219
test('seeking while paused works as expected', function() {
214220
tick(790);
@@ -434,4 +440,22 @@ suite('player', function() {
434440
p.currentTime = undefined;
435441
assert.equal(p.currentTime, 10);
436442
});
443+
test('play() should not set a start time', function() {
444+
var p = document.body.animate([], 1000);
445+
p.cancel();
446+
assert.equal(p.startTime, null);
447+
assert.equal(p.playState, 'idle');
448+
p.play();
449+
assert.equal(p.startTime, null);
450+
assert.equal(p.playState, 'pending');
451+
});
452+
test('reverse() should not set a start time', function() {
453+
var p = document.body.animate([], 1000);
454+
p.cancel();
455+
assert.equal(p.startTime, null);
456+
assert.equal(p.playState, 'idle');
457+
p.reverse();
458+
assert.equal(p.startTime, null);
459+
assert.equal(p.playState, 'pending');
460+
});
437461
});

0 commit comments

Comments
 (0)