Skip to content

Commit c32019f

Browse files
committed
improved quality 🚀
refactored some code to improve quality
1 parent ffe82f2 commit c32019f

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/app/modules/zxing-scanner/browser-code-reader.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class BrowserCodeReader {
132132
*/
133133
private startDecodeFromStream(stream: MediaStream, callbackFn?: (result: Result) => any): void {
134134
this.stream = stream;
135-
this.bindSrc(this.videoElement, this.stream);
135+
this.bindVideoSrc(this.videoElement, this.stream);
136136
this.bindEvents(this.videoElement, callbackFn);
137137
this.checkTorchCompatibility(this.stream);
138138
}
@@ -143,8 +143,8 @@ export class BrowserCodeReader {
143143
* @param videoElement
144144
* @param stream
145145
*/
146-
private bindSrc(videoElement: HTMLVideoElement, stream: MediaStream): void {
147-
// Older browsers may not have srcObject
146+
public bindVideoSrc(videoElement: HTMLVideoElement, stream: MediaStream): void {
147+
// Older browsers may not have `srcObject`
148148
try {
149149
// @NOTE Throws Exception if interrupted by a new loaded request
150150
videoElement.srcObject = stream;
@@ -154,6 +154,19 @@ export class BrowserCodeReader {
154154
}
155155
}
156156

157+
/**
158+
* Unbinds a HTML video src property.
159+
*
160+
* @param videoElement
161+
*/
162+
public unbindVideoSrc(videoElement: HTMLVideoElement): void {
163+
try {
164+
this.videoElement.srcObject = null;
165+
} catch (err) {
166+
this.videoElement.src = '';
167+
}
168+
}
169+
157170
/**
158171
* Binds listeners and callbacks to the videoElement.
159172
*
@@ -383,11 +396,8 @@ export class BrowserCodeReader {
383396

384397
// then forgets about that element 😢
385398

386-
try {
387-
this.videoElement.srcObject = null;
388-
} catch (err) {
389-
this.videoElement.src = '';
390-
}
399+
this.unbindVideoSrc(this.videoElement);
400+
391401
this.videoElement.removeAttribute('src');
392402
this.videoElement = undefined;
393403
}

src/app/modules/zxing-scanner/zxing-scanner.component.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,23 +320,17 @@ export class ZXingScannerComponent implements AfterViewInit, OnDestroy, OnChange
320320
.then((stream: MediaStream) => {
321321

322322
try {
323-
// Start stream so Browser can display permission-dialog ("Website wants to access your camera, allow?")
324-
try {
325-
this.previewElemRef.nativeElement.srcObject = stream;
326-
} catch (err) {
327-
this.previewElemRef.nativeElement.src = window.URL.createObjectURL(stream);
328-
}
323+
324+
// Start stream so Browser can display its permission-dialog
325+
this.codeReader.bindVideoSrc(this.previewElemRef.nativeElement, stream);
329326

330327
// After permission was granted, we can stop it again
331328
stream.getVideoTracks().forEach(track => {
332329
track.stop();
333330
});
334331

335-
try {
336-
this.previewElemRef.nativeElement.srcObject = null;
337-
} catch (err) {
338-
this.previewElemRef.nativeElement.src = '';
339-
}
332+
// should stop the opened stream
333+
this.codeReader.unbindVideoSrc(this.previewElemRef.nativeElement);
340334

341335
// if the scripts lives until here, that's only one mean:
342336

0 commit comments

Comments
 (0)