Skip to content

Commit

Permalink
fix: errors when VideoRecorder is unmounted while setting up (#100)
Browse files Browse the repository at this point in the history
Co-authored-by: = <=>
  • Loading branch information
suchk authored and fbaiodias committed May 3, 2021
1 parent 0b0797d commit d416ec0
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/video-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export default class VideoRecorder extends Component {

videoInput = React.createRef()

isComponentUnmounted = false

timeSinceInactivity = 0

state = {
Expand Down Expand Up @@ -196,6 +198,7 @@ export default class VideoRecorder extends Component {

componentWillUnmount () {
this.turnOffCamera()
this.isComponentUnmounted = true
}

turnOnCamera = () => {
Expand Down Expand Up @@ -247,6 +250,12 @@ export default class VideoRecorder extends Component {
}

handleSuccess = (stream) => {
// Since handleSuccess is an async function, we may be in a situation where this was called after the
// component was unmounted
if (this.isComponentUnmounted) {
return
}

this.stream = stream
this.setState({
isCameraOn: true,
Expand Down Expand Up @@ -276,14 +285,18 @@ export default class VideoRecorder extends Component {
handleError = (err) => {
const { onError } = this.props

console.error('Captured error', err)

clearTimeout(this.timeLimitTimeout)

if (onError) {
onError(err)
}

if (this.isComponentUnmounted) {
return
}

console.error('Captured error', err)

clearTimeout(this.timeLimitTimeout)

this.setState({
isConnecting: this.state.isConnecting && false,
isRecording: false,
Expand Down

1 comment on commit d416ec0

@vercel
Copy link

@vercel vercel bot commented on d416ec0 May 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.