Skip to content

Commit

Permalink
fix: [fixes #175] Do not resume complete Tweenables in Scene (#176)
Browse files Browse the repository at this point in the history
* fix: [#175] don't resume completed tweenables in scene
* test: validate that stopped tweenables are not resumed by scene.resume
  • Loading branch information
jeremyckahn committed Mar 21, 2023
1 parent ba285da commit e819bdc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export class Scene {
return [...this.#tweenables]
}

/**
* A list of {@link Tweenable}s in the scene that have not yet ended (playing
* or not).
* @member Scene#playingTweenables
* @type {Array.<Tweenable>}
*/
get playingTweenables() {
return this.#tweenables.filter(tweenable => !tweenable.hasEnded())
}

/**
* The {@link external:Promise}s for all {@link Tweenable}s in this
* {@link Scene} that have been configured with {@link
Expand Down Expand Up @@ -146,7 +156,7 @@ export class Scene {
* @return {Scene}
*/
resume() {
this.#tweenables.forEach(tweenable => tweenable.resume())
this.playingTweenables.forEach(tweenable => tweenable.resume())

return this
}
Expand Down
21 changes: 20 additions & 1 deletion src/scene.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,32 @@ describe('resume', () => {
scene = new Scene(new Tweenable(), new Tweenable())
})

test('resumes all Tweenables', () => {
test('resumes Tweenables', () => {
scene.play()
scene.pause()
scene.resume()

expect(scene.isPlaying()).toBeTruthy()
})

test('does not resume Tweenables that have ended', () => {
const tweenable1 = new Tweenable()
const tweenable2 = new Tweenable()

scene = new Scene(tweenable1, tweenable2)

jest.spyOn(tweenable1, 'hasEnded').mockReturnValue(false)
jest.spyOn(tweenable1, 'resume')
jest.spyOn(tweenable2, 'hasEnded').mockReturnValue(true)
jest.spyOn(tweenable2, 'resume')

scene.play()
scene.pause()
scene.resume()

expect(tweenable1.resume).toHaveBeenCalled()
expect(tweenable2.resume).not.toHaveBeenCalled()
})
})

describe('stop', () => {
Expand Down

0 comments on commit e819bdc

Please sign in to comment.