Skip to content

Commit

Permalink
fix: scanner breaks after constraint update
Browse files Browse the repository at this point in the history
Scanner does not work anymore after when `deviceId` via `constraints` is updated
and no other props are changed. This is because:

  ==> both `cameraActive.value` and `cameraSettings.shouldStream` stay `true`
  ==> then `shouldScan` also does not change
  ==> therefore the watcher on `shouldScan` is not triggered
  ==> and finally we don't start a new scanning process

To prevent that, we now explicitly set `cameraActive` to `false` right before
requesting a new camera. That is not just a hack but also makes semantically sense,
because the camera is briefly inactive right before requesting a new camera.

See: #416
  • Loading branch information
gruhn committed Feb 7, 2024
1 parent 8d011e9 commit c879435
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/components/QrcodeStream.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ onUnmounted(() => {
cameraController.stop()
})
// Collect all reactive values together that incluence the camera to have a
// Collect all reactive values together that influence the camera to have a
// single source of truth for when EXACTLY to start/stop/restart the camera.
// The watcher on this computed value should be the only function to interact
// with the camera directly and it should only interact with it in response to
Expand Down Expand Up @@ -136,6 +136,17 @@ watch(
assert(ctx !== null, 'if cavnas is defined, canvas 2d context should also be non-null')
if (cameraSettings.shouldStream) {
// When a camera is already loaded and then the `constraints` prop is changed, then
// => both `cameraActive.value` and `cameraSettings.shouldStream` stay `true`
// => so `shouldScan` does not change and thus
// => and thus the watcher on `shouldScan` is not triggered
// => and finally we don't start a new scanning process
// So in this interaction scanning breaks. To prevent that we explicitly set `cameraActive`
// to `false` here. That is not just a hack but also makes semantically sense, because
// the camera is briefly inactive right before requesting a new camera.
cameraController.stop()
cameraActive.value = false
// start camera
try {
// Usually, when the component is destroyed the `onUnmounted` hook takes care of stopping the camera.
Expand Down

0 comments on commit c879435

Please sign in to comment.