Skip to content

Commit 60ff03c

Browse files
committed
fix(camera-prop): unneccesary new stream requests
When an object value is passed to the `camera` prop it might change be reference without actually changing by value causing unneccesary camera stream requests. For templates like this: <qrcode-stream :camera="{}"></qrcode-stream> Each re-evaluation will generate a new empty object instance with a different reference. The new input for `camera` is therefore strictly different (`===`) from the previous although the constraints haven't actually changed. Since this shouldn't cause a new camera stream to be requested, new and old values for `camera` are deeply compared now. See #69
1 parent 06119d9 commit 60ff03c

File tree

6 files changed

+45
-39
lines changed

6 files changed

+45
-39
lines changed

config/webpack.config.common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = merge(base, {
2020
'jsqr': 'jsqr',
2121
'babel-runtime': 'babel-runtime',
2222
'webrtc-adapter': 'webrtc-adapter',
23+
'lodash': 'lodash',
2324
},
2425
plugins: [
2526
new webpack.optimize.UglifyJsPlugin({

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"dependencies": {
8686
"babel-runtime": "^6.26.0",
8787
"jsqr": "^1.1.0",
88+
"lodash": "^4.17.11",
8889
"webrtc-adapter": "^6.2.1"
8990
}
9091
}

src/components/QrcodeReader.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<script>
3737
import { keepScanning } from '../misc/scanner.js'
3838
import Camera from '../misc/camera.js'
39-
import { isBoolean } from '../misc/util.js'
39+
import isBoolean from 'lodash/isBoolean'
4040
import QrcodeDropZone from './QrcodeDropZone.vue'
4141
import CommonAPI from '../mixins/CommonAPI.vue'
4242

src/components/QrcodeStream.vue

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131
<script>
3232
import { keepScanning } from '../misc/scanner.js'
3333
import Camera from '../misc/camera.js'
34-
import { isBoolean, emptyObject } from '../misc/util.js'
3534
import CommonAPI from '../mixins/CommonAPI.vue'
35+
import isEqual from 'lodash/isEqual'
36+
import isBoolean from 'lodash/isBoolean'
37+
import isObject from 'lodash/isObject'
38+
import stubObject from 'lodash/stubObject'
3639
3740
export default {
3841
@@ -46,7 +49,7 @@ export default {
4649
4750
camera: {
4851
type: [Object, Boolean],
49-
default: emptyObject,
52+
default: stubObject,
5053
},
5154
5255
track: {
@@ -59,6 +62,7 @@ export default {
5962
return {
6063
cameraInstance: null,
6164
destroyed: false,
65+
constraints: {},
6266
}
6367
},
6468
@@ -87,30 +91,6 @@ export default {
8791
}
8892
},
8993
90-
/**
91-
* Full constraints object which is passed to `getUserMedia` to request a
92-
* camera stream. Properties define if a certain camera is adequate or not.
93-
*/
94-
constraints () {
95-
if (isBoolean(this.camera)) {
96-
return {
97-
audio: false,
98-
video: this.camera,
99-
}
100-
} else {
101-
return {
102-
audio: false,
103-
video: {
104-
facingMode: { ideal: 'environment' },
105-
width: { min: 360, ideal: 640, max: 1920 },
106-
height: { min: 240, ideal: 480, max: 1080 },
107-
108-
...this.camera,
109-
},
110-
}
111-
}
112-
},
113-
11494
trackRepaintFunction () {
11595
if (this.track === true) {
11696
return function (location, ctx) {
@@ -159,13 +139,40 @@ export default {
159139
}
160140
},
161141
162-
constraints: {
142+
camera: {
163143
deep: true,
164-
165-
handler () {
166-
this.$emit('init', this.init())
144+
immediate: true,
145+
146+
handler (camera, oldValue) {
147+
const deeplyEqual = isEqual(camera, oldValue)
148+
149+
if (deeplyEqual) {
150+
// object reference changed but constraints are actually the same
151+
return
152+
} else if (isBoolean(camera)) {
153+
this.constraints = {
154+
audio: false,
155+
video: camera,
156+
}
157+
} else if (isObject(camera)) {
158+
this.constraints = {
159+
audio: false,
160+
video: {
161+
facingMode: { ideal: 'environment' },
162+
width: { min: 360, ideal: 640, max: 1920 },
163+
height: { min: 240, ideal: 480, max: 1080 },
164+
165+
// overrides properties above if given
166+
...camera,
167+
},
168+
}
169+
}
167170
},
168171
},
172+
173+
constraints () {
174+
this.$emit('init', this.init())
175+
},
169176
},
170177
171178
mounted () {

src/misc/util.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5003,6 +5003,11 @@ lodash@^4.0.0, lodash@^4.0.1, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.3.0, lod
50035003
version "4.17.4"
50045004
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
50055005

5006+
lodash@^4.17.11:
5007+
version "4.17.11"
5008+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
5009+
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==
5010+
50065011
lodash@^4.2.1:
50075012
version "4.17.10"
50085013
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

0 commit comments

Comments
 (0)