Skip to content

Commit 83a3e3a

Browse files
huyenltnguyenahmaxed
authored andcommitted
Use react-page-visibility
1 parent eb85453 commit 83a3e3a

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"react-device-detect": "^1.7.5",
1111
"react-dom": "^16.8.6",
1212
"react-hotkeys": "^2.0.0",
13+
"react-page-visibility": "^6.3.0",
1314
"react-scripts": "3.0.1",
1415
"store": "^2.0.12"
1516
},

src/components/Visualizer.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import PageVisibility from 'react-page-visibility';
34

45
const DELAY = 500;
56

@@ -15,7 +16,8 @@ export default class Visualizer extends React.PureComponent {
1516
baseColour: 'rgb(10, 10, 35)',
1617
translucent: 'rgba(10, 10, 35, 0.6)',
1718
multiplier: 0.7529
18-
}
19+
},
20+
isTabVisible: true,
1921
};
2022
}
2123

@@ -24,21 +26,31 @@ export default class Visualizer extends React.PureComponent {
2426
// of the audio context stuff AFTER the audio has been triggered.
2527
// We can't see it until
2628
// then anyway so it makes no difference to desktop.
27-
componentDidUpdate(prevProps) {
28-
if (prevProps.playing !== this.props.playing) {
29-
if (this.props.playing) {
29+
componentDidUpdate(prevProps, prevState) {
30+
if (prevProps.playing === this.props.playing && prevState.isTabVisible === this.state.isTabVisible) {
31+
return
32+
}
33+
34+
// If the player is playing and the tab is being active,
35+
// draw the visualization
36+
if (this.props.playing && this.state.isTabVisible) {
37+
// Create a new audio context if there isn't one available
38+
if (!this.state.eq.context) {
3039
this.initiateEQ();
31-
this.createVisualizer();
32-
this.startDrawing();
33-
} else {
34-
// Workaround componentWillUnmount to delay the clean up and achieve fadeout animation
35-
setTimeout(() => {
36-
// Note: Order matters.
37-
// Stop the drawing loop first (using this.rafId), then set the ID to null
38-
this.stopDrawing();
39-
this.reset();
40-
}, DELAY);
4140
}
41+
this.createVisualizer();
42+
this.startDrawing();
43+
}
44+
// If the player is not playing or the tab is running in the background,
45+
// stop the animation
46+
else {
47+
// Workaround for componentWillUnmount to delay the clean up and achieve fadeout animation
48+
setTimeout(() => {
49+
// Note: Order matters.
50+
// Stop the drawing loop first (using this.rafId), then set the ID to null
51+
this.stopDrawing();
52+
this.reset();
53+
}, DELAY);
4254
}
4355
}
4456

@@ -167,11 +179,17 @@ export default class Visualizer extends React.PureComponent {
167179
this.visualizer.ctx.fill();
168180
}
169181

182+
handleVisibilityChange = isTabVisible => {
183+
this.setState({ isTabVisible });
184+
}
185+
170186
render() {
171187
return (
172-
<div id='visualizer'>
173-
<canvas aria-label='visualizer' ref={a => (this._canvas = a)} />
174-
</div>
188+
<PageVisibility onChange={this.handleVisibilityChange}>
189+
<div id='visualizer'>
190+
<canvas aria-label='visualizer' ref={a => (this._canvas = a)} />
191+
</div>
192+
</PageVisibility>
175193
);
176194
}
177195
}

0 commit comments

Comments
 (0)