Skip to content

Commit

Permalink
fix(core/barcode): Fixed a bug caused by a missing undefined check on…
Browse files Browse the repository at this point in the history
… the barcode video element when resetting or stopping streams.
  • Loading branch information
Marco Tozzi authored and robzan8 committed Aug 23, 2023
1 parent 091bd56 commit 111c647
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions projects/core/barcode/src/barcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import {catchError, map, take, tap} from 'rxjs/operators';
@Directive()
export abstract class AjfBarcode implements ControlValueAccessor {
resetEvt: EventEmitter<void> = new EventEmitter<void>();
@ViewChild('barcodeVideo', {read: ElementRef}) barcodeVideo!: ElementRef<HTMLVideoElement>;
@ViewChild('barcodeVideo', {read: ElementRef}) barcodeVideo:
| ElementRef<HTMLVideoElement>
| undefined;
@ViewChild('barcodeVideoPreview', {read: ElementRef})
barcodeVideoPreview!: ElementRef<HTMLDivElement>;
@ViewChild('barcodeImagePreview', {read: ElementRef})
Expand Down Expand Up @@ -124,10 +126,12 @@ export abstract class AjfBarcode implements ControlValueAccessor {

reset(): void {
this.value = '';
const video = this.barcodeVideo.nativeElement;
const video = this.barcodeVideo?.nativeElement ?? null;
this.resetEvt.emit();
this.initVideoStreams();
video.play();
if (video) {
video.play();
}
this._onTouchedCallback();
}

Expand Down Expand Up @@ -283,11 +287,16 @@ export abstract class AjfBarcode implements ControlValueAccessor {
*/
private _gotStream(stream: MediaStream | null) {
this._currentVideoStream = stream;
this.barcodeVideo.nativeElement.srcObject = stream;
if (this.barcodeVideo) {
this.barcodeVideo.nativeElement.srcObject = stream;
}
this._cdr.markForCheck();
}

stopCurrentStream(): void {
if (this.barcodeVideo == undefined) {
return;
}
const video = this.barcodeVideo.nativeElement;
const stream: MediaStream | null = video.srcObject as MediaStream | null;
if (stream == null) return;
Expand Down

0 comments on commit 111c647

Please sign in to comment.