-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Animate } from '../animate'; | ||
import { Queue } from '../queue'; | ||
import { easing as e } from '../easing'; | ||
|
||
describe('Queue', () => { | ||
it('should add animations to the queue and start the first one immediately', done => { | ||
const animate1 = new Animate({ | ||
from: 0, | ||
to: 100, | ||
duration: 50, | ||
easing: e.linear, | ||
}); | ||
|
||
const animate2 = new Animate({ | ||
from: 0, | ||
to: 100, | ||
duration: 50, | ||
easing: e.linear, | ||
}); | ||
|
||
const queue = new Queue(); | ||
|
||
queue.add(animate1); | ||
queue.add(animate2); | ||
|
||
let completeCount = 0; | ||
|
||
queue.on('complete', () => { | ||
completeCount++; | ||
if (completeCount === 2) { | ||
expect(completeCount).toBe(2); | ||
done(); | ||
} | ||
}); | ||
}); | ||
|
||
it('should handle stop correctly', done => { | ||
const animate1 = new Animate({ | ||
from: 0, | ||
to: 100, | ||
duration: 50, | ||
easing: e.linear, | ||
}); | ||
|
||
animate1.on('stop', () => { | ||
done(); | ||
}); | ||
|
||
const queue = new Queue(); | ||
|
||
queue.add(animate1); | ||
|
||
queue.stop(); | ||
}); | ||
|
||
it('should clear the queue and stop the current animation', done => { | ||
const animate1 = new Animate({ | ||
from: 0, | ||
to: 100, | ||
duration: 100, | ||
easing: e.linear, | ||
}); | ||
|
||
const animate2 = new Animate({ | ||
from: 0, | ||
to: 100, | ||
duration: 100, | ||
easing: e.linear, | ||
}); | ||
|
||
const queue = new Queue(); | ||
|
||
queue.add(animate1); | ||
queue.add(animate2); | ||
|
||
queue.clear(); | ||
|
||
expect(queue['animations'].length).toBe(0); | ||
done(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import EventEmitter from 'eventemitter3'; | ||
import { Animate } from './animate'; | ||
|
||
export class Queue extends EventEmitter { | ||
private animations: Animate[] = []; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
public add(animation: Animate) { | ||
this.animations.push(animation); | ||
animation.on('start', () => this.emit('start', animation)); | ||
animation.on('update', (value: any) => this.emit('update', value, animation)); | ||
animation.on('complete', () => this.onAnimationComplete(animation)); | ||
animation.on('stop', () => this.emit('stop', animation)); | ||
|
||
// If this is the first animation added and no other animation is running, start it immediately | ||
if (this.animations.length === 1) { | ||
this.startNextAnimation(); | ||
} | ||
} | ||
|
||
private startNextAnimation() { | ||
if (this.animations.length > 0) { | ||
const animation = this.animations[0]; | ||
animation.start(); | ||
} else { | ||
this.emit('complete'); // Emit complete event when all animations in the queue have completed | ||
} | ||
} | ||
|
||
private onAnimationComplete(animation: Animate) { | ||
this.emit('complete', animation); | ||
this.animations.shift(); // Remove the completed animation from the queue | ||
this.startNextAnimation(); // Start the next animation in the queue | ||
} | ||
|
||
public clear() { | ||
if (this.animations.length > 0) { | ||
this.animations[0].stop(); | ||
} | ||
this.animations = []; | ||
} | ||
|
||
public stop() { | ||
if (this.animations.length > 0) { | ||
this.animations[0].stop(); | ||
} | ||
} | ||
} |