Skip to content

Commit 356935c

Browse files
Merge pull request #126 from LottieFiles/fix/load-event
fix: re-arranged events to fire when state is set
2 parents 46f8d7c + c07b6e6 commit 356935c

File tree

3 files changed

+86
-66
lines changed

3 files changed

+86
-66
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 3.5.1
4+
5+
### Patch Changes
6+
7+
- f807fde: re-arranged events to fire when internal state is set
8+
39
## 3.5.0
410

511
### Minor Changes

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lottiefiles/react-lottie-player",
3-
"version": "3.5.0",
3+
"version": "3.5.1",
44
"description": "Lottie web player wrapper for React",
55
"main": "dist/lottie-react.js",
66
"module": "dist/lottie-react.esm.js",
@@ -85,9 +85,9 @@
8585
"lottie-web": "^5.10.0"
8686
},
8787
"scripts": {
88-
"predev": "node -p \"'export const LOTTIE_PLAYER_VERSION = ' + JSON.stringify(require('./package.json').version) + '; \\n' + 'export const LOTTIE_WEB_VERSION = ' + JSON.stringify(require('./package.json').dependencies['lottie-web']) + ';'\" > src/versions.ts",
88+
"predev": "node -p \"'export const REACT_LOTTIE_PLAYER_VERSION = ' + JSON.stringify(require('./package.json').version) + '; \\n' + 'export const LOTTIE_WEB_VERSION = ' + JSON.stringify(require('./package.json').dependencies['lottie-web']) + ';'\" > src/versions.ts",
8989
"dev": "rollup -c -w",
90-
"prebuild": "node -p \"'export const LOTTIE_PLAYER_VERSION = ' + JSON.stringify(require('./package.json').version) + '; \\n' + 'export const LOTTIE_WEB_VERSION = ' + JSON.stringify(require('./package.json').dependencies['lottie-web']) + ';'\" > src/versions.ts",
90+
"prebuild": "node -p \"'export const REACT_LOTTIE_PLAYER_VERSION = ' + JSON.stringify(require('./package.json').version) + '; \\n' + 'export const LOTTIE_WEB_VERSION = ' + JSON.stringify(require('./package.json').dependencies['lottie-web']) + ';'\" > src/versions.ts",
9191
"build": "rollup -c",
9292
"prepublishOnly": "yarn build",
9393
"run-tests": "while ! nc -z localhost 8080 ; do sleep 1 ; done && yarn run start-cypress && npx nyc report --reporter=text-summary",

src/Player.tsx

Lines changed: 77 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import lottie, { AnimationItem } from 'lottie-web';
22
import * as React from 'react';
33

4-
import { LOTTIE_PLAYER_VERSION, LOTTIE_WEB_VERSION } from './versions';
4+
import { LOTTIE_WEB_VERSION, REACT_LOTTIE_PLAYER_VERSION } from './versions';
55

66
/**
77
* Parse a resource into a JSON object or a URL string
@@ -139,7 +139,7 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
139139
public getVersions(): Versions {
140140
return {
141141
lottieWebVersion: LOTTIE_WEB_VERSION,
142-
lottiePlayerVersion: LOTTIE_PLAYER_VERSION,
142+
lottiePlayerVersion: REACT_LOTTIE_PLAYER_VERSION,
143143
};
144144
}
145145

@@ -311,8 +311,17 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
311311
let animationData = parseSrc(src);
312312

313313
if (typeof animationData === 'string') {
314-
const fetchResult = await fetch(animationData as string);
315-
animationData = await fetchResult.json();
314+
const fetchResult = await fetch(animationData as string).catch(() => {
315+
this.setState({ playerState: PlayerState.Error });
316+
this.triggerEvent(PlayerEvent.Error);
317+
throw new Error('Error @LottieFiles/lottie-react: Animation data could not be fetched.');
318+
});
319+
320+
animationData = await fetchResult.json().catch(() => {
321+
this.setState({ playerState: PlayerState.Error });
322+
this.triggerEvent(PlayerEvent.Error);
323+
throw new Error('Error @LottieFiles/lottie-react: Animation data could not be fetched.');
324+
});
316325
}
317326

318327
// Clear previous animation, if any
@@ -334,83 +343,88 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
334343
}
335344
this.setState({ animationData });
336345

337-
// Handle new frame event
338-
newInstance.addEventListener('enterFrame', () => {
339-
this.triggerEvent(PlayerEvent.Frame);
346+
this.setState({ instance: newInstance }, () => {
347+
if (typeof lottieRef === 'function') {
348+
lottieRef(newInstance);
349+
}
350+
351+
// Handle new frame event
352+
newInstance.addEventListener('enterFrame', () => {
353+
this.triggerEvent(PlayerEvent.Frame);
340354

341-
this.setState({
342-
seeker: Math.floor((newInstance as any).currentFrame),
355+
this.setState({
356+
seeker: Math.floor((newInstance as any).currentFrame),
357+
});
343358
});
344-
});
345359

346-
// Handle lottie-web ready event
347-
newInstance.addEventListener('DOMLoaded', () => {
348-
this.triggerEvent(PlayerEvent.Load);
349-
});
360+
// Handle lottie-web ready event
361+
newInstance.addEventListener('DOMLoaded', () => {
362+
this.triggerEvent(PlayerEvent.Load);
350363

351-
// Handle animation data load complete
352-
newInstance.addEventListener('data_ready', () => {
353-
this.triggerEvent(PlayerEvent.Ready);
354-
});
364+
if (autoplay) {
365+
this.play();
366+
}
367+
});
355368

356-
// Set error state when animation load fail event triggers
357-
newInstance.addEventListener('data_failed', () => {
358-
this.setState({ playerState: PlayerState.Error });
359-
});
369+
// Handle animation data load complete
370+
newInstance.addEventListener('data_ready', () => {
371+
this.triggerEvent(PlayerEvent.Ready);
372+
});
360373

361-
// Handle new loop event
362-
newInstance.addEventListener('loopComplete', () => {
363-
this.triggerEvent(PlayerEvent.Loop);
364-
});
374+
// Set error state when animation load fail event triggers
375+
newInstance.addEventListener('data_failed', () => {
376+
this.setState({ playerState: PlayerState.Error });
377+
this.triggerEvent(PlayerEvent.Error);
378+
});
365379

366-
// Set state to paused if loop is off and anim has completed
367-
newInstance.addEventListener('complete', () => {
368-
this.triggerEvent(PlayerEvent.Complete);
369-
this.setState({ playerState: PlayerState.Paused });
380+
// Handle new loop event
381+
newInstance.addEventListener('loopComplete', () => {
382+
this.triggerEvent(PlayerEvent.Loop);
383+
});
370384

371-
if (!this.props.keepLastFrame || this.props.loop) {
372-
this.setSeeker(0);
373-
}
374-
});
385+
// Set state to paused if loop is off and anim has completed
386+
newInstance.addEventListener('complete', () => {
387+
this.triggerEvent(PlayerEvent.Complete);
388+
this.setState({ playerState: PlayerState.Paused });
375389

376-
// Set handlers to auto play animation on hover if enabled
377-
this.container.addEventListener('mouseenter', () => {
378-
if (hover && this.state.playerState !== PlayerState.Playing) {
379-
if (this.props.keepLastFrame) {
380-
this.stop();
390+
if (!this.props.keepLastFrame || this.props.loop) {
391+
this.setSeeker(0);
381392
}
382-
this.play();
383-
}
384-
});
385-
this.container.addEventListener('mouseleave', () => {
386-
if (hover && this.state.playerState === PlayerState.Playing) {
387-
this.stop();
388-
}
389-
});
390-
391-
// Set initial playback speed and direction
392-
if (speed) {
393-
this.setPlayerSpeed(speed);
394-
}
393+
});
395394

396-
if (direction) {
397-
this.setPlayerDirection(direction);
398-
}
395+
// Set handlers to auto play animation on hover if enabled
396+
if (this.container) {
397+
this.container.addEventListener('mouseenter', () => {
398+
if (hover && this.state.playerState !== PlayerState.Playing) {
399+
if (this.props.keepLastFrame) {
400+
this.stop();
401+
}
402+
this.play();
403+
}
404+
});
405+
this.container.addEventListener('mouseleave', () => {
406+
if (hover && this.state.playerState === PlayerState.Playing) {
407+
this.stop();
408+
}
409+
});
410+
}
399411

400-
if (background) {
401-
this.setState({ background });
402-
}
412+
// Set initial playback speed and direction
413+
if (speed) {
414+
this.setPlayerSpeed(speed);
415+
}
403416

404-
this.setState({ instance: newInstance }, () => {
405-
if (typeof lottieRef === 'function') {
406-
lottieRef(newInstance);
417+
if (direction) {
418+
this.setPlayerDirection(direction);
407419
}
408-
if (autoplay) {
409-
this.play();
420+
421+
if (background) {
422+
this.setState({ background });
410423
}
411424
});
412425
} catch (e) {
413426
this.setState({ playerState: PlayerState.Error });
427+
this.triggerEvent(PlayerEvent.Error);
414428
}
415429
}
416430

0 commit comments

Comments
 (0)