Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Added optional onDraw method to play to run after the _render call. #266

Merged
merged 4 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
✨ Added optional onDraw method to run after the _render call.
  • Loading branch information
abidjappie committed Feb 26, 2024
commit 7e712fb80f6f7c7f47eca92e0b08fd547ad263a5
18 changes: 18 additions & 0 deletions spec/unit/movie/movie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ describe('Unit Tests ->', function () {
expect(layer.stop).toHaveBeenCalledTimes(0)
})

it('should call user provided `onDraw` after drawing', async function () {
// 1a. Force currentTime to 0
mockTime(0)

// 1b. Layer must be inactive to start
const layer = movie.layers[0]
layer.active = false

// 2a. Prepare options object with onDraw callback
const options = jasmine.createSpyObj('options', ['onDraw'])

// 2. Play one frame at the beginning of the movie with the spy options
await movie.play(options)

// 3. Make sure onDraw was called
expect(options.onDraw).toHaveBeenCalledTimes(1)
})

it('should be able to operate after a layer has been deleted', async function () {
mockTime()

Expand Down
7 changes: 6 additions & 1 deletion src/movie/movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export class Movie {
* @return Fulfilled when the movie is done playing, never fails
*/
async play (options: {
onDraw?: () => void,
clabe45 marked this conversation as resolved.
Show resolved Hide resolved
onStart?: () => void,
duration?: number,
} = {}): Promise<void> {
Expand All @@ -152,7 +153,11 @@ export class Movie {
await new Promise<void>(resolve => {
if (!this.renderingFrame) {
// Not rendering (and not playing), so play.
this._render(undefined, resolve)
this._render(undefined, () => {
// Call optional onDraw callback when render is complete
options.onDraw?.()
return resolve()
})
}

// Stop rendering frame if currently doing so, because playing has higher
Expand Down