-
Notifications
You must be signed in to change notification settings - Fork 411
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
fixes #287 Unable to stop the camera when route changed #357
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import * as React from 'react'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import { styles } from './styles'; | ||
import { useQrReader } from './hooks'; | ||
|
@@ -16,12 +17,35 @@ export const QrReader: React.FC<QrReaderProps> = ({ | |
onResult, | ||
videoId, | ||
}) => { | ||
useQrReader({ | ||
const [stopRequested] = useQrReader({ | ||
constraints, | ||
scanDelay, | ||
onResult, | ||
videoId, | ||
}); | ||
const isUnmounting = useRef(false); | ||
|
||
useEffect( | ||
() => () => { | ||
if (isUnmounting.current) { | ||
//Stop and remove all video based tracks from video media streams | ||
navigator.mediaDevices | ||
.getUserMedia({ | ||
video: true, | ||
audio: false, | ||
}) | ||
.then((mediaStream) => | ||
mediaStream.getTracks().forEach((track) => { | ||
track.stop(); | ||
mediaStream.removeTrack(track); | ||
}) | ||
); | ||
Comment on lines
+32
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This causes an error when QrReader is conditionally rendered, because when QrReader is unmounted, no media can be found. We can handle it: navigator.mediaDevices
.getUserMedia({
video: true,
audio: false,
})
.then((mediaStream) =>
mediaStream.getTracks().forEach((track) => {
track.stop();
mediaStream.removeTrack(track);
})
)
+ .catch((error) => {
+ console.warn('Error stopping video tracks:', error);
+ }); |
||
stopRequested.current = true; | ||
} | ||
isUnmounting.current = true; | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<section className={className} style={containerStyle}> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import { BrowserQRCodeReader } from '@zxing/browser'; | ||
import { Result } from '@zxing/library'; | ||
import { Exception } from '@zxing/library'; | ||
import { MutableRefObject } from 'react'; | ||
|
||
export class StopRequestedException extends Exception { | ||
constructor() { | ||
super(); | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
} | ||
} | ||
Comment on lines
+6
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this class if we use |
||
|
||
export type QrReaderProps = { | ||
/** | ||
|
@@ -74,4 +83,6 @@ export type UseQrReaderHookProps = { | |
videoId?: string; | ||
}; | ||
|
||
export type UseQrReaderHook = (props: UseQrReaderHookProps) => void; | ||
export type UseQrReaderHook = ( | ||
props: UseQrReaderHookProps | ||
) => [MutableRefObject<boolean>]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have found that throwing an Exception here is unnecessary and undesirable as it leads to an uncaught runtime error. We can also stop the code reader using
controls
: