Skip to content

Commit

Permalink
improved quality 🚀
Browse files Browse the repository at this point in the history
refactored some code to improve quality
  • Loading branch information
odahcam committed Apr 24, 2018
1 parent ffe82f2 commit c32019f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
26 changes: 18 additions & 8 deletions src/app/modules/zxing-scanner/browser-code-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class BrowserCodeReader {
*/
private startDecodeFromStream(stream: MediaStream, callbackFn?: (result: Result) => any): void {
this.stream = stream;
this.bindSrc(this.videoElement, this.stream);
this.bindVideoSrc(this.videoElement, this.stream);
this.bindEvents(this.videoElement, callbackFn);
this.checkTorchCompatibility(this.stream);
}
Expand All @@ -143,8 +143,8 @@ export class BrowserCodeReader {
* @param videoElement
* @param stream
*/
private bindSrc(videoElement: HTMLVideoElement, stream: MediaStream): void {
// Older browsers may not have srcObject
public bindVideoSrc(videoElement: HTMLVideoElement, stream: MediaStream): void {
// Older browsers may not have `srcObject`
try {
// @NOTE Throws Exception if interrupted by a new loaded request
videoElement.srcObject = stream;
Expand All @@ -154,6 +154,19 @@ export class BrowserCodeReader {
}
}

/**
* Unbinds a HTML video src property.
*
* @param videoElement
*/
public unbindVideoSrc(videoElement: HTMLVideoElement): void {
try {
this.videoElement.srcObject = null;
} catch (err) {
this.videoElement.src = '';
}
}

/**
* Binds listeners and callbacks to the videoElement.
*
Expand Down Expand Up @@ -383,11 +396,8 @@ export class BrowserCodeReader {

// then forgets about that element 😢

try {
this.videoElement.srcObject = null;
} catch (err) {
this.videoElement.src = '';
}
this.unbindVideoSrc(this.videoElement);

this.videoElement.removeAttribute('src');
this.videoElement = undefined;
}
Expand Down
16 changes: 5 additions & 11 deletions src/app/modules/zxing-scanner/zxing-scanner.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,23 +320,17 @@ export class ZXingScannerComponent implements AfterViewInit, OnDestroy, OnChange
.then((stream: MediaStream) => {

try {
// Start stream so Browser can display permission-dialog ("Website wants to access your camera, allow?")
try {
this.previewElemRef.nativeElement.srcObject = stream;
} catch (err) {
this.previewElemRef.nativeElement.src = window.URL.createObjectURL(stream);
}

// Start stream so Browser can display its permission-dialog
this.codeReader.bindVideoSrc(this.previewElemRef.nativeElement, stream);

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

try {
this.previewElemRef.nativeElement.srcObject = null;
} catch (err) {
this.previewElemRef.nativeElement.src = '';
}
// should stop the opened stream
this.codeReader.unbindVideoSrc(this.previewElemRef.nativeElement);

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

Expand Down

0 comments on commit c32019f

Please sign in to comment.